Skip to content

Commit a9b9836

Browse files
committed
Clean up failed conn hooks
1 parent a75dde3 commit a9b9836

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

client/net/conn.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ type Conn struct {
1717
ID hooks.ConnectionID
1818
}
1919

20-
// Close overrides the net.Conn Close method to execute all registered hooks after closing the connection
21-
// Close overrides the net.Conn Close method to execute all registered hooks before closing the connection.
20+
// Close overrides the net.Conn Close method to execute all registered hooks after closing the connection.
2221
func (c *Conn) Close() error {
2322
return closeConn(c.ID, c.Conn)
2423
}
@@ -29,21 +28,24 @@ type TCPConn struct {
2928
ID hooks.ConnectionID
3029
}
3130

32-
// Close overrides the net.TCPConn Close method to execute all registered hooks before closing the connection.
31+
// Close overrides the net.TCPConn Close method to execute all registered hooks after closing the connection.
3332
func (c *TCPConn) Close() error {
3433
return closeConn(c.ID, c.TCPConn)
3534
}
3635

3736
// closeConn is a helper function to close connections and execute close hooks.
3837
func closeConn(id hooks.ConnectionID, conn io.Closer) error {
3938
err := conn.Close()
39+
cleanupConnID(id)
40+
return err
41+
}
4042

43+
// cleanupConnID executes close hooks for a connection ID.
44+
func cleanupConnID(id hooks.ConnectionID) {
4145
closeHooks := hooks.GetCloseHooks()
4246
for _, hook := range closeHooks {
4347
if err := hook(id); err != nil {
4448
log.Errorf("Error executing close hook: %v", err)
4549
}
4650
}
47-
48-
return err
4951
}

client/net/dial.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func DialTCP(network string, laddr, raddr *net.TCPAddr) (transport.TCPConn, erro
7474
}
7575
return &TCPConn{TCPConn: tcpConn, ID: c.ID}, nil
7676
}
77-
7877
if err := conn.Close(); err != nil {
7978
log.Errorf("failed to close connection: %v", err)
8079
}

client/net/dialer_dial.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.
3030

3131
conn, err := d.Dialer.DialContext(ctx, network, address)
3232
if err != nil {
33+
cleanupConnID(connID)
3334
return nil, fmt.Errorf("d.Dialer.DialContext: %w", err)
3435
}
3536

@@ -64,7 +65,7 @@ func callDialerHooks(ctx context.Context, connID hooks.ConnectionID, address str
6465

6566
ips, err := resolver.LookupIPAddr(ctx, host)
6667
if err != nil {
67-
return fmt.Errorf("failed to resolve address %s: %w", address, err)
68+
return fmt.Errorf("resolve address %s: %w", address, err)
6869
}
6970

7071
log.Debugf("Dialer resolved IPs for %s: %v", address, ips)

client/net/listener_listen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
4848
return c.PacketConn.WriteTo(b, addr)
4949
}
5050

51-
// Close overrides the net.PacketConn Close method to execute all registered hooks before closing the connection.
51+
// Close overrides the net.PacketConn Close method to execute all registered hooks after closing the connection.
5252
func (c *PacketConn) Close() error {
5353
defer c.seenAddrs.Clear()
5454
return closeConn(c.ID, c.PacketConn)
@@ -69,7 +69,7 @@ func (c *UDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
6969
return c.UDPConn.WriteTo(b, addr)
7070
}
7171

72-
// Close overrides the net.UDPConn Close method to execute all registered hooks before closing the connection.
72+
// Close overrides the net.UDPConn Close method to execute all registered hooks after closing the connection.
7373
func (c *UDPConn) Close() error {
7474
defer c.seenAddrs.Clear()
7575
return closeConn(c.ID, c.UDPConn)

0 commit comments

Comments
 (0)