Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e7197c

Browse files
committed
Add closed func
1 parent d03849e commit 5e7197c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

agent/conn.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ type TailnetConn struct {
138138
*tailnet.Conn
139139
}
140140

141-
func (c *TailnetConn) Closed() <-chan struct{} {
142-
return nil
143-
}
144-
145141
func (c *TailnetConn) Ping() (time.Duration, error) {
146142
return 0, nil
147143
}

tailnet/conn.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ func IP() netip.Addr {
221221
// Conn is an actively listening Wireguard connection.
222222
type Conn struct {
223223
mutex sync.Mutex
224+
closed chan struct{}
224225
logger slog.Logger
225226

226227
dialer *tsdial.Dialer
@@ -332,13 +333,25 @@ func (c *Conn) Ping(ip netip.Addr, pingType tailcfg.PingType, cb func(*ipnstate.
332333
c.wireguardEngine.Ping(ip, pingType, cb)
333334
}
334335

336+
// Closed is a channel that ends when the connection has
337+
// been closed.
338+
func (c *Conn) Closed() chan<- struct{} {
339+
return c.closed
340+
}
341+
335342
// Close shuts down the Wireguard connection.
336343
func (c *Conn) Close() error {
337344
for _, l := range c.listeners {
338345
_ = l.Close()
339346
}
340347
c.mutex.Lock()
341348
defer c.mutex.Unlock()
349+
select {
350+
case <-c.closed:
351+
return nil
352+
default:
353+
}
354+
close(c.closed)
342355
_ = c.dialer.Close()
343356
_ = c.magicConn.Close()
344357
_ = c.netStack.Close()

0 commit comments

Comments
 (0)