This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic dial queue to avoid many connections to peer (#310)
BREAKING CHANGE: This adds a very basic dial queue peer peer. This will prevent multiple, simultaneous dial requests to the same peer from creating multiple connections. The requests will be queued per peer, and will leverage the same connection when possible. The breaking change here is that `.dial`, will no longer return a connection. js-libp2p, circuit relay, and kad-dht, which use `.dial` were not using the returned connection. So while this is a breaking change it should not break the existing libp2p stack. If custom applications are leveraging the returned connection, they will need to convert to only using the connection returned via the callback. * chore: dont log priviatized unless it actually happened * refactor: only get our addresses for filtering once
- Loading branch information
Showing
18 changed files
with
537 additions
and
236 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,68 @@ | ||
'use strict' | ||
|
||
const DialQueueManager = require('./queueManager') | ||
const getPeerInfo = require('../get-peer-info') | ||
|
||
module.exports = function (_switch) { | ||
const dialQueueManager = new DialQueueManager(_switch) | ||
|
||
_switch.state.on('STOPPING:enter', abort) | ||
|
||
/** | ||
* @param {DialRequest} dialRequest | ||
* @returns {void} | ||
*/ | ||
function _dial ({ peerInfo, protocol, useFSM, callback }) { | ||
if (typeof protocol === 'function') { | ||
callback = protocol | ||
protocol = null | ||
} | ||
|
||
try { | ||
peerInfo = getPeerInfo(peerInfo, _switch._peerBook) | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
|
||
// Add it to the queue, it will automatically get executed | ||
dialQueueManager.add({ peerInfo, protocol, useFSM, callback }) | ||
} | ||
|
||
/** | ||
* Aborts all dials that are queued. This should | ||
* only be used when the Switch is being stopped | ||
* | ||
* @param {function} callback | ||
*/ | ||
function abort (callback) { | ||
dialQueueManager.abort() | ||
callback() | ||
} | ||
|
||
/** | ||
* Adds the dial request to the queue for the given `peerInfo` | ||
* @param {PeerInfo} peerInfo | ||
* @param {string} protocol | ||
* @param {function(Error, Connection)} callback | ||
*/ | ||
function dial (peerInfo, protocol, callback) { | ||
_dial({ peerInfo, protocol, useFSM: false, callback }) | ||
} | ||
|
||
/** | ||
* Behaves like dial, except it calls back with a ConnectionFSM | ||
* | ||
* @param {PeerInfo} peerInfo | ||
* @param {string} protocol | ||
* @param {function(Error, ConnectionFSM)} callback | ||
*/ | ||
function dialFSM (peerInfo, protocol, callback) { | ||
_dial({ peerInfo, protocol, useFSM: true, callback }) | ||
} | ||
|
||
return { | ||
dial, | ||
dialFSM, | ||
abort | ||
} | ||
} |
Oops, something went wrong.