mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Switch connchecker to service
This commit is contained in:
@@ -79,7 +79,7 @@ func main() {
|
||||
cli.StringFlag{
|
||||
Name: "delimiter",
|
||||
Value: "-",
|
||||
Usage: "the default pair delimiter used to standardise currency pair input",
|
||||
Usage: "the default currency pair delimiter used to standardise currency pair input",
|
||||
Destination: &pairDelimiter,
|
||||
},
|
||||
}
|
||||
|
||||
63
engine/connection.go
Normal file
63
engine/connection.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/connchecker"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
)
|
||||
|
||||
// connectionManager manages the connchecker
|
||||
type connectionManager struct {
|
||||
started int32
|
||||
stopped int32
|
||||
conn *connchecker.Checker
|
||||
}
|
||||
|
||||
func (c *connectionManager) Started() bool {
|
||||
return atomic.LoadInt32(&c.started) == 1
|
||||
}
|
||||
|
||||
func (c *connectionManager) Start() error {
|
||||
if atomic.AddInt32(&c.started, 1) != 1 {
|
||||
return errors.New("connection manager already started")
|
||||
}
|
||||
|
||||
log.Debugln("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("Connection manager started.")
|
||||
return nil
|
||||
}
|
||||
|
||||
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("Connection manager shutting down...")
|
||||
c.conn.Shutdown()
|
||||
log.Debugln("Connection manager stopped.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *connectionManager) IsOnline() bool {
|
||||
if c.conn == nil {
|
||||
log.Warnf("Connection manager: IsOnline called but conn is nil")
|
||||
return false
|
||||
}
|
||||
|
||||
return c.conn.IsConnected()
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/communications"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/connchecker"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/currency/coinmarketcap"
|
||||
"github.com/thrasher-/gocryptotrader/engine/events"
|
||||
@@ -33,10 +32,10 @@ type Engine struct {
|
||||
Portfolio *portfolio.Base
|
||||
Exchanges []exchange.IBotExchange
|
||||
ExchangeCurrencyPairManager *ExchangeCurrencyPairSyncer
|
||||
ConnectionManager connectionManager
|
||||
OrderManager orderManager
|
||||
PortfolioManager portfolioManager
|
||||
CommsRelayer *communications.Communications
|
||||
Connectivity *connchecker.Checker
|
||||
Shutdown chan struct{}
|
||||
Settings Settings
|
||||
CryptocurrencyDepositAddresses map[string]map[string]string
|
||||
@@ -267,13 +266,9 @@ func (e *Engine) Start() {
|
||||
}
|
||||
|
||||
// Sets up internet connectivity monitor
|
||||
var err error
|
||||
if e.Settings.EnableConnectivityMonitor {
|
||||
e.Connectivity, err = connchecker.New(e.Config.ConnectionMonitor.DNSList,
|
||||
e.Config.ConnectionMonitor.PublicDomainList,
|
||||
e.Config.ConnectionMonitor.CheckInterval)
|
||||
if err != nil {
|
||||
log.Fatalf("Connectivity checker failure: %s", err)
|
||||
if err := e.ConnectionManager.Start(); err != nil {
|
||||
log.Errorf("Connection manager unable to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +333,7 @@ func (e *Engine) Start() {
|
||||
newFxSettings = append(newFxSettings, currency.FXSettings(d))
|
||||
}
|
||||
|
||||
err = currency.RunStorageUpdater(currency.BotOverrides{
|
||||
err := currency.RunStorageUpdater(currency.BotOverrides{
|
||||
Coinmarketcap: e.Settings.EnableCoinmarketcapAnalysis,
|
||||
FxCurrencyConverter: e.Settings.EnableCurrencyConverter,
|
||||
FxCurrencyLayer: e.Settings.EnableCurrencyLayer,
|
||||
@@ -430,6 +425,12 @@ func (e *Engine) Stop() {
|
||||
}
|
||||
}
|
||||
|
||||
if e.ConnectionManager.Started() {
|
||||
if err := e.ConnectionManager.Stop(); err != nil {
|
||||
log.Errorf("Connection manager unable to stop. Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !e.Settings.EnableDryRun {
|
||||
err := e.Config.SaveConfig(e.Settings.ConfigFile)
|
||||
if err != nil {
|
||||
|
||||
@@ -82,11 +82,7 @@ func GetAuthAPISupportedExchanges() []string {
|
||||
|
||||
// IsOnline returns whether or not the engine has Internet connectivity
|
||||
func IsOnline() bool {
|
||||
if Bot.Connectivity == nil {
|
||||
log.Warnf("IsOnline called but Bot.Connectivity is nil")
|
||||
return false
|
||||
}
|
||||
return Bot.Connectivity.IsConnected()
|
||||
return Bot.ConnectionManager.IsOnline()
|
||||
}
|
||||
|
||||
// GetAvailableExchanges returns a list of enabled exchanges
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/connchecker"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/assets"
|
||||
@@ -104,6 +103,9 @@ func TestGetExchangeoOTPByName(t *testing.T) {
|
||||
if result == "" {
|
||||
t.Fatal("Expected valid OTP code")
|
||||
}
|
||||
|
||||
// Flush setting
|
||||
bCfg.API.Credentials.OTPSecret = ""
|
||||
}
|
||||
|
||||
func TestGetAuthAPISupportedExchanges(t *testing.T) {
|
||||
@@ -119,22 +121,21 @@ func TestIsOnline(t *testing.T) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
var err error
|
||||
Bot.Connectivity, err = connchecker.New(Bot.Config.ConnectionMonitor.DNSList,
|
||||
Bot.Config.ConnectionMonitor.PublicDomainList,
|
||||
Bot.Config.ConnectionMonitor.CheckInterval)
|
||||
if err != nil {
|
||||
if err := Bot.ConnectionManager.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tick := time.NewTicker(time.Second * 5)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
t.Fatal("Test timeout")
|
||||
default:
|
||||
if IsOnline() {
|
||||
Bot.Connectivity.Shutdown()
|
||||
if err := Bot.ConnectionManager.Stop(); err != nil {
|
||||
t.Fatal("unable to shutdown connection manager")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user