mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-10-12 01:58:46 +02:00
fix(net): port transport resource cleanup
This commit is contained in:
1 parent
163c8c75c6
commit
b8c6ca8f7a
6 files changed
+1020
-109
No files matched your search
+16
-12
@@ -294,8 +294,6 @@ func (d *ParallelDialer) DialContext(ctx context.Context, network string, addrs
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
ch := make(chan *parallelDialerResult, len(live))
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(live))
|
||||
@@ -317,25 +315,31 @@ func (d *ParallelDialer) DialContext(ctx context.Context, network string, addrs
|
||||
} else {
|
||||
unreachable.markReachable(addr)
|
||||
}
|
||||
select {
|
||||
case ch <- ¶llelDialerResult{conn: conn, err: err}:
|
||||
case <-done:
|
||||
if conn != nil {
|
||||
logger.Debug("Connection closed", zap.String("remote_address", conn.RemoteAddr().String()))
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
ch <- ¶llelDialerResult{conn: conn, err: err}
|
||||
}(addr)
|
||||
}
|
||||
|
||||
errs := make([]error, 0, len(addrs))
|
||||
errs := make([]error, 0, len(live))
|
||||
for res := range ch {
|
||||
if res.err == nil {
|
||||
cancel()
|
||||
logger.Debug("Connected to", zap.String("remote_address", res.conn.RemoteAddr().String()))
|
||||
return res.conn, res.err
|
||||
// Losing dials may already have connected, or may still connect
|
||||
// after cancel; close whatever they hand back.
|
||||
go closeLosingConns(ch, logger)
|
||||
return res.conn, nil
|
||||
}
|
||||
errs = append(errs, res.err)
|
||||
}
|
||||
return nil, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// closeLosingConns drains ch and closes every connection left in it.
|
||||
func closeLosingConns(ch <-chan *parallelDialerResult, logger *zap.Logger) {
|
||||
for res := range ch {
|
||||
if res.conn != nil {
|
||||
logger.Debug("Connection closed", zap.String("remote_address", res.conn.RemoteAddr().String()))
|
||||
_ = res.conn.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user