1# lws minimal ws client binance 2 3This connects to the binance ws server and monitors transactions with 4an eye on low latency. 5 6Latency seems to be associated with server-side coalescing at tls 7layer, and the coalescing at server side seems somewhat correlated to number 8of transactions per second, which seems to cause increased packet sizes from the 9server as a reaction. The relationship is more complex probably according to what 10actually happens at the server backend, but it seems to be broadly related 11reliably. 12 13Typically when showing low latency at ~70msg/s, the messages on the wire are 14eg, ~70 byte packets containing small tls records 15 1610:14:40.682293 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42952: Flags [P.], seq 50846:50927, ack 1, win 11, options [nop,nop,TS val 366445630 ecr 3893437035], length 81 17 18under pressure from increased messages per second, the tls records increase above 2KB 19 2008:06:02.825160 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42688: Flags [.], seq 512319:513643, ack 1, win 11, options [nop,nop,TS val 3990208942 ecr 3885719233], length 1324 2108:06:02.825290 IP constance.42688 > ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https: Flags [.], ack 513643, win 14248, options [nop,nop,TS val 3885719479 ecr 3990208942], length 0 2208:06:02.891646 IP ec2-54-249-113-172.ap-northeast-1.compute.amazonaws.com.https > constance.42688: Flags [.], seq 513643:516291, ack 1, win 11, options [nop,nop,TS val 3990209006 ecr 3885719296], length 2648 23 24The larger the packets, the longer the first item in the packet had to 25wait before it was sent, and a tls record cannot be authenticated until 26all of it has been received. 27 28The example circumvents this somewhat by using `permessage_deflate`, which reduces 29the packet size before tls by applying compression, making even coalesced packets 30smaller, and a new option for adjusting how lws manages conflicting requirements to 31clear pending rx and allow interleaved tx, `LCCSCF_PRIORITIZE_READS` that causes the 32stream to prioritize handling any pending rx, not just pending at ssl layer, in one 33event loop trip. 34 35## build 36 37Lws must have been built with `LWS_ROLE_WS=1` and `LWS_WITHOUT_EXTENSIONS=0` 38 39``` 40 $ cmake . && make 41``` 42 43## Commandline Options 44 45Option|Meaning 46---|--- 47-d|Set logging verbosity 48 49## usage 50 51``` 52$ ./bin/lws-minimal-ws-client-binance 53[2020/08/23 10:22:49:3003] U: LWS minimal binance client 54[2020/08/23 10:22:49:3005] N: LWS: 4.0.99-v4.1.0-rc2-4-g3cf133aef, loglevel 1031 55[2020/08/23 10:22:49:3005] N: NET CLI SRV H1 H2 WS MQTT SS-JSON-POL SSPROX ASYNC_DNS IPv6-absent 56[2020/08/23 10:22:50:8243] N: checking client ext permessage-deflate 57[2020/08/23 10:22:50:8244] N: instantiating client ext permessage-deflate 58[2020/08/23 10:22:50:8244] U: callback_minimal: established 59[2020/08/23 10:22:51:8244] N: sul_hz_cb: price: min: 1160284¢, max: 1163794¢, avg: 1160516¢, (150 prices/s) 60[2020/08/23 10:22:51:8245] N: sul_hz_cb: elatency: min: 112ms, max: 547ms, avg: 259ms, (155 msg/s) 61[2020/08/23 10:22:52:8244] N: sul_hz_cb: price: min: 1160287¢, max: 1178845¢, avg: 1160897¢, (112 prices/s) 62[2020/08/23 10:22:52:8245] N: sul_hz_cb: elatency: min: 111ms, max: 226ms, avg: 152ms, (134 msg/s) 63[2020/08/23 10:22:53:8247] N: sul_hz_cb: price: min: 1160287¢, max: 1168005¢, avg: 1160806¢, (86 prices/s) 64[2020/08/23 10:22:53:8248] N: sul_hz_cb: elatency: min: 112ms, max: 476ms, avg: 287ms, (101 msg/s) 65[2020/08/23 10:22:54:8247] N: sul_hz_cb: price: min: 1160284¢, max: 1162780¢, avg: 1160698¢, (71 prices/s) 66... 67``` 68