Skip to content

Commit

Permalink
[client] Feature/relay integration fix reconnection (#2405)
Browse files Browse the repository at this point in the history
When a peer disconnects gracefully from the server and immediately connects back, then the server will clean up an incorrect peer instance from the in-memory store. In this fix the store checks the peers instance before deleting it.
  • Loading branch information
pappz authored Aug 16, 2024
1 parent 2f848cc commit 2b00c99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion relay/server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewStore() *Store {
}

// AddPeer adds a peer to the store
// It distinguishes the peers by their ID
// todo: consider to close peer conn if the peer already exists
func (s *Store) AddPeer(peer *Peer) {
s.peersLock.Lock()
defer s.peersLock.Unlock()
Expand All @@ -30,6 +30,15 @@ func (s *Store) AddPeer(peer *Peer) {
func (s *Store) DeletePeer(peer *Peer) {
s.peersLock.Lock()
defer s.peersLock.Unlock()

dp, ok := s.peers[peer.String()]
if !ok {
return
}
if dp != peer {
return
}

delete(s.peers, peer.String())
}

Expand Down
30 changes: 30 additions & 0 deletions relay/server/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package server

import (
"testing"
)

func TestStore_DeletePeer(t *testing.T) {
s := NewStore()
p := NewPeer([]byte("peer_one"), nil, nil)

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

not enough arguments in call to NewPeer

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

not enough arguments in call to NewPeer

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

not enough arguments in call to NewPeer

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

not enough arguments in call to NewPeer

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

not enough arguments in call to NewPeer

Check failure on line 9 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test

not enough arguments in call to NewPeer
s.AddPeer(p)
s.DeletePeer(p)
if _, ok := s.Peer(p.String()); ok {
t.Errorf("peer was not deleted")
}
}

func TestStore_DeleteDeprecatedPeer(t *testing.T) {
s := NewStore()

p1 := NewPeer([]byte("peer_id"), nil, nil)

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

not enough arguments in call to NewPeer

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

not enough arguments in call to NewPeer

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

not enough arguments in call to NewPeer

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

not enough arguments in call to NewPeer

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

not enough arguments in call to NewPeer

Check failure on line 20 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test

not enough arguments in call to NewPeer
p2 := NewPeer([]byte("peer_id"), nil, nil)

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

not enough arguments in call to NewPeer

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

not enough arguments in call to NewPeer

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

not enough arguments in call to NewPeer

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

not enough arguments in call to NewPeer

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

not enough arguments in call to NewPeer

Check failure on line 21 in relay/server/store_test.go

View workflow job for this annotation

GitHub Actions / test

not enough arguments in call to NewPeer

s.AddPeer(p1)
s.AddPeer(p2)
s.DeletePeer(p1)

if _, ok := s.Peer(p2.String()); !ok {
t.Errorf("second peer was deleted")
}
}

0 comments on commit 2b00c99

Please sign in to comment.