Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"protons": "^2.0.0",
"retimer": "^2.0.0",
"sanitize-filename": "^1.6.3",
"set-delayed-interval": "^1.0.0",
"streaming-iterables": "^5.0.2",
"timeout-abort-controller": "^1.1.1",
"varint": "^5.0.0",
Expand Down
22 changes: 11 additions & 11 deletions src/circuit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const debug = require('debug')
const log = debug('libp2p:relay')
log.error = debug('libp2p:relay:error')

const {
setDelayedInterval,
clearDelayedInterval
} = require('set-delayed-interval')

const AutoRelay = require('./auto-relay')
const { namespaceToCid } = require('./utils')
const {
Expand Down Expand Up @@ -33,6 +38,8 @@ class Relay {

// Create autoRelay if enabled
this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay })

this._advertiseService = this._advertiseService.bind(this)
}

/**
Expand All @@ -45,9 +52,9 @@ class Relay {
const canHop = this._options.hop.enabled

if (canHop && this._options.advertise.enabled) {
this._timeout = setTimeout(() => {
this._advertiseService()
}, this._options.advertise.bootDelay)
this._timeout = setDelayedInterval(
this._advertiseService, this._options.advertise.ttl, this._options.advertise.bootDelay
)
}
}

Expand All @@ -57,7 +64,7 @@ class Relay {
* @returns {void}
*/
stop () {
clearTimeout(this._timeout)
clearDelayedInterval(this._timeout)
}

/**
Expand All @@ -77,14 +84,7 @@ class Relay {
} else {
log.error(err)
}

return
}

// Restart timeout
this._timeout = setTimeout(() => {
this._advertiseService()
}, this._options.advertise.ttl)
}
}

Expand Down