Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决websocket并发 bug #73

Merged
merged 9 commits into from
Nov 5, 2018
Merged

解决websocket并发 bug #73

merged 9 commits into from
Nov 5, 2018

Conversation

locxiang
Copy link
Contributor

解决2个并发问题

panic: concurrent write to websocket connection

WARNING: DATA RACE

panic: concurrent write to websocket connection
WARNING: DATA RACE
WARNING: DATA RACE
WARNING: DATA RACE
@nntaoli
Copy link
Member

nntaoli commented Oct 30, 2018

代码里面有createWsLock 这个锁,应该不会出现并发问题哦

@locxiang
Copy link
Contributor Author

你可以试试,使用-race 编译试试, 会提示 WARNING: DATA RACE

至于,panic: concurrent write to websocket connection 低概率会发生,大概允许给你4,5个小时会导致程序直接崩溃

官方对此的解释
https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
gorilla/websocket#119

@nntaoli
Copy link
Member

nntaoli commented Oct 30, 2018

这个跟使用场景有关系,一般是启动策略后先发订阅信息,然后读取推送数据,这样就不存在并发了吧

@nntaoli
Copy link
Member

nntaoli commented Oct 30, 2018

我目前好像没遇到过并发问题,我在策略中已经实盘跑了okex websocket几个月了

@locxiang
Copy link
Contributor Author

我晚上回家把我的那个业务代码发给你看下吧,同时连接的 okex和火币

-race 编译运行 会出现WARNING: DATA RACE的错误

@locxiang
Copy link
Contributor Author

1. 源代码

package main

import (
	"github.com/nntaoli-project/GoEx/huobi"
	"github.com/nntaoli-project/GoEx"
	"net/http"
	"fmt"
)

func main()  {
	var huoBiPro = huobi.NewHuoBiProSpot(http.DefaultClient, "", "")

	huoBiPro.GetTickerWithWs(goex.EOS_USDT, func(ticker *goex.Ticker) {
		fmt.Printf("huobi \n")
	})

	select {
	}
}

2. 编译运行

go run -race main.go

一定要加上 -race 这个是

3. 错误警告

==================
WARNING: DATA RACE
Read at 0x00c0001a0088 by goroutine 33:
  github.com/gorilla/websocket.(*messageWriter).flushFrame()
      /Users/xh/golib/src/github.com/gorilla/websocket/conn.go:596 +0x6b7
  github.com/gorilla/websocket.(*messageWriter).Close()
      /Users/xh/golib/src/github.com/gorilla/websocket/conn.go:711 +0x72
  github.com/gorilla/websocket.(*Conn).WriteJSON()
      /Users/xh/golib/src/github.com/gorilla/websocket/json.go:29 +0x141
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).createWsConn.func2()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:591 +0x3d6
  github.com/nntaoli-project/GoEx.(*WsConn).ReceiveMessage.func1()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:120 +0x23b

Previous write at 0x00c0001a0088 by main goroutine:
  github.com/gorilla/websocket.(*messageWriter).flushFrame()
      /Users/xh/golib/src/github.com/gorilla/websocket/conn.go:606 +0x883
  github.com/gorilla/websocket.(*messageWriter).Close()
      /Users/xh/golib/src/github.com/gorilla/websocket/conn.go:711 +0x72
  github.com/gorilla/websocket.(*Conn).WriteJSON()
      /Users/xh/golib/src/github.com/gorilla/websocket/json.go:29 +0x141
  github.com/nntaoli-project/GoEx.(*WsConn).Subscribe()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:97 +0x75
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).GetTickerWithWs()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:704 +0x392
  main.main()
      /Users/xh/golib/src/github.com/locxiang/quantitative-trading/exchange/test_race/main.go:13 +0x10d

Goroutine 33 (running) created at:
  github.com/nntaoli-project/GoEx.(*WsConn).ReceiveMessage()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:106 +0x56
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).createWsConn()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:578 +0x1ea
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).GetTickerWithWs()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:701 +0x53
  main.main()
      /Users/xh/golib/src/github.com/locxiang/quantitative-trading/exchange/test_race/main.go:13 +0x10d
==================
2018/10/30 22:57:16 {"id":1,"status":"ok","subbed":"market.eosusdt.detail","ts":1540911436547}
==================
WARNING: DATA RACE
Write at 0x00c0000e000e by goroutine 31:
  runtime.slicecopy()
      /usr/local/Cellar/go/1.11/libexec/src/runtime/slice.go:221 +0x0
  github.com/gorilla/websocket.(*messageWriter).Write()
      /Users/xh/golib/src/github.com/gorilla/websocket/conn.go:657 +0x230
  encoding/json.(*Encoder).Encode()
      /usr/local/Cellar/go/1.11/libexec/src/encoding/json/stream.go:225 +0x44e
  github.com/gorilla/websocket.(*Conn).WriteJSON()
      /Users/xh/golib/src/github.com/gorilla/websocket/json.go:28 +0x119
  github.com/nntaoli-project/GoEx.(*WsConn).Heartbeat.func1()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:81 +0x1b0

Previous read at 0x00c0000e0008 by goroutine 33:
  [failed to restore the stack]

Goroutine 31 (running) created at:
  github.com/nntaoli-project/GoEx.(*WsConn).Heartbeat()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:77 +0xdd
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).createWsConn()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:573 +0x145
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).GetTickerWithWs()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:701 +0x53
  main.main()
      /Users/xh/golib/src/github.com/locxiang/quantitative-trading/exchange/test_race/main.go:13 +0x10d

Goroutine 33 (running) created at:
  github.com/nntaoli-project/GoEx.(*WsConn).ReceiveMessage()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/websocket.go:106 +0x56
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).createWsConn()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:578 +0x1ea
  github.com/nntaoli-project/GoEx/huobi.(*HuoBiPro).GetTickerWithWs()
      /Users/xh/golib/src/github.com/nntaoli-project/GoEx/huobi/HuobiPro.go:701 +0x53
  main.main()
      /Users/xh/golib/src/github.com/locxiang/quantitative-trading/exchange/test_race/main.go:13 +0x10d
==================

@locxiang
Copy link
Contributor Author

locxiang commented Nov 3, 2018

又增加了okex的新版websocket支持

@nntaoli nntaoli merged commit b8bfa76 into nntaoli-project:dev Nov 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants