Skip to content

Commit

Permalink
feat: allow passing ProvidersInit in KadDHT constructor (libp2p#404)
Browse files Browse the repository at this point in the history
The `Providers` component takes a `ProvidersInit` object, but there's not actually a way to provide it with one from the `KadDHT` constructor. This means users aren't able to override the defaults for how long provider records are valid for and how frequently to clean up expired provider records.

Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
joeltg and achingbrain authored Dec 7, 2022
1 parent e27747a commit e64af85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { KadDHT as SingleKadDHT } from './kad-dht.js'
import { DualKadDHT } from './dual-kad-dht.js'
import type { ProvidersInit } from './providers.js'
import type { Selectors, Validators } from '@libp2p/interface-dht'
import type { Registrar } from '@libp2p/interface-registrar'
import type { AddressManager } from '@libp2p/interface-address-manager'
Expand Down Expand Up @@ -62,6 +63,11 @@ export interface KadDHTInit {
* How many parallel outgoing streams to allow on the DHT protocol per-connection
*/
maxOutboundStreams?: number

/**
* Initialization options for the Providers component
*/
providers?: ProvidersInit
}

export interface KadDHTComponents {
Expand Down
5 changes: 3 additions & 2 deletions src/kad-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
pingTimeout,
pingConcurrency,
maxInboundStreams,
maxOutboundStreams
maxOutboundStreams,
providers: providersInit
} = init

this.running = false
Expand All @@ -102,7 +103,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
protocol: this.protocol
})

this.providers = new Providers(components)
this.providers = new Providers(components, providersInit ?? {})

this.validators = {
...recordValidators,
Expand Down
14 changes: 14 additions & 0 deletions test/kad-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ describe('KadDHT', () => {
expect(dht).to.have.property('getMode')
expect(dht).to.have.property('setMode')
})

it('forward providers init options to providers component', async () => {
const dht = await tdht.spawn({
kBucketSize: 5,
providers: {
cleanupInterval: 60,
provideValidity: 60 * 10
}
})
expect(dht.lan.providers).to.have.property('cleanupInterval', 60)
expect(dht.lan.providers).to.have.property('provideValidity', 60 * 10)
expect(dht.wan.providers).to.have.property('cleanupInterval', 60)
expect(dht.wan.providers).to.have.property('provideValidity', 60 * 10)
})
})

describe('start and stop', () => {
Expand Down

0 comments on commit e64af85

Please sign in to comment.