Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

refactor: async it #153

Merged
merged 3 commits into from
Nov 26, 2019
Merged

refactor: async it #153

merged 3 commits into from
Nov 26, 2019

Conversation

vasco-santos
Copy link
Member

@vasco-santos vasco-santos commented Nov 20, 2019

This PR aims to use async iterators instead of pull-streams in the context of the asyn migration, as a follow up for #148

@vasco-santos vasco-santos force-pushed the refactor/async-iterators branch 13 times, most recently from efece9f to 0fc408a Compare November 21, 2019 20:08
"libp2p-mplex": "~0.8.5",
"libp2p-switch": "~0.42.7",
"libp2p-tcp": "~0.13.0",
"it-pair": "^1.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create an issue to track the removal of peer-book for tests, as well as handle the peer-store operations currently done here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async can be removed from the dependencies now. And chai-checkmark needs to be a dev dep.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still use async/queue for the query WorkerQueue. I will create an issue to track that

@@ -100,10 +96,10 @@ describe('multiple nodes', () => {
await dhts[4].put(key, Buffer.from('world4'))

const res = await Promise.all([
dhts[3].get(key, { maxTimeout: 2000 }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont't know as this was passing before.

It was getting stored locally world3 (after put), which was the first result obtained from getMany. Then, we use the selector: () => 0 and the first record was obtained

@vasco-santos vasco-santos marked this pull request as ready for review November 21, 2019 21:28
@vasco-santos vasco-santos mentioned this pull request Nov 21, 2019
1 task
Copy link
Contributor

@dirkmc dirkmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just left a couple of nits and suggestions

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/network.js Show resolved Hide resolved
),
lp.decode(),
async source => {
for await (const chunk of source) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth adding a dependency just for this, but the streaming iterables library has some useful functions, eg this could be replaced with a map and a collect

src/rpc/index.js Outdated
* @param {PeerInfo} peer
* @param {Message} msg
* @returns {Promise<Message>}
*
* @private
*/
async function handleMessage (peer, msg) {
async function handleMessage (peer, msg) { // eslint-disable-line
// get handler & exectue it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// get handler & exectue it
// get handler & execute it

src/rpc/index.js Outdated Show resolved Hide resolved
@@ -11,6 +11,16 @@ const { Record } = require('libp2p-record')
const PeerId = require('peer-id')
const errcode = require('err-code')

exports.itFilter = function (predicate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"libp2p-mplex": "~0.8.5",
"libp2p-switch": "~0.42.7",
"libp2p-tcp": "~0.13.0",
"it-pair": "^1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async can be removed from the dependencies now. And chai-checkmark needs to be a dev dep.

src/network.js Outdated Show resolved Hide resolved
src/network.js Outdated Show resolved Hide resolved
src/network.js Outdated
await this.dht._add(peer)
this._log('added to the routing table: %s', peer.id.toB58String())
// Open a stream with the connected peer
await conn.newStream(c.PROTOCOL_DHT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary now, correct? We previously dialed the protocol to check support, but we already have that information now. We're not performing an explicit action, so we shouldn't create a stream.

src/network.js Outdated
@@ -151,10 +154,10 @@ class Network {
*
* @param {Connection} conn - the connection to use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few places in the jsdocs that inaccurately reference Connection. We should make sure these are duplex iterables.

_peerInfo: ourPeerInfo,
_peerBook: new PeerBook()
}
_peerBook: peerStore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dialer._peerBook and dialer._peerInfo should be deleted, they're not properties of the dialer.

@@ -87,6 +87,7 @@ describe('rpc - handlers - GetValue', () => {

const msg = new Message(T, key, 0)

dht.peerStore.put(other)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this need to get added?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the code we try to find the peer in the peer-store. After this test we have another one for unkwown peers

data.push(chunk.slice())
}
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use collect from streaming-iterables I think this code can be simplified a bit (as well as some of the other places we are doing collects here. Also, there are multiple slices of the BufferList happening in this stream, it's not needed. You could also use the it-buffer module and simplify all of this to be:

    const data = await pipe(
      [msg.serialize()],
      lp.encode(),
      collect
    )

    const duplexStream = {
      source: data,
      sink: async (source) => {
        const res = await pipe(
          source,
          lp.decode(),
          toBuffer,  // Ensure we have buffers here for validateMessage to consume
          collect
        )
        validateMessage(res)
      }
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yap, cool tip!

test/utils/index.js Outdated Show resolved Hide resolved
test/utils/index.js Outdated Show resolved Hide resolved
@vasco-santos vasco-santos force-pushed the refactor/async-iterators branch 2 times, most recently from a10506e to 5ad4af7 Compare November 22, 2019 21:27
Co-Authored-By: dirkmc <dirkmdev@gmail.com>
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
Copy link
Contributor

@jacobheun jacobheun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! 🚢 🚀

@vasco-santos vasco-santos merged commit dc824d9 into master Nov 26, 2019
@vasco-santos vasco-santos deleted the refactor/async-iterators branch November 26, 2019 12:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants