Fix guard handling

This commit is contained in:
Zoltán Papp 2024-10-29 10:59:04 +01:00
parent 7bde2ac09c
commit 8f391b3ec1
3 changed files with 13 additions and 5 deletions

View File

@ -38,10 +38,15 @@ func NewGuard(sp *ServerPicker) *Guard {
// - relayClient: The relay client instance that was disconnected.
// todo prevent multiple reconnection instances. In the current usage it should not happen, but it is better to prevent
func (g *Guard) StartReconnectTrys(ctx context.Context, relayClient *Client) {
if relayClient == nil {
goto RETRY
}
if g.isServerURLStillValid(relayClient) && g.quickReconnect(ctx, relayClient) {
return
}
RETRY:
// todo use exponent ticker
ticker := time.NewTicker(reconnectingTimeout)
defer ticker.Stop()
@ -60,6 +65,7 @@ func (g *Guard) StartReconnectTrys(ctx context.Context, relayClient *Client) {
}
func (g *Guard) retry(ctx context.Context) error {
log.Infof("try to pick up a new Relay server")
relayClient, err := g.serverPicker.PickServer(ctx)
if err != nil {
return err
@ -80,6 +86,7 @@ func (g *Guard) quickReconnect(parentCtx context.Context, rc *Client) bool {
if parentCtx.Err() != nil {
return false
}
log.Infof("try to reconnect to Relay server: %s", rc.connectionURL)
if err := rc.Connect(); err != nil {
log.Errorf("failed to reconnect to relay server: %s", err)

View File

@ -89,6 +89,7 @@ func NewManager(ctx context.Context, serverURLs []string, peerID string) *Manage
onDisconnectedListeners: make(map[string]*list.List),
}
m.serverPicker.ServerURLs.Store(serverURLs)
m.reconnectGuard = NewGuard(m.serverPicker)
return m
}
@ -103,14 +104,14 @@ func (m *Manager) Serve() error {
m.running = true
log.Debugf("starting relay client manager with %v relay servers", m.serverPicker.ServerURLs.Load())
m.reconnectGuard = NewGuard(m.serverPicker)
go m.listenGuardEvent(m.ctx)
client, err := m.serverPicker.PickServer(m.ctx)
if err == nil {
if err != nil {
m.reconnectGuard.StartReconnectTrys(m.ctx, nil)
} else {
m.storeClient(client)
}
go m.listenGuardEvent(m.ctx)
go m.startCleanupLoop()
return err
}

View File

@ -80,7 +80,7 @@ func (sp *ServerPicker) processConnResults(resultChan chan connResult, successCh
for numOfResults := 0; numOfResults < cap(resultChan); numOfResults++ {
cr := <-resultChan
if cr.Err != nil {
log.Debugf("failed to connect to Relay server: %s: %v", cr.Url, cr.Err)
log.Tracef("failed to connect to Relay server: %s: %v", cr.Url, cr.Err)
continue
}
log.Infof("connected to Relay server: %s", cr.Url)