Closed ultrasaw closed 5 months ago
Hey, it's been long time since I used this library, but you don't need to use a WaitGroup. If correctly implemented, the IOC is supposed to wait for all async events. Also, you don't need to run the IOC in another goroutine, I guess you could (if correctly implemented by the author(s)). Ideally you run your background processes in another goroutine, not the other way around.
About the readLoop
, I wouldn't do it that way, I'd rather you could define the callback as a function instead of as a lambda function.
To add to the above, you can handle multiple websocket streams in the same goroutine, no need to go run(wg, url, stream)
. There's an example in the README, you can apply it to websocket streams as well:
You can create n
streams in a for
loop one-by-one, setting up the read loop (by lambda is fine as well, it's a style thing) properly for each one correctly, without spawning any goroutine. You don't even need to append each connection to a slice. In the end, just use for { ioc.Poll() }
or ioc.Run()
to run all the streams.
@dgrr @sergiu128 thank you so much for the responses. Indeed while waiting on the comments here & extending on the example, I found out that wait groups are not necessary, neat;
@dgrr out of curiosity, since you said you haven't used the sonic
package in a long time, did you switch to your own implementation for low latency websocket communication?
Have a splendid remainder of the week!
First of all, thank you so much for open-sourcing the
sonic
package! It made me rethink my current approach to websocket stream subscribing as I currently do everything in a dedicated goroutine per asset, e.g.:Above is the high-level overview of how I currently handle websocket connections. However after stumbling onto this package, I realized that handling all streams of interest, e.g. order book, trades etc., could be better implemented using sonic, hence my question regarding the binance example:
if I were to subscribe to multiple different streams of different servers, how would I go on about doing that? Does the code below correctly implement the idea using
sonic
?Another question I have is how to properly use this package together with additional logic, e.g. in the binance example one would get a snapshot of the order book while buffering the update events. Would using a
sync.WaitGroup
be considered correct in the following scenario?Thank you so much in advance!
PS I'm not expecting exact code to be provided back to me, i.e. I want to use this moment to get to know golang and the sonic package better, would also greatly appreciate any hints / recommendations on how to get comfortable with this repo so that I am able to contribute in future 😇 Recommendations on what to try and build myself from the ground up are most welcome.