Skip to content

Commit 878a05e

Browse files
authored
fix(networking): fix wrong peer connected state (#1560)
1 parent f0eddc6 commit 878a05e

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

waku/v2/node/peer_manager/peer_manager.nim

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const
4545
MaxParalelDials = 10
4646

4747
# Delay between consecutive relayConnectivityLoop runs
48-
ConnectivityLoopInterval = chronos.seconds(30)
48+
ConnectivityLoopInterval = chronos.seconds(15)
4949

5050
# How often the peer store is pruned
5151
PrunePeerStoreInterval = chronos.minutes(5)
@@ -156,24 +156,36 @@ proc loadFromStorage(pm: PeerManager) =
156156
# Initialisation #
157157
##################
158158

159+
# currently disabled. note that peer connection state connected/disconnected
160+
# cant be tracked using this handler when more than one conn is allowed and
161+
# when using autonat. eg if a peer has 2 conns and one is disconnected we cant
162+
# assume that the peer is disconnected, because the other one might still be active.
163+
# note that even with maxconn = 1, autonat forces more than one connection.
159164
proc onConnEvent(pm: PeerManager, peerId: PeerID, event: ConnEvent) {.async.} =
160165

161166
case event.kind
162167
of ConnEventKind.Connected:
163168
let direction = if event.incoming: Inbound else: Outbound
169+
discard
170+
of ConnEventKind.Disconnected:
171+
discard
172+
173+
proc onPeerEvent(pm: PeerManager, peerId: PeerId, event: PeerEvent) {.async.} =
174+
if event.kind == PeerEventKind.Joined:
175+
let direction = if event.initiator: Outbound else: Inbound
164176
pm.peerStore[ConnectionBook][peerId] = Connected
165177
pm.peerStore[DirectionBook][peerId] = direction
166-
167178
waku_connected_peers.inc(1, labelValues=[$direction])
168179

169180
if not pm.storage.isNil:
170181
pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), Connected)
171182
return
172-
of ConnEventKind.Disconnected:
173-
waku_connected_peers.dec(1, labelValues=[$pm.peerStore[DirectionBook][peerId]])
174183

184+
elif event.kind == PeerEventKind.Left:
175185
pm.peerStore[DirectionBook][peerId] = UnknownDirection
176186
pm.peerStore[ConnectionBook][peerId] = CanConnect
187+
waku_connected_peers.dec(1, labelValues=[$pm.peerStore[DirectionBook][peerId]])
188+
177189
if not pm.storage.isNil:
178190
pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), CanConnect, getTime().toUnix)
179191
return
@@ -199,14 +211,21 @@ proc new*(T: type PeerManager,
199211
initialBackoffInSec: initialBackoffInSec,
200212
backoffFactor: backoffFactor,
201213
maxFailedAttempts: maxFailedAttempts)
202-
proc peerHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
214+
proc connHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
203215
onConnEvent(pm, peerId, event)
204216

217+
proc peerHook(peerId: PeerId, event: PeerEvent): Future[void] {.gcsafe.} =
218+
onPeerEvent(pm, peerId, event)
219+
205220
proc peerStoreChanged(peerId: PeerId) {.gcsafe.} =
206221
waku_peer_store_size.set(toSeq(pm.peerStore[AddressBook].book.keys).len.int64)
207222

208-
pm.switch.addConnEventHandler(peerHook, ConnEventKind.Connected)
209-
pm.switch.addConnEventHandler(peerHook, ConnEventKind.Disconnected)
223+
# currently disabled
224+
#pm.switch.addConnEventHandler(connHook, ConnEventKind.Connected)
225+
#pm.switch.addConnEventHandler(connHook, ConnEventKind.Disconnected)
226+
227+
pm.switch.addPeerEventHandler(peerHook, PeerEventKind.Joined)
228+
pm.switch.addPeerEventHandler(peerHook, PeerEventKind.Left)
210229

211230
# called every time the peerstore is updated
212231
pm.peerStore[AddressBook].addHandler(peerStoreChanged)

0 commit comments

Comments
 (0)