@@ -158,7 +158,6 @@ proc new*(T: type PeerManager, switch: Switch, storage: PeerStorage = nil): Peer
158
158
# Manager interface #
159
159
# ####################
160
160
161
- # TODO: Move to peer store and unit test
162
161
proc addPeer * (pm: PeerManager , remotePeerInfo: RemotePeerInfo , proto: string ) =
163
162
# Adds peer to manager for the specified protocol
164
163
@@ -195,31 +194,26 @@ proc reconnectPeers*(pm: PeerManager,
195
194
debug " Reconnecting peers" , proto= proto
196
195
197
196
for storedInfo in pm.peerStore.peers (protocolMatcher):
198
- # Check that peer exists and can be connected
199
- if storedInfo.peerId notin pm.peerStore[ConnectionBook ] or
200
- pm.peerStore[ConnectionBook ][storedInfo.peerId] == CannotConnect :
197
+ # Check that the peer can be connected
198
+ if storedInfo.connection == CannotConnect :
201
199
debug " Not reconnecting to unreachable or non-existing peer" , peerId= storedInfo.peerId
202
200
continue
203
201
204
202
# Respect optional backoff period where applicable.
205
203
let
206
- disconnectTime = Moment .init (pm.peerStore[DisconnectBook ][storedInfo.peerId], Second ) # Convert
204
+ # TODO : Add method to peerStore (eg isBackoffExpired())
205
+ disconnectTime = Moment .init (storedInfo.disconnectTime, Second ) # Convert
207
206
currentTime = Moment .init (getTime ().toUnix, Second ) # Current time comparable to persisted value
208
207
backoffTime = disconnectTime + backoff - currentTime # Consider time elapsed since last disconnect
209
208
210
209
trace " Respecting backoff" , backoff= backoff, disconnectTime= disconnectTime, currentTime= currentTime, backoffTime= backoffTime
211
210
211
+ # TODO: This blocks the whole function. Try to connect to another peer in the meantime.
212
212
if backoffTime > ZeroDuration :
213
213
debug " Backing off before reconnect..." , peerId= storedInfo.peerId, backoffTime= backoffTime
214
214
# We disconnected recently and still need to wait for a backoff period before connecting
215
215
await sleepAsync (backoffTime)
216
216
217
- # Add to protos for peer, if it has not been added yet
218
- if not pm.peerStore.get (storedInfo.peerId).protos.contains (proto):
219
- let remotePeerInfo = storedInfo.toRemotePeerInfo ()
220
- trace " Adding newly dialed peer to manager" , peerId = remotePeerInfo.peerId, addr = remotePeerInfo.addrs[0 ], proto = proto
221
- pm.addPeer (remotePeerInfo, proto)
222
-
223
217
trace " Reconnecting to peer" , peerId= storedInfo.peerId
224
218
discard await pm.dialPeer (storedInfo.peerId, toSeq (storedInfo.addrs), proto)
225
219
0 commit comments