This repository was archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Fix/dial class #203
Merged
Merged
Fix/dial class #203
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
413f7e5
wip
daviddias 2cc2185
y kill?
daviddias 5f1b223
fix: do not reuse queues per transport
daviddias e8a2226
implement dialer limiting properly
dignifiedquire 8babbfb
test: do not reuse dialing peers
dignifiedquire 1b28337
refactor: be less like Java
dignifiedquire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict' | ||
|
||
const map = require('async/map') | ||
const debug = require('debug') | ||
|
||
const log = debug('libp2p:swarm:dialer') | ||
|
||
const DialQueue = require('./queue') | ||
|
||
/** | ||
* Track dials per peer and limited them. | ||
*/ | ||
class LimitDialer { | ||
/** | ||
* Create a new dialer. | ||
* | ||
* @param {number} perPeerLimit | ||
* @param {number} dialTimeout | ||
*/ | ||
constructor (perPeerLimit, dialTimeout) { | ||
log('create: %s peer limit, %s dial timeout', perPeerLimit, dialTimeout) | ||
this.perPeerLimit = perPeerLimit | ||
this.dialTimeout = dialTimeout | ||
this.queues = new Map() | ||
} | ||
|
||
/** | ||
* Dial a list of multiaddrs on the given transport. | ||
* | ||
* @param {PeerId} peer | ||
* @param {SwarmTransport} transport | ||
* @param {Array<Multiaddr>} addrs | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
*/ | ||
dialMany (peer, transport, addrs, callback) { | ||
log('dialMany:start') | ||
// we use a token to track if we want to cancel following dials | ||
const token = {cancel: false} | ||
map(addrs, (m, cb) => { | ||
this.dialSingle(peer, transport, m, token, cb) | ||
}, (err, results) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
const success = results.filter((res) => res.conn) | ||
if (success.length > 0) { | ||
log('dialMany:success') | ||
return callback(null, success[0].conn) | ||
} | ||
|
||
log('dialMany:error') | ||
const error = new Error('Failed to dial any provided address') | ||
error.errors = results | ||
.filter((res) => res.error) | ||
.map((res) => res.error) | ||
return callback(error) | ||
}) | ||
} | ||
|
||
/** | ||
* Dial a single multiaddr on the given transport. | ||
* | ||
* @param {PeerId} peer | ||
* @param {SwarmTransport} transport | ||
* @param {Multiaddr} addr | ||
* @param {CancelToken} token | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
*/ | ||
dialSingle (peer, transport, addr, token, callback) { | ||
const ps = peer.toB58String() | ||
log('dialSingle: %s:%s', ps, addr.toString()) | ||
let q | ||
if (this.queues.has(ps)) { | ||
q = this.queues.get(ps) | ||
} else { | ||
q = new DialQueue(this.perPeerLimit, this.dialTimeout) | ||
this.queues.set(ps, q) | ||
} | ||
|
||
q.push(transport, addr, token, callback) | ||
} | ||
} | ||
|
||
module.exports = LimitDialer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
|
||
const Connection = require('interface-connection').Connection | ||
const pull = require('pull-stream') | ||
const timeout = require('async/timeout') | ||
const queue = require('async/queue') | ||
const debug = require('debug') | ||
|
||
const log = debug('libp2p:swarm:dialer:queue') | ||
|
||
/** | ||
* Queue up the amount of dials to a given peer. | ||
*/ | ||
class DialQueue { | ||
/** | ||
* Create a new dial queue. | ||
* | ||
* @param {number} limit | ||
* @param {number} dialTimeout | ||
*/ | ||
constructor (limit, dialTimeout) { | ||
this.dialTimeout = dialTimeout | ||
|
||
this.queue = queue((task, cb) => { | ||
this._doWork(task.transport, task.addr, task.token, cb) | ||
}, limit) | ||
} | ||
|
||
/** | ||
* The actual work done by the queue. | ||
* | ||
* @param {SwarmTransport} transport | ||
* @param {Multiaddr} addr | ||
* @param {CancelToken} token | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
* @private | ||
*/ | ||
_doWork (transport, addr, token, callback) { | ||
log('work') | ||
this._dialWithTimeout( | ||
transport, | ||
addr, | ||
(err, conn) => { | ||
if (err) { | ||
log('work:error') | ||
return callback(null, {error: err}) | ||
} | ||
|
||
if (token.cancel) { | ||
log('work:cancel') | ||
// clean up already done dials | ||
pull(pull.empty(), conn) | ||
// TODO: proper cleanup once the connection interface supports it | ||
// return conn.close(() => callback(new Error('Manual cancel')) | ||
return callback(null, {cancel: true}) | ||
} | ||
|
||
// one is enough | ||
token.cancel = true | ||
|
||
log('work:success') | ||
|
||
const proxyConn = new Connection() | ||
proxyConn.setInnerConn(conn) | ||
callback(null, {conn}) | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Dial the given transport, timing out with the set timeout. | ||
* | ||
* @param {SwarmTransport} transport | ||
* @param {Multiaddr} addr | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
* | ||
* @private | ||
*/ | ||
_dialWithTimeout (transport, addr, callback) { | ||
timeout((cb) => { | ||
const conn = transport.dial(addr, (err) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, conn) | ||
}) | ||
}, this.dialTimeout)(callback) | ||
} | ||
|
||
/** | ||
* Add new work to the queue. | ||
* | ||
* @param {SwarmTransport} transport | ||
* @param {Multiaddr} addr | ||
* @param {CancelToken} token | ||
* @param {function(Error, Connection)} callback | ||
* @returns {void} | ||
*/ | ||
push (transport, addr, token, callback) { | ||
this.queue.push({transport, addr, token}, callback) | ||
} | ||
} | ||
|
||
module.exports = DialQueue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I intentionally had made
swarm.transport.dial
be just multiaddr aware for simplicity, and so, it would only receive a multiaddr and not a peerInfoswarm.transport.dial -> deals with dialing on a transport
swarm.dial -> does all the magic of upgrading the connection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of a breaking change on the API, not a biggie because it is more of an internal that is just exposed for testing, however, I'm not sure if I see the advantage, could you clarify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am doing the same thing that go does, that is limiting dials per peer. To do that I need to know the peerId otherwise I can't track dials per peer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can keep it.