Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: UDP direct hole punching, high CPU usage and misc. #130

Closed
wants to merge 4 commits into from

Conversation

@robert-cronin robert-cronin added bug Something isn't working enhancement New feature or request development Standard development cleanup labels Nov 12, 2020
@robert-cronin robert-cronin self-assigned this Nov 12, 2020
@robert-cronin
Copy link
Contributor Author

robert-cronin commented Nov 24, 2020

After some testing with flamegraph, it turned out swagger-ui and a few loops were the cause of the idle CPU usage. The loops include multicast broadcasting and the peerDHT trying to connect to unconnected peers. The latter looks to be the biggest source of inefficiency so I think it should initially try to connect to those peers and if its not able to after maybe 3 tries, it stops trying unless the user tries to connect to it. The inspiration for this was libp2p's 'random walk' which allows a peer to maintain its position in the DHT by randomly querying peers every now and again.

Another optimisation might be to increase the interval time

@robert-cronin
Copy link
Contributor Author

This is with swagger-ui:
image
This is without:
image

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Nov 24, 2020 via email

@CMCDragonkai
Copy link
Member

Will this PR also fix the high CPU usage? I didn't see an issue for that.

@CMCDragonkai
Copy link
Member

Can you update the title of this PR since it seems to address alot more than UDP hole punching.

@CMCDragonkai
Copy link
Member

BTW, high cpu usage when idling is critical to mobile deployment. @gideonairex

We need ways of hooking in events that occur on mobile like 4g vs wifi, battery vs powered... etc.

These would impact any background processing or discovery processes.

CPU usage for js-polykey is really important to optimize!

@CMCDragonkai
Copy link
Member

If @gideonairex has experience here, maybe he can help?

@robert-cronin robert-cronin changed the title Udp direct hole punch Fix: UDP direct hole punching and high CPU usage Nov 27, 2020
@robert-cronin robert-cronin changed the title Fix: UDP direct hole punching and high CPU usage Fix: UDP direct hole punching, high CPU usage and misc. Nov 27, 2020
@robert-cronin
Copy link
Contributor Author

robert-cronin commented Nov 27, 2020

Updated title to the two major things being addressed by this PR.
As for the events on mobile, there are some plugins we can use in nativescript:

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Nov 27, 2020

Cool so the plugins will be useful to trigger those events, but then js-polykey needs the hooks as "governor" to govern all of its loops so that the user which is Electron Polykey or Native Script Polykey can know how to optimize the usage of js-polykey.

By "governor" I mean this:

https://en.wikipedia.org/wiki/Governor_(device)

@CMCDragonkai
Copy link
Member

@robert-cronin ETA for this PR?

@CMCDragonkai
Copy link
Member

@gideonairex you should track this PR as well, looks like some things coming here will result in features in the frontend.

@robert-cronin
Copy link
Contributor Author

@robert-cronin ETA for this PR?

I am hoping to be finished by Friday, still need to schedule a meeting with @nzhang-zh to ensure the fixes to the hole punching are working correctly

@robert-cronin
Copy link
Contributor Author

robert-cronin commented Dec 16, 2020

Went a bit over schedule, the UDP hole punching was a little more complicated than anticipated but is finally working over the public internet. To keep the code review more manageable, I will be breaking this PR up so that the already solved issues can be reviewed and merged. Issues #113, #128 and #131 will be split into their own PR for social discoverey and other UI/UX issues

@CMCDragonkai
Copy link
Member

@gideonairex Can you cross review this with @robert-cronin.

I will need you guys to review each others code in accordance with coding guidelines in the orientation repository as well and make discussions available here too.

I won't always have time to review all the code.

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Dec 16, 2020

@robert-cronin Make sure to get lint and lintfix working as well to ensure that all code is formatted.

See: https://gitlab.com/MatrixAI/open-source/js-polykey/-/pipelines/229949699

It's currently failing the build.

Also squash the commits into semantic commits.

@CMCDragonkai
Copy link
Member

BTW are the tests intended to work via the CI? Seems like it times out.

@robert-cronin
Copy link
Contributor Author

There is a discrepancy between the gitlab-runner on my local machine and the one actually on gitlab and I haven't managed to figure out why yet. I can have a look soon in the next PR hopefully.

@robert-cronin
Copy link
Contributor Author

robert-cronin commented Dec 16, 2020

So in the cleanup for this PR, I remembered a bug in the connection logic that I forgot to deal with; if the direct connection tries and fails (in the case of public to private connections) then the rest of the methods cannot be performed, I was getting around the bug before by commenting out the direct connection method so it would go directly to the hole punching.

The bug seems to be something to do with my custom promiseAny utility, it almost seems to be blocking the event loop for 10 seconds which is the default timeout set for connectDirectly method. I have a couple of things to try; first is the try to increase the timeout for the connectFirstChannel method so all options can be tried or reduce the timeout for the connectDirectly method so the other methods can have their try. Even though Node.js is async/await, the call stack is single threaded so it still has to empty before the event loop can pop the next task onto it (unless we use webworkers, but that seems a little overkill).

@CMCDragonkai
Copy link
Member

@robert-cronin is this still relevant?

@CMCDragonkai
Copy link
Member

Development occurs on gitlab now.

@CMCDragonkai CMCDragonkai deleted the udp-direct-hole-punch branch March 17, 2021 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment