Files
gocryptotrader/engine/connection.go
Ryan O'Hara-Reid 0c5d75b22c (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
2019-12-03 10:06:08 +11:00

70 lines
1.8 KiB
Go

package engine
import (
"errors"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/connchecker"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
// connectionManager manages the connchecker
type connectionManager struct {
started int32
stopped int32
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")
}
log.Debugln(log.ConnectionMgr, "Connection manager starting...")
var err error
c.conn, err = connchecker.New(Bot.Config.ConnectionMonitor.DNSList,
Bot.Config.ConnectionMonitor.PublicDomainList,
Bot.Config.ConnectionMonitor.CheckInterval)
if err != nil {
atomic.CompareAndSwapInt32(&c.started, 1, 0)
return err
}
log.Debugln(log.ConnectionMgr, "Connection manager started.")
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")
}
if atomic.AddInt32(&c.stopped, 1) != 1 {
return errors.New("connection manager is already stopped")
}
log.Debugln(log.ConnectionMgr, "Connection manager shutting down...")
c.conn.Shutdown()
atomic.CompareAndSwapInt32(&c.stopped, 1, 0)
atomic.CompareAndSwapInt32(&c.started, 1, 0)
log.Debugln(log.ConnectionMgr, "Connection manager stopped.")
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")
return false
}
return c.conn.IsConnected()
}