mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-07 08:44:07 +01:00
Fix unused servers cleanup (#2826)
The cleanup loop did not manage those situations well when a connection failed or the connection success but the code did not add a peer connection to it yet. - in the cleanup loop check if a connection failed to a server - after adding a foreign server connection force to keep it a minimum 5 sec
This commit is contained in:
parent
bac95ace18
commit
0eb99c266a
@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
relayCleanupInterval = 60 * time.Second
|
relayCleanupInterval = 60 * time.Second
|
||||||
|
keepUnusedServerTime = 5 * time.Second
|
||||||
|
|
||||||
ErrRelayClientNotConnected = fmt.Errorf("relay client not connected")
|
ErrRelayClientNotConnected = fmt.Errorf("relay client not connected")
|
||||||
)
|
)
|
||||||
@ -27,10 +28,13 @@ type RelayTrack struct {
|
|||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
relayClient *Client
|
relayClient *Client
|
||||||
err error
|
err error
|
||||||
|
created time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRelayTrack() *RelayTrack {
|
func NewRelayTrack() *RelayTrack {
|
||||||
return &RelayTrack{}
|
return &RelayTrack{
|
||||||
|
created: time.Now(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type OnServerCloseListener func()
|
type OnServerCloseListener func()
|
||||||
@ -302,6 +306,18 @@ func (m *Manager) cleanUpUnusedRelays() {
|
|||||||
|
|
||||||
for addr, rt := range m.relayClients {
|
for addr, rt := range m.relayClients {
|
||||||
rt.Lock()
|
rt.Lock()
|
||||||
|
// if the connection failed to the server the relay client will be nil
|
||||||
|
// but the instance will be kept in the relayClients until the next locking
|
||||||
|
if rt.err != nil {
|
||||||
|
rt.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Since(rt.created) <= keepUnusedServerTime {
|
||||||
|
rt.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if rt.relayClient.HasConns() {
|
if rt.relayClient.HasConns() {
|
||||||
rt.Unlock()
|
rt.Unlock()
|
||||||
continue
|
continue
|
||||||
|
@ -288,8 +288,9 @@ func TestForeginAutoClose(t *testing.T) {
|
|||||||
t.Fatalf("failed to close connection: %s", err)
|
t.Fatalf("failed to close connection: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("waiting for relay cleanup: %s", relayCleanupInterval+1*time.Second)
|
timeout := relayCleanupInterval + keepUnusedServerTime + 1*time.Second
|
||||||
time.Sleep(relayCleanupInterval + 1*time.Second)
|
t.Logf("waiting for relay cleanup: %s", timeout)
|
||||||
|
time.Sleep(timeout)
|
||||||
if len(mgr.relayClients) != 0 {
|
if len(mgr.relayClients) != 0 {
|
||||||
t.Errorf("expected 0, got %d", len(mgr.relayClients))
|
t.Errorf("expected 0, got %d", len(mgr.relayClients))
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServerPicker_UnavailableServers(t *testing.T) {
|
func TestServerPicker_UnavailableServers(t *testing.T) {
|
||||||
@ -13,7 +12,7 @@ func TestServerPicker_UnavailableServers(t *testing.T) {
|
|||||||
PeerID: "test",
|
PeerID: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout+1)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
Loading…
Reference in New Issue
Block a user