使用Go协程结束(Binance)Web服务流的代码示例:

huangapple go评论73阅读模式
英文:

Golang ending (Binance) web service stream using go routine

问题

我正在将Binance API集成到现有系统中,大部分部分都很简单,但数据流API超出了我对go-routines的理解范围。我不认为Binance的golang SDK中有任何特殊之处,但基本上我只需要两个函数:一个函数用于启动数据流,并将事件处理程序作为参数进行处理;另一个函数用于结束数据流,而不会关闭客户端,因为这样会关闭所有其他连接。在以前的项目中,有两种消息类型,但binance SDK使用了一种返回两个go通道的实现,一个用于错误,另一个(根据名称猜测)用于停止数据流。

我编写的启动数据流的代码如下:

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
}

这个代码按预期工作并流式传输数据。一个简单的测试可以验证它:

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)
}

对我来说不太清楚的事情,主要是由于理解不完全,实际上是如何停止数据流。我认为返回的stopC通道可以用于类似于系统级别的end信号,然后数据流应该结束。

假设我为五个符号启动了五个数据流,现在我想停止其中一个数据流。这就引出了以下问题:

1)如何跟踪所有这些stopC通道?

2)我可以使用以符号为键的集合,提取stopC通道,然后只发出结束该数据流的信号吗?

3)如何从停止函数向stopC通道写入数据?

再次强调,我不认为这特别困难,只是我还没有从文档中找到答案,所以任何帮助将不胜感激。

谢谢。

英文:

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

答案1

得分: 1

原来,只需保存并关闭通道就解决了问题。我真的很惊讶这是多么简单,但这是更新后的函数代码:

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
	}
    // 只需保存停止通道
	chanMap[symbol] = stopC
	return nil
}

然后,停止函数变得非常简单:

func stopDataStream(symbol string) {
	stopC := chanMap[symbol]  // 加载该符号的停止通道
	close(stopC) // 直接关闭它
}

最后,测试一切是否正常:

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("开始流")
	_ = startDataStream(symbol, interval, wsKlineHandler, errHandler)

	time.Sleep(3 * time.Second)

	println("停止流")
	stopDataStream(symbol)

	time.Sleep(1 * time.Second)
}

就是这样。

英文:

(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.

huangapple
  • 本文由 发表于 2021年6月25日 13:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/68126096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定