From bb2068ba2717584d600ba2f783f339415fc7060e Mon Sep 17 00:00:00 2001 From: Amir Date: Sat, 16 Oct 2021 12:27:39 +0330 Subject: [PATCH] Release: v1.0.25-alpha --- CHANGELOG.md | 7 +++++++ api/ws/client.go | 37 ++++++++++++++++++++++++++----------- api/ws/private.go | 24 ------------------------ api/ws/trade.go | 12 ------------ 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4213aa2..0f0aa9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ Changelog ========= All notable changes to this project will be documented in this file. +v1.0.25-alpha +------------ + +### Changed + +- Fixed login process + v1.0.24-alpha ------------ diff --git a/api/ws/client.go b/api/ws/client.go index 50bbc67..63a862d 100644 --- a/api/ws/client.go +++ b/api/ws/client.go @@ -10,6 +10,7 @@ import ( "github.com/amir-the-h/okex" "github.com/amir-the-h/okex/events" "github.com/gorilla/websocket" + "log" "net/http" "sync" "time" @@ -107,14 +108,17 @@ func (c *ClientWs) Connect(p bool) error { // // https://www.okex.com/docs-v5/en/#websocket-api-login func (c *ClientWs) Login() error { + c.mu[true].Lock() if c.Authorized { return nil } - if c.AuthRequested != nil && time.Since(*c.AuthRequested) < 30 { + if c.AuthRequested != nil && time.Since(*c.AuthRequested).Seconds() < 30 { return nil } + log.Println("login") now := time.Now() c.AuthRequested = &now + c.mu[true].Unlock() method := http.MethodGet path := "/users/self/verify" ts, sign := c.sign(method, path) @@ -167,10 +171,20 @@ func (c *ClientWs) Unsubscribe(p bool, ch []okex.ChannelName, args map[string]st // Send message through either connections func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string, extras ...map[string]string) error { - err := c.Connect(p) - if err != nil { - return err + if op != okex.LoginOperation { + err := c.Connect(p) + if err == nil { + if p { + err = c.WaitForAuthorization() + if err != nil { + return err + } + } + } else { + return err + } } + data := map[string]interface{}{ "op": op, "args": args, @@ -199,11 +213,14 @@ func (c *ClientWs) SetChannels(errCh chan *events.Error, subCh chan *events.Subs // WaitForAuthorization waits for the auth response and try to log in if it was needed func (c *ClientWs) WaitForAuthorization() error { - ticker := time.NewTicker(time.Millisecond * 300) - defer ticker.Stop() + if c.Authorized { + return nil + } if err := c.Login(); err != nil { return err } + ticker := time.NewTicker(time.Millisecond * 300) + defer ticker.Stop() for range ticker.C { if c.Authorized { return nil @@ -220,21 +237,20 @@ func (c *ClientWs) dial(p bool) error { c.rmu[p].Unlock() }() conn, res, err := websocket.DefaultDialer.Dial(string(c.url[p]), nil) + defer res.Body.Close() if err != nil { - return fmt.Errorf("error %d: %s", res.StatusCode, err) + return fmt.Errorf("error %d: %w", res.StatusCode, err) } go func() { err := c.receiver(p) if err != nil { fmt.Printf("receiver error: %v\n", err) - c.Cancel() } }() go func() { err := c.sender(p) if err != nil { fmt.Printf("sender error: %v\n", err) - c.Cancel() } }() c.conn[p] = conn @@ -362,8 +378,7 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool { }() return true case "login": - au := time.Now().Add(time.Second * -30) - if au.After(*c.AuthRequested) { + if time.Since(*c.AuthRequested).Seconds() > 30 { c.AuthRequested = nil _ = c.Login() break diff --git a/api/ws/private.go b/api/ws/private.go index e05e233..2a80b68 100644 --- a/api/ws/private.go +++ b/api/ws/private.go @@ -33,10 +33,6 @@ func (c *Private) Account(req requests.Account, ch ...chan *private.Account) err if len(ch) > 0 { c.aCh = ch[0] } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Subscribe(true, []okex.ChannelName{"account"}, m) } @@ -60,10 +56,6 @@ func (c *Private) Position(req requests.Position, ch ...chan *private.Position) if len(ch) > 0 { c.pCh = ch[0] } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Subscribe(true, []okex.ChannelName{"positions"}, m) } @@ -75,10 +67,6 @@ func (c *Private) UPosition(req requests.Position, rCh ...bool) error { if len(rCh) > 0 && rCh[0] { c.pCh = nil } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Unsubscribe(true, []okex.ChannelName{"positions"}, m) } @@ -91,10 +79,6 @@ func (c *Private) BalanceAndPosition(ch ...chan *private.BalanceAndPosition) err if len(ch) > 0 { c.bnpCh = ch[0] } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Subscribe(true, []okex.ChannelName{"balance_and_position"}, m) } @@ -118,10 +102,6 @@ func (c *Private) Order(req requests.Order, ch ...chan *private.Order) error { if len(ch) > 0 { c.oCh = ch[0] } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Subscribe(true, []okex.ChannelName{"orders"}, m) } @@ -133,10 +113,6 @@ func (c *Private) UOrder(req requests.Order, rCh ...bool) error { if len(rCh) > 0 && rCh[0] { c.oCh = nil } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Unsubscribe(true, []okex.ChannelName{"orders"}, m) } diff --git a/api/ws/trade.go b/api/ws/trade.go index 1cb470d..71c8a6e 100644 --- a/api/ws/trade.go +++ b/api/ws/trade.go @@ -34,10 +34,6 @@ func (c *Trade) PlaceOrder(req ...requests.PlaceOrder) error { for i, order := range req { tmpArgs[i] = okex.S2M(order) } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID}) } @@ -58,10 +54,6 @@ func (c *Trade) CancelOrder(req ...requests.CancelOrder) error { for i, order := range req { tmpArgs[i] = okex.S2M(order) } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID}) } @@ -82,9 +74,5 @@ func (c *Trade) AmendOrder(req ...requests.AmendOrder) error { for i, order := range req { tmpArgs[i] = okex.S2M(order) } - err := c.WaitForAuthorization() - if err != nil { - return err - } return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID}) }