derp/derphttp: fix reconnect behavior

I broke it in today's 752146a70f.

Added some comments to prevent recurrences.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2020-02-28 13:38:45 -08:00
parent d59d0576af
commit 517a91de75

View File

@ -214,7 +214,7 @@ func (c *Client) Send(dstKey key.Public, b []byte) error {
return err
}
if err := client.Send(dstKey, b); err != nil {
c.Close()
c.closeForReconnect()
}
return err
}
@ -226,7 +226,7 @@ func (c *Client) Recv(b []byte) (derp.ReceivedMessage, error) {
}
m, err := client.Recv(b)
if err != nil {
c.Close()
c.closeForReconnect()
}
return m, err
}
@ -244,10 +244,21 @@ func (c *Client) Close() error {
c.closed = true
if c.netConn != nil {
c.netConn.Close()
c.netConn = nil
}
c.client = nil
return nil
}
// closeForReconnect closes the underlying network connection and
// zeros out the client field so future calls to Connect will
// reconnect.
func (c *Client) closeForReconnect() {
c.mu.Lock()
defer c.mu.Unlock()
if c.netConn != nil {
c.netConn.Close()
c.netConn = nil
}
c.client = nil
}
var ErrClientClosed = errors.New("derphttp.Client closed")