Skip to content

Commit

Permalink
feat: add more configuration options (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Sep 20, 2021
1 parent a6d24de commit ff67106
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ export const GossipsubMaxIHaveMessages = 10
*/
export const GossipsubIWantFollowupTime = 3 * second

/**
* Time in milliseconds to keep message ids in the seen cache
*/
export const GossipsubSeenTTL = 30 * second

export const TimeCacheDuration = 120 * 1000

export const ERR_TOPIC_VALIDATOR_REJECT = 'ERR_TOPIC_VALIDATOR_REJECT'
Expand Down
7 changes: 4 additions & 3 deletions ts/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Heartbeat {

const timeout = setTimeout(() => {
heartbeat()
this._heartbeatTimer!.runPeriodically(heartbeat, constants.GossipsubHeartbeatInterval)
this._heartbeatTimer!.runPeriodically(heartbeat, this.gossipsub._options.heartbeatInterval)
}, constants.GossipsubHeartbeatInitialDelay)

this._heartbeatTimer = {
Expand Down Expand Up @@ -69,7 +69,8 @@ export class Heartbeat {
Dlo,
Dhi,
Dscore,
Dout
Dout,
fanoutTTL
} = this.gossipsub._options
this.gossipsub.heartbeatTicks++

Expand Down Expand Up @@ -285,7 +286,7 @@ export class Heartbeat {
// expire fanout for topics we haven't published to in a while
const now = this.gossipsub._now()
this.gossipsub.lastpub.forEach((lastpb, topic) => {
if ((lastpb + constants.GossipsubFanoutTTL) < now) {
if ((lastpb + fanoutTTL) < now) {
this.gossipsub.fanout.delete(topic)
this.gossipsub.lastpub.delete(topic)
}
Expand Down
31 changes: 29 additions & 2 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ interface GossipInputOptions {
* Dlazy affects how many peers we will emit gossip to at each heartbeat.
*/
Dlazy: number
/**
* heartbeatInterval is the time between heartbeats in milliseconds
*/
heartbeatInterval: number
/**
* fanoutTTL controls how long we keep track of the fanout state. If it's been
* fanoutTTL milliseconds since we've published to a topic that we're not subscribed to,
* we'll delete the fanout map for that topic.
*/
fanoutTTL: number
/**
* mcacheLength is the number of windows to retain full messages for IWANT responses
*/
mcacheLength: number
/**
* mcacheGossip is the number of windows to gossip about
*/
mcacheGossip: number
/**
* seenTTL is the number of milliseconds to retain message IDs in the seen cache
*/
seenTTL: number
}

interface GossipOptions extends GossipInputOptions {
Expand Down Expand Up @@ -132,6 +154,11 @@ class Gossipsub extends Pubsub {
Dscore: constants.GossipsubDscore,
Dout: constants.GossipsubDout,
Dlazy: constants.GossipsubDlazy,
heartbeatInterval: constants.GossipsubHeartbeatInterval,
fanoutTTL: constants.GossipsubFanoutTTL,
mcacheLength: constants.GossipsubHistoryLength,
mcacheGossip: constants.GossipsubHistoryGossip,
seenTTL: constants.GossipsubSeenTTL,
...options,
scoreParams: createPeerScoreParams(options.scoreParams),
scoreThresholds: createPeerScoreThresholds(options.scoreThresholds)
Expand Down Expand Up @@ -167,7 +194,7 @@ class Gossipsub extends Pubsub {
*
* @type {TimeCache}
*/
this.seenCache = new TimeCache()
this.seenCache = new TimeCache({ validity: opts.seenTTL / 1000 })

/**
* Map of topic meshes
Expand Down Expand Up @@ -238,7 +265,7 @@ class Gossipsub extends Pubsub {
* A message cache that contains the messages for last few hearbeat ticks
*
*/
this.messageCache = options.messageCache || new MessageCache(constants.GossipsubHistoryGossip, constants.GossipsubHistoryLength, this.getMsgId.bind(this))
this.messageCache = options.messageCache || new MessageCache(opts.mcacheGossip, opts.mcacheLength, this.getMsgId.bind(this))

/**
* A heartbeat timer that maintains the mesh
Expand Down

0 comments on commit ff67106

Please sign in to comment.