mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-15 03:11:02 +01:00
- Fix reconnect guard
- Avoid double client creation
This commit is contained in:
parent
6801dcb3f6
commit
06ceac65de
@ -19,6 +19,10 @@ const (
|
|||||||
serverResponseTimeout = 8 * time.Second
|
serverResponseTimeout = 8 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrConnAlreadyExists = fmt.Errorf("connection already exists")
|
||||||
|
)
|
||||||
|
|
||||||
// Msg carry the payload from the server to the client. With this sturct, the net.Conn can free the buffer.
|
// Msg carry the payload from the server to the client. With this sturct, the net.Conn can free the buffer.
|
||||||
type Msg struct {
|
type Msg struct {
|
||||||
Payload []byte
|
Payload []byte
|
||||||
@ -157,6 +161,11 @@ func (c *Client) OpenConn(dstPeerID string) (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hashedID, hashedStringID := messages.HashID(dstPeerID)
|
hashedID, hashedStringID := messages.HashID(dstPeerID)
|
||||||
|
_, ok := c.conns[hashedStringID]
|
||||||
|
if ok {
|
||||||
|
return nil, ErrConnAlreadyExists
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("open connection to peer: %s", hashedStringID)
|
log.Infof("open connection to peer: %s", hashedStringID)
|
||||||
msgChannel := make(chan Msg, 2)
|
msgChannel := make(chan Msg, 2)
|
||||||
conn := NewConn(c, hashedID, hashedStringID, msgChannel)
|
conn := NewConn(c, hashedID, hashedStringID, msgChannel)
|
||||||
|
@ -26,6 +26,7 @@ func Dial(address string) (net.Conn, error) {
|
|||||||
|
|
||||||
wsConn, _, err := websocket.Dial(context.Background(), url, opts)
|
wsConn, _, err := websocket.Dial(context.Background(), url, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("failed to dial to Relay server '%s': %s", url, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ package client
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -24,13 +26,20 @@ func NewGuard(context context.Context, relayClient *Client) *Guard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Guard) OnDisconnected() {
|
func (g *Guard) OnDisconnected() {
|
||||||
timeout := time.NewTimer(reconnectingTimeout)
|
ticker := time.NewTicker(reconnectingTimeout)
|
||||||
defer timeout.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
select {
|
for {
|
||||||
case <-timeout.C:
|
select {
|
||||||
_ = g.relayClient.Connect()
|
case <-ticker.C:
|
||||||
case <-g.ctx.Done():
|
err := g.relayClient.Connect()
|
||||||
return
|
if err != nil {
|
||||||
|
log.Errorf("failed to reconnect to relay server: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case <-g.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user