import { DEFAULT_MAX_RECONNECT_TRIES } from '../constants' ; import { P2PPeerInfo } from '../types' ; import { PEER_TYPE } from '../utils' ; import { BaseList , PeerListConfig } from './base_list' ; export interface TriedListConfig extends PeerListConfig { readonly maxReconnectTries ? : number ; } export class TriedList extends BaseList { private readonly _maxReconnectTries : number ; public constructor ( { numOfBuckets , bucketSize , maxReconnectTries , secret , peerType , } ) { super ( { secret , numOfBuckets , bucketSize , peerType , } ) ; this . type = PEER_TYPE . TRIED_PEER ; this . _maxReconnectTries = maxReconnectTries ?? DEFAULT_MAX_RECONNECT_TRIES ; } public get triedPeerConfig ( ) : TriedListConfig { return { ... this . peerListConfig , maxReconnectTries : this . _maxReconnectTries , } ; } public failedConnectionAction ( incomingPeerInfo ) { const { bucket } = this . calculateBucket ( incomingPeerInfo . ipAddress ) ; const incomingPeerId = incomingPeerInfo . peerId ; const foundPeer = bucket . get ( incomingPeerId ) ; if ( ! foundPeer ) { return false ; } const { numOfConnectionFailures } = foundPeer ; if ( ( numOfConnectionFailures as number ) + 1 >= this . _maxReconnectTries ) { const removedFromBucket = bucket . delete ( incomingPeerId ) ; const removedFromPeerLookup = this . peerIdToPeerInfo . delete ( incomingPeerId ) ; return removedFromBucket && removedFromPeerLookup ; } const updatedTriedPeerInfo = { ... foundPeer , numOfConnectionFailures : ( numOfConnectionFailures as number ) + 1 , } ; bucket . set ( incomingPeerId , updatedTriedPeerInfo ) ; return false ; } }