[client] Give the netsweep registry a named id type

The connection and dial registries keyed on a bare uint64, which says
nothing about what the number identifies. Introduce sweepID so the maps,
the counter and the id fields state their intent. No behavior change.
This commit is contained in:
Zoltán Papp
2026-08-12 10:14:51 +02:00
parent 53f2518af1
commit ecce1aa2a1
+14 -10
View File
@@ -20,6 +20,10 @@ import (
// as a failed dial and redial on the new network. // as a failed dial and redial on the new network.
var ErrSwept = errors.New("netsweep: connection swept by network change") var ErrSwept = errors.New("netsweep: connection swept by network change")
// sweepID identifies one registration in a sweeper. Connections and dials
// draw from the same counter, so an id is unique across both registries.
type sweepID uint64
// Dial tracks one dial from start to connection registration. It hands the // Dial tracks one dial from start to connection registration. It hands the
// dialed connection to the sweeper atomically, so a sweep can never fall // dialed connection to the sweeper atomically, so a sweep can never fall
// between the dial finishing and the connection being registered. // between the dial finishing and the connection being registered.
@@ -27,7 +31,7 @@ type Dial struct {
sweeper *Sweeper sweeper *Sweeper
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
id uint64 id sweepID
done bool // set by Sweep, WrapConn or Release; guarded by sweeper.mu done bool // set by Sweep, WrapConn or Release; guarded by sweeper.mu
} }
@@ -57,7 +61,7 @@ func (d *Dial) Release() {
type sweptConn struct { type sweptConn struct {
net.Conn net.Conn
sweeper *Sweeper sweeper *Sweeper
id uint64 id sweepID
} }
func (c *sweptConn) Close() error { func (c *sweptConn) Close() error {
@@ -69,16 +73,16 @@ func (c *sweptConn) Close() error {
// everything that started before the network changed. // everything that started before the network changed.
type Sweeper struct { type Sweeper struct {
mu sync.Mutex mu sync.Mutex
conns map[uint64]net.Conn conns map[sweepID]net.Conn
dials map[uint64]*Dial dials map[sweepID]*Dial
nextID uint64 nextID sweepID
} }
// New creates an empty sweeper. // New creates an empty sweeper.
func New() *Sweeper { func New() *Sweeper {
return &Sweeper{ return &Sweeper{
conns: make(map[uint64]net.Conn), conns: make(map[sweepID]net.Conn),
dials: make(map[uint64]*Dial), dials: make(map[sweepID]*Dial),
} }
} }
@@ -141,8 +145,8 @@ func (s *Sweeper) Sweep() int {
s.mu.Lock() s.mu.Lock()
conns := s.conns conns := s.conns
dials := s.dials dials := s.dials
s.conns = make(map[uint64]net.Conn) s.conns = make(map[sweepID]net.Conn)
s.dials = make(map[uint64]*Dial) s.dials = make(map[sweepID]*Dial)
for _, d := range dials { for _, d := range dials {
d.done = true d.done = true
} }
@@ -164,7 +168,7 @@ func (s *Sweeper) Sweep() int {
return len(conns) return len(conns)
} }
func (s *Sweeper) deregister(id uint64) { func (s *Sweeper) deregister(id sweepID) {
s.mu.Lock() s.mu.Lock()
delete(s.conns, id) delete(s.conns, id)
s.mu.Unlock() s.mu.Unlock()