Skip to content
This repository has been archived by the owner on Oct 9, 2021. It is now read-only.

Reuse TCP connection / keep-alive #210

Open
muety opened this issue Apr 26, 2020 · 6 comments
Open

Reuse TCP connection / keep-alive #210

muety opened this issue Apr 26, 2020 · 6 comments

Comments

@muety
Copy link

muety commented Apr 26, 2020

I might be totally wrong, but to me it seems like Wakatime does not reuse an existing TCP connection in consecutive requests. Take a look at this Wireshark screenshot.

Screenshot from 2020-04-26 20-00-00

What you can see is three consecutive requests (I changed tabs in VSCode three times) and apparently, new TCP- and TLS handshakes are performed each time.

Please correct me if I got something wrong. But if not, wouldn't it be worth to reuse the connection for efficiency?

@muety
Copy link
Author

muety commented Aug 19, 2020

I figured out the problem. The cli is using requests.Session, which is generally good. Sessions are made to (1) keep request parameters among multiple requests and (2) to keep the underlying TCP connection alive for consecutive requests. SessionCache serializes Session objects to SQLite and reads them back for every request that is made. This is useful to keep internal attributes of the session class. However, as a new object is instantiated every time, the TCP connection is closed once the old session runs out of scope.

I'd like to fix this by extending the SessionCache cache to hold a reference to the current open session in memory and change the api module to hold a SessionManager "singleton" instead of instantiating a new one for every request.

Are you open for PRs, @alanhamlett ?

@alanhamlett
Copy link
Member

hold a SessionManager "singleton" instead of instantiating a new one for every request

Would that singleton be shared across multiple Python processes?

@alanhamlett
Copy link
Member

alanhamlett commented Aug 20, 2020

Are you open for PRs?

Yes, for sure. However we're also working on porting this Python code into Go so it might be better worth the time to fix it in the new repo: https://github.com/wakatime/wakatime-cli

@muety
Copy link
Author

muety commented Aug 20, 2020

Would that singleton be shared across multiple Python processes?

That's a good point. If I think about it, my above mentioned approach would only help to reuse the connection across multiple calls to send_heartbeats() within the same process, i.e. when sending multiple batches of old heartbeats. It's probably not worth the effort then.

we're also working on porting this Python code into Go

Very cool! I assume the new version is going to be a cli as well? An alternative would be to have a long-running daemon process that listens on local TCP/IP connections or so. But that would require to adapt all plugins and probably comes with some disadvantages on its own.

@muety
Copy link
Author

muety commented Oct 9, 2020

What has happened to wakatime/wakatime-cli ? 🤔

@gandarezwarren
Copy link

What has happened to wakatime/wakatime-cli ? 🤔

It's private now because we're not ready and still redesign the entire cli. Will reopen as soon as possible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants