libwebsockets icon indicating copy to clipboard operation
libwebsockets copied to clipboard

0.3 BTC for fixing obviously broken libwebsockets client

Open deforation opened this issue 6 years ago • 15 comments

With this issue, i provide a bounty of 0.3 BTC to fix the following libwebsockets client issue. Issue: 2015

Problem: Libwebsockets performs worse compared to ordinary python libraries. It might be, that the root cause is openssl or something else, but they fall indirectly under this libwebsockets umbrella.

Task:

  1. Crate a fix for the issue Nr. 2015. So that libwebsockets will perform equel or better to the python counterpart.
  2. Give an explanation, what the issue was and which component is at fault.

Guidlines:

  1. The fix must work under ubuntu 18.04
  2. The fix should integrate nicely into libwebsockets (obviously)
  3. The fix should primarily use openssl and if not possible with that (wolfssl)
  4. The fix should use one the integrated eventloops such as (libev, libuv, libevent or the default poll)
  5. Me or im sure, @kutoga will try the fix for stability with other endpoints that have this issue.

Bounty: I Provide a bounty of 0.3 BTC to the one who finds and fixes the issue.

Why should i trust you? Good question. Don't trust anyone in the internet. The only thing i can offer is the following:

  1. You search some sort of trustworthy escrow service/counterparty, which makes the final transaction.
  2. If @lws-team offers to take this role, i will send it to him and he can Release it.
  3. If @lws-team agrees to point 2 and he fixes him self, nice, just keep it for this otherwise reall nice library.
  4. You trust me, that i send it.

Thanks for your cooperation. And i hope @lws-team that you don't take it personally. Its all about improving the library and the parts within its umbrella (according to lws-overview)

deforation avatar Aug 22 '20 15:08 deforation

Great idea! I add 1 ETH to the "pot":)!

kutoga avatar Aug 22 '20 15:08 kutoga

I don't want anything to do with the proposed transaction... if somebody wants to take you up on it, that's between you and them... OTOH useful patches are always welcome from anyone, if they are getting paid to produce them that's not a bad thing in itself.

But neither lws nor I endorses the offer, or had even heard of you guys before today AFAIK, so if you spend time on it do it at your own risk.

lws-team avatar Aug 22 '20 17:08 lws-team

@lws-team Thanks for your response. I'm sure we will find another way if someone wants to take the offer/has a solution.

Obviously, i will try to track this issue down as well and i hope the same applies to @kutoga (which i thank for the contribution).

I really like this piece of software. But to be honest, this one case (even though it might seem special), makes it for some use cases unusable. That's why i would like to see this fixed.

deforation avatar Aug 22 '20 17:08 deforation

That's why i would like to see this fixed.

#2015 is all about how nobody did the work yet to determine what 'this' is... I guess we will see if throwing money at something instead of making sure you understand it gives useful results.

lws-team avatar Aug 22 '20 17:08 lws-team

Lets see. If it works out, its a win-win situation for everyone. One thing i don't really understand is this "nobody did the work".

What i understood is, that @kutoga tried:

  1. libuv, libev, openssl, wolfssl, fedora, some other operating system
  2. all had the same issue

If one just looks at this information, there are basically the following possibilities: a) The user uses the library wrong. b) The network connectivity has issues c) The server has issues d) The websocket library has issues e) Something completely different

  • Please correct me, if i missed something.

Now if we take into account that one other library performs better and another worse, we can most likely assume that Options b and c are not the case.

As a result, it is valid in order to invalidate option a, by asking experienced users which did not had this issue yet, if they can reproduce it.

In order to invalidate option d, it makes sense to ask for some advice of someone who knows the library very well, what a good approach would be to figure out if d is really the case or maybe something completely unrelated like the OS or Computer (option e).

And in my opinion, by opening that issue, that's what the logical next step was.

deforation avatar Aug 22 '20 17:08 deforation

I have put an improved version of kutoga's example (itself based on minimal-ws-client) in its own minimal example.

tldr is the server side acts differently as the number of messages per seconds increases, it starts to withhold and coalesce messages into larger tls records. The earlier messages especially in these coalesced tls records are made to wait until the server feels he has coalesced enough and issues the record. You can see more details in the readme here

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-client/minimal-ws-client-binance

We can't directly control what the server-side architecture wants to do, but we can indirectly affect it actually, by enabling permessage-deflate compression on the ws connection. This reduces the size of the outgoing JSON messages by a factor of 2 or 3 or so and changes the coalescing behaviour on the server side so it isn't triggered so readily. Notice that you need a non-default lws option for this, -DLWS_WITHOUT_EXTENSIONS=0. I guess this was the difference with python or not.

I also added a client flag that makes lws prioritize clearing any pending rx, normally it has to take care that everybody gets a turn sending and receiving fairly, but here you just care about latency. This doesn't help unless things got delayed and all sent at once, but then it will help clear them rapidly at the cost of momentarily blocking other lws connections.

There have been several people wanting to use lws to talk to binance, I think the example will save me time in the end providing a best practice based way to handle it.

Whether that fixes what you observed you have to try it and see, I never saw these 20s spikes but it was off and on delaying things by up to 4s, it may just be related to the time of day or that I am looking at it on a Sunday. Either way for reasons I won't bore you with taking your money will just create problems for me. So just enjoy these improvements to the extent they improve things.

lws-team avatar Aug 23 '20 10:08 lws-team

Thanks for doing some analysis of the problem. I would have spent way more time and I'm not sure if I could have solved it. Unfortunately, I'm not in an office, so I only could do a very quick test with the code you provided (with mobile internet). This first quick test already showed stable latencies. Tomorrow, I'll have to do more sophisticated (stress) tests.

Also the example you provided is well done. The use functions like lws_json_simple_find cleary makes the example more stable and readable than the code I've posted. I also think it will help many people out there, because probably quite some have the same issue. Thanks for the effort and the detailed description of the solution!

As soon as I've done more tests, I'll write a comment about the results to this thread / issue.

Thanks, and have a nice Sunday:)!

  • EDIT - I need one more day for the results.

kutoga avatar Aug 23 '20 12:08 kutoga

I thank you @lws-team . It solved the issues i had as well.

I don't know if i would have been able to come up with this solution. You said, you will not take the offer, but i promised one. So please let me know (does not need to be now), where i should send it to. The amount is reserved.

Thanks again for your effort and time.

deforation avatar Aug 24 '20 14:08 deforation

I've done many tests and it really seems the issue is gone. I did not only test binance, but also some other endpoints. Here just a plot for binance with the "old" configuration and the new one (also wolfssl vs openssl). Looks pretty good now: plot

Thanks for the help:)

@lws-team If you want to have the 1 ETH, I'll send it you

kutoga avatar Aug 25 '20 14:08 kutoga

IIUI the coalescing behaviour is decided by the server side before it encrypts and sends the tls record. We just tell it during ws negotiation to compress the ws messages before the encryption serverside, and that seems to cause it to act differently... still serverside.

I guess if it gets busy enough serverside to produce the same amount of compressed data as when it made the symptom, it will start to show the old behaviour again.

I appreciate the offers but I will decline thanks.

lws-team avatar Aug 25 '20 15:08 lws-team

I've done many tests and it really seems the issue is gone. I did not only test binance, but also some other endpoints. Here just a plot for binance with the "old" configuration and the new one (also wolfssl vs openssl). Looks pretty good now: plot

Thanks for the help:)

@lws-team If you want to have the 1 ETH, I'll send it you

how did you do it?

alex09x avatar Aug 26 '20 00:08 alex09x

The "new" code was embedded into an existing application which then was used to measure the book age (=latency). Then it basically just exported this book age. I've embedded the code into this application, because it allowed me to do some more sophisticated testing. I'm still working on this.

kutoga avatar Aug 26 '20 06:08 kutoga

The "new" code was embedded into an existing application which then was used to measure the book age (=latency). Then it basically just exported this book age. I've embedded the code into this application, because it allowed me to do some more sophisticated testing. I'm still working on this.

show please example binance_wolfssl

alex09x avatar Aug 26 '20 17:08 alex09x

Unfortunately, I cannot post the complete code, but it is basically the new binance example compiled with wolfssl.

kutoga avatar Aug 26 '20 17:08 kutoga

The "new" code was embedded into an existing application which then was used to measure the book age (=latency). Then it basically just exported this book age. I've embedded the code into this application, because it allowed me to do some more sophisticated testing. I'm still working on this.

show please example binance_wolfssl

https://github.com/mussonero/binance-cxx-api-v2

mussonero avatar Oct 20 '21 08:10 mussonero