mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* refactor use of global Bot * make the dependency on having an existing running Engine more obvious * use explicit dependency of engine in RPCServer * reduce static dependencies in rpcserver * improve helpers * revert bad document update, add check for nil error in test * add basic start stop test * fix race condition in storage * skip the test because of race conditions * fix typo * add empty line
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"sync/atomic"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
"github.com/thrasher-corp/gocryptotrader/connchecker"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
// 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(conf *config.ConnectionMonitorConfig) 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(conf.DNSList,
|
|
conf.PublicDomainList,
|
|
conf.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()
|
|
}
|