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

fix status concurrent issue and add missing fields #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ws *WsService) baseSubscribe(event string, channel string, payload []strin
func (ws *WsService) readMsg() {
ws.once.Do(func() {
go func() {
defer ws.Client.Close()
defer ws.Close()

for {
select {
Expand All @@ -157,7 +157,7 @@ func (ws *WsService) readMsg() {

var rawTrade UpdateMsg
if err := json.Unmarshal(message, &rawTrade); err != nil {
ws.Logger.Printf("Unmarshal err:%s, body:%s", err.Error(), string(message))
ws.Logger.Printf("Unmarshal err: %s, body: %s", err.Error(), string(message))
continue
}

Expand Down
36 changes: 25 additions & 11 deletions go/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"os"
"strings"
"sync"
"sync/atomic"
"time"

mapset "github.com/deckarep/golang-set"
"github.com/gorilla/websocket"
)

type status int

const (
disconnected status = iota
disconnected uint64 = iota
connected
reconnecting
)
Expand All @@ -30,7 +29,7 @@ type WsService struct {
msgChs *sync.Map // business chan
calls *sync.Map
conf *ConnConf
status status
status atomic.Uint64
clientMu *sync.Mutex
}

Expand Down Expand Up @@ -108,10 +107,11 @@ func NewWsService(ctx context.Context, logger *log.Logger, conf *ConnConf) (*WsS
calls: new(sync.Map),
msgChs: new(sync.Map),
once: new(sync.Once),
status: connected,
clientMu: new(sync.Mutex),
}

ws.status.Store(connected)

go ws.activePing()

return ws, nil
Expand Down Expand Up @@ -172,7 +172,7 @@ func (ws *WsService) GetConnConf() *ConnConf {

func (ws *WsService) reconnect() error {
// avoid repeated reconnection
if ws.status == reconnecting {
if ws.status.Load() == reconnecting {
return nil
}

Expand All @@ -183,7 +183,7 @@ func (ws *WsService) reconnect() error {
ws.Client.Close()
}

ws.status = reconnecting
ws.status.Store(reconnecting)

stop := false
retry := 0
Expand All @@ -204,7 +204,7 @@ func (ws *WsService) reconnect() error {
}
}

ws.status = connected
ws.status.Store(connected)

// resubscribe after reconnect
ws.conf.subscribeMsg.Range(func(key, value interface{}) bool {
Expand Down Expand Up @@ -298,6 +298,20 @@ func (ws *WsService) GetConnection() *websocket.Conn {
return ws.Client
}

func (ws *WsService) IsConnected() bool {
return ws.status.Load() == connected
}

func (ws *WsService) Close() {
ws.mu.Lock()
defer ws.mu.Unlock()
if ws.Client != nil {
if err := ws.Client.Close(); err != nil {
ws.Logger.Printf("close err: %s", err.Error())
}
}
}

func (ws *WsService) activePing() {
du, err := time.ParseDuration(ws.conf.PingInterval)
if err != nil {
Expand Down Expand Up @@ -325,7 +339,7 @@ func (ws *WsService) activePing() {
return true
})

if ws.status != connected {
if ws.status.Load() != connected {
continue
}

Expand All @@ -339,12 +353,12 @@ func (ws *WsService) activePing() {
}
}

var statusString = map[status]string{
var statusString = map[uint64]string{
disconnected: "disconnected",
connected: "connected",
reconnecting: "reconnecting",
}

func (ws *WsService) Status() string {
return statusString[ws.status]
return statusString[ws.status.Load()]
}
2 changes: 2 additions & 0 deletions go/response_spot.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ type OrderMsg struct {
FillPrice string `json:"fill_price,omitempty"`
// Total filled in quote currency
FilledTotal string `json:"filled_total,omitempty"`
// Average deal price
AvgDealPrice string `json:"avg_deal_price,omitempty"`
// Fee deducted
Fee string `json:"fee,omitempty"`
// Fee currency unit
Expand Down