Skip to content

Commit

Permalink
fix: begin routing table refresh after routing table has started (#2511)
Browse files Browse the repository at this point in the history
Fixes an error in the logs where we try to refresh the routing table
before it's started.

Switch to doing it in `afterStart` so the routing table is available.
  • Loading branch information
achingbrain authored Apr 30, 2024
1 parent 9e9a32b commit 3bc94b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
40 changes: 19 additions & 21 deletions packages/kad-dht/src/kad-dht.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol } from '@libp2p/interface'
import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol, start, stop } from '@libp2p/interface'
import drain from 'it-drain'
import pDefer from 'p-defer'
import { PROTOCOL } from './constants.js'
Expand Down Expand Up @@ -379,16 +379,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
// Only respond to queries when not in client mode
await this.setMode(this.clientMode ? 'client' : 'server')

this.querySelf.start()

await Promise.all([
this.providers.start(),
this.queryManager.start(),
this.network.start(),
this.routingTable.start(),
this.topologyListener.start(),
this.routingTableRefresh.start()
])
await start(
this.querySelf,
this.providers,
this.queryManager,
this.network,
this.routingTable,
this.topologyListener,
this.routingTableRefresh
)
}

/**
Expand All @@ -398,16 +397,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
async stop (): Promise<void> {
this.running = false

this.querySelf.stop()

await Promise.all([
this.providers.stop(),
this.queryManager.stop(),
this.network.stop(),
this.routingTable.stop(),
this.routingTableRefresh.stop(),
this.topologyListener.stop()
])
await stop(
this.querySelf,
this.providers,
this.queryManager,
this.network,
this.routingTable,
this.routingTableRefresh,
this.topologyListener
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/kad-dht/src/routing-table/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class RoutingTableRefresh {
this.refreshTable = this.refreshTable.bind(this)
}

async start (): Promise<void> {
async afterStart (): Promise<void> {
this.log(`refreshing routing table every ${this.refreshInterval}ms`)
this.refreshTable(true)
}
Expand Down

0 comments on commit 3bc94b4

Please sign in to comment.