goevent-handlinggoroutinebinancebinance-api-client

Golang ending (Binance) web service stream using go routine


I'm integrating Binance API into an existing system and while most parts a straight forward, the data streaming API hits my limited understanding of go-routines. I don't believe there is anything special in the golang SDK for Binance, but essentially I only need two functions, one that starts the data stream and processes events with the event handler given as a parameter and a second one that ends the data stream without actually shutting down the client as it would close all other connections. On a previous project, there were two message types for this, but the binance SDK uses an implementation that returns two go channels, one for errors and an another one, I guess from the name, for stopping the data stram.

The code I wrote for starting the data stream looks like this:


func startDataStream(symbol, interval string, wsKlineHandler futures.WsKlineHandler, errHandler futures.ErrHandler) (err error){

    doneC, stopC, err := futures.WsKlineServe(symbol, interval, wsKlineHandler, errHandler)
    if err != nil {
        fmt.Println(err)
        return err
    }

    return nil
}


This works as expected and streams data. A simple test verifies it:


func runWSDataTest() {
    symbol := "BTCUSDT"
    interval := "15m"
    errHandler := func(err error) {fmt.Println(err)}

    wsKlineHandler := func(event *futures.WsKlineEvent) {fmt.Println(event)}

    _ = startDataStream(symbol, interval, wsKlineHandler, errHandler)
}

The thing that is not so clear to me, mainly due to incomplete understanding, really is how do I stop the stream. I think the returned stopC channel can be used to somehow issue a end singnal similar to, say, a sigterm on system level and then the stream should end.

Say, I have a stopDataStream function that takes a symbol as an argument

func stopDataStream(symbol){

}

Let's suppose I start 5 data streams for five symbols and now I want to stop just one of the streams. That begs the question of:

  1. How do I track all those stopC channels?

  2. Can I use a collection keyed with the symbol, pull the stopC channel, and then just issue a signal to end just that data stream?

  3. How do I actually write into the stopC channel from the stop function?

Again, I don't think this is particularly hard, it's just I could not figure it out yet from the docs so any help would be appreciated.

Thank you


Solution

  • (Answer originally written by @Marvin.Hansen)

    Turned out, just saving & closing the channel solved it all. I was really surprised how easy this is, but here is the code of the updated functions:

    func startDataStream(symbol, interval string, wsKlineHandler futures.WsKlineHandler, errHandler futures.ErrHandler) (err error) {
    
        _, stopC, err := futures.WsKlineServe(symbol, interval, wsKlineHandler, errHandler)
        if err != nil {
            fmt.Println(err)
            return err
        }
        // just save the stop channel 
        chanMap[symbol] = stopC
        return nil
    }
    

    And then, the stop function really becomes embarrassing trivial:

    func stopDataStream(symbol string) {
        stopC := chanMap[symbol]  // load the stop channel for the symbol
        close(stopC) // just close it. 
    }
    

    Finally, testing it all out:

    
    var (
        chanMap map[string]chan struct{}
    )
    
    func runWSDataTest() {
        chanMap = make(map[string]chan struct{})
    
        symbol := "BTCUSDT"
        interval := "15m"
        errHandler := func(err error) { fmt.Println(err) }
        wsKlineHandler := getKLineHandler()
    
        println("Start stream")
        _ = startDataStream(symbol, interval, wsKlineHandler, errHandler)
    
        time.Sleep(3 * time.Second)
    
        println("Stop stream")
        stopDataStream(symbol)
    
        time.Sleep(1 * time.Second)
    }
    
    

    This is it.