(Engine) Variety of engine updates (#390)

* drop common uuid v4 func and imported package as needed

* removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests.

* add in string

* explicitly throw away return error value

* atleast return the error that websocket initialise returns

* return error when not connected

* fix comment

* Adds comments

* move package declarations

* drop append whenever we call supported

* remove unused import

* Change incorrect spelling

* fix tests

* fix go import issue
This commit is contained in:
Ryan O'Hara-Reid
2019-12-03 10:06:08 +11:00
committed by Adrian Gallagher
parent c27b8657e2
commit 0c5d75b22c
70 changed files with 393 additions and 462 deletions

View File

@@ -15,10 +15,12 @@ type connectionManager struct {
conn *connchecker.Checker
}
// Started returns if the connection manager has started
func (c *connectionManager) Started() bool {
return atomic.LoadInt32(&c.started) == 1
}
// Start starts an instance of the connection manager
func (c *connectionManager) Start() error {
if atomic.AddInt32(&c.started, 1) != 1 {
return errors.New("connection manager already started")
@@ -38,6 +40,7 @@ func (c *connectionManager) Start() error {
return nil
}
// Stop stops the connection manager
func (c *connectionManager) Stop() error {
if atomic.LoadInt32(&c.started) == 0 {
return errors.New("connection manager not started")
@@ -55,6 +58,7 @@ func (c *connectionManager) Stop() error {
return nil
}
// IsOnline returns if the connection manager is online
func (c *connectionManager) IsOnline() bool {
if c.conn == nil {
log.Warnln(log.ConnectionMgr, "Connection manager: IsOnline called but conn is nil")

View File

@@ -6,6 +6,7 @@ import (
"sync/atomic"
"time"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
@@ -207,7 +208,7 @@ func (o *orderManager) Submit(exchName string, newOrder *order.Submit) (*orderSu
return nil, errors.New("unable to get exchange by name")
}
id, err := common.GetV4UUID()
id, err := uuid.NewV4()
if err != nil {
log.Warnf(log.OrderMgr,
"Order manager: Unable to generate UUID. Err: %s\n",

View File

@@ -1,12 +1,12 @@
package engine
import (
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
@@ -79,7 +79,7 @@ func (h *WebsocketHub) run() {
// SendWebsocketMessage sends a websocket event to the client
func (c *WebsocketClient) SendWebsocketMessage(evt interface{}) error {
data, err := common.JSONEncode(evt)
data, err := json.Marshal(evt)
if err != nil {
log.Errorf(log.WebsocketMgr, "websocket: failed to send message: %s\n", err)
return err
@@ -106,7 +106,7 @@ func (c *WebsocketClient) read() {
if msgType == websocket.TextMessage {
var evt WebsocketEvent
err := common.JSONDecode(message, &evt)
err := json.Unmarshal(message, &evt)
if err != nil {
log.Errorf(log.WebsocketMgr, "websocket: failed to decode JSON sent from client %s\n", err)
continue
@@ -117,7 +117,7 @@ func (c *WebsocketClient) read() {
continue
}
dataJSON, err := common.JSONEncode(evt.Data)
dataJSON, err := json.Marshal(evt.Data)
if err != nil {
log.Errorln(log.WebsocketMgr, "websocket: client sent data we couldn't JSON decode")
break
@@ -197,7 +197,7 @@ func BroadcastWebsocketMessage(evt WebsocketEvent) error {
return errors.New("websocket service not started")
}
data, err := common.JSONEncode(evt)
data, err := json.Marshal(evt)
if err != nil {
return err
}
@@ -257,7 +257,7 @@ func wsAuth(client *WebsocketClient, data interface{}) error {
}
var auth WebsocketAuth
err := common.JSONDecode(data.([]byte), &auth)
err := json.Unmarshal(data.([]byte), &auth)
if err != nil {
wsResp.Error = err.Error()
client.SendWebsocketMessage(wsResp)
@@ -303,7 +303,7 @@ func wsSaveConfig(client *WebsocketClient, data interface{}) error {
Event: "SaveConfig",
}
var cfg config.Config
err := common.JSONDecode(data.([]byte), &cfg)
err := json.Unmarshal(data.([]byte), &cfg)
if err != nil {
wsResp.Error = err.Error()
client.SendWebsocketMessage(wsResp)
@@ -344,7 +344,7 @@ func wsGetTicker(client *WebsocketClient, data interface{}) error {
Event: "GetTicker",
}
var tickerReq WebsocketOrderbookTickerRequest
err := common.JSONDecode(data.([]byte), &tickerReq)
err := json.Unmarshal(data.([]byte), &tickerReq)
if err != nil {
wsResp.Error = err.Error()
client.SendWebsocketMessage(wsResp)
@@ -376,7 +376,7 @@ func wsGetOrderbook(client *WebsocketClient, data interface{}) error {
Event: "GetOrderbook",
}
var orderbookReq WebsocketOrderbookTickerRequest
err := common.JSONDecode(data.([]byte), &orderbookReq)
err := json.Unmarshal(data.([]byte), &orderbookReq)
if err != nil {
wsResp.Error = err.Error()
client.SendWebsocketMessage(wsResp)