Walking Corpse
Walking Corpse

Reputation: 73

Simultaneous two OkHttp websocket connections, one of them getting thread starved

In my spring boot app that I'm running in Intellij I have one @Scheduled task, and two WebSocket streams open to two different servers using OkHttp connections from two different instances of OkHttpClients. I don't think the Connection pool setting in the OkHttpClient.Builder is of relevance here since there are two instances of OkHttpClient. Both the websocket streams receive some events initially and then one of them never receives any event anymore. I have also verified by printing the hashcode of the worker threads that they are different. [![image][1]][1]

Sadly I notice the same problem on the command line mvn run also, so it doesn't seem to be an Intellij specific issue.

How can I make the events received on both socket streams continously? Will it help if I reused the same OkHttpClient to create both the connections?

Update: Not wanting to waste time waiting to solve this I went to the extreme step of separating into 2 separate applications each with a websocket listener. Yet I face the same issue, one of them stops receiving any callback after a few seconds of starting. Can't blame it on the remote server, because if I ran any one of them individually, it never stops receiving the callback stream. I need them to be up and running simultaneously and communicate with one another.

fwiw, I'm using [this][2] client library to subscribe to one WebSocket stream, and [this][3] for another. The first endpoint is wss://stream.binance.com:9443/ws/btcusdt@aggTrade and the second is wss://fstream.binance.com. The latter subscription is established by posting a JSON {"method":"SUBSCRIBE","id":1663348051361,"params":"btcusdt@aggTrade"} to it.

I have chatted with the customer support of the server endpoints, they are unable to help me, they just said that if I'm able to subscribe to the socket stream individually just fine, they don't see any reason why one of them should stop working when I bring them up both at the same time. [1]: https://i.sstatic.net/T5oia.png [2]: https://github.com/binance-exchange/binance-java-api/blob/master/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java#L47 [3]: https://github.com/Binance-docs/Binance_Futures_Java/blob/master/src/main/java/com/binance/client/impl/WebSocketStreamClientImpl.java#L63

Upvotes: 0

Views: 1023

Answers (2)

bhantol
bhantol

Reputation: 9616

Even though you have stream.binance.com:9443 and fstream.binance.com listed as 2 host:port then may be served by a single server.

And even if you separated these 2 clients into its own jvm (to rule out threading and other issues) your clientIp and other parameters might make the 2 clients looks like 1.

I suggest try to make these 2 client subscriptions look different by spoofing client ip header. Perhaps set X-Forwarded-For and/or the X-Real-IP.

https://stackoverflow.com/a/67103219/2103767 might help in providing that socket

Update:

Are you sending pong frame ?

According to https://binance-docs.github.io/apidocs/futures/en/#websocket-market-streams you must send a pong frame otherwise it will disconnect the client.

The websocket server will send a ping frame every 5 minutes. If the websocket server does not receive a pong frame back from the connection within a 15 minute period, the connection will be disconnected. Unsolicited pong frames are allowed.

Not sure if it suits your needs but you can listen to 2 channels with same client.

Upvotes: 0

Jesse Wilson
Jesse Wilson

Reputation: 40593

Each WebSocket gets its own thread. Could it be something in your listener?

Upvotes: 1

Related Questions