From 33085318c46bcb727c1f952b3297c83a46e50e1b Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 12 Jun 2019 17:52:40 +1000 Subject: [PATCH] Switch connchecker to service --- cmd/gctcli/main.go | 2 +- engine/connection.go | 63 ++++++++++++++++++++++++++++++++++++++++++ engine/engine.go | 19 +++++++------ engine/helpers.go | 6 +--- engine/helpers_test.go | 15 +++++----- 5 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 engine/connection.go diff --git a/cmd/gctcli/main.go b/cmd/gctcli/main.go index d501e69b..36d0f9ba 100644 --- a/cmd/gctcli/main.go +++ b/cmd/gctcli/main.go @@ -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, }, } diff --git a/engine/connection.go b/engine/connection.go new file mode 100644 index 00000000..f104c256 --- /dev/null +++ b/engine/connection.go @@ -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() +} diff --git a/engine/engine.go b/engine/engine.go index 87c50ec1..7c513bb3 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -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 { diff --git a/engine/helpers.go b/engine/helpers.go index fb500ca0..938efa6e 100644 --- a/engine/helpers.go +++ b/engine/helpers.go @@ -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 diff --git a/engine/helpers_test.go b/engine/helpers_test.go index 7b66bee2..77d5fde2 100644 --- a/engine/helpers_test.go +++ b/engine/helpers_test.go @@ -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 } }