Skip to content

Commit

Permalink
Engine: Refactor use of global Bot (thrasher-corp#563)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Rots authored Sep 23, 2020
1 parent c038562 commit 991dfed
Show file tree
Hide file tree
Showing 26 changed files with 450 additions and 427 deletions.
4 changes: 2 additions & 2 deletions cmd/config_builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
var wg sync.WaitGroup
for x := range exchange.Exchanges {
name := exchange.Exchanges[x]
err = engine.LoadExchange(name, true, &wg)
err = engine.Bot.LoadExchange(name, true, &wg)
if err != nil {
log.Printf("Failed to load exchange %s. Err: %s", name, err)
continue
Expand All @@ -31,7 +31,7 @@ func main() {
log.Println("Done.")

var cfgs []config.ExchangeConfig
exchanges := engine.GetExchanges()
exchanges := engine.Bot.GetExchanges()
for x := range exchanges {
var cfg *config.ExchangeConfig
cfg, err = exchanges[x].GetDefaultConfig()
Expand Down
4 changes: 2 additions & 2 deletions cmd/exchange_wrapper_coverage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
var wg sync.WaitGroup
for x := range exchange.Exchanges {
name := exchange.Exchanges[x]
err := engine.LoadExchange(name, true, &wg)
err := engine.Bot.LoadExchange(name, true, &wg)
if err != nil {
log.Printf("Failed to load exchange %s. Err: %s", name, err)
continue
Expand All @@ -46,7 +46,7 @@ func main() {
log.Printf("Testing exchange wrappers..")
results := make(map[string][]string)
wg = sync.WaitGroup{}
exchanges := engine.GetExchanges()
exchanges := engine.Bot.GetExchanges()
for x := range exchanges {
wg.Add(1)
go func(num int) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
func main() {
log.Println("Loading flags...")
parseCLFlags()
var err error
log.Println("Loading engine...")
engine.Bot, err = engine.New()
bot, err := engine.New()
if err != nil {
log.Fatalf("Failed to initialise engine. Err: %s", err)
}
engine.Bot = bot

engine.Bot.Settings = engine.Settings{
bot.Settings = engine.Settings{
DisableExchangeAutoPairUpdates: true,
Verbose: verboseOverride,
EnableExchangeHTTPRateLimiter: true,
Expand All @@ -63,7 +63,7 @@ func main() {
wrapperConfig.Exchanges[strings.ToLower(name)] = &config.APICredentialsConfig{}
}
if shouldLoadExchange(name) {
err = engine.LoadExchange(name, true, &wg)
err = bot.LoadExchange(name, true, &wg)
if err != nil {
log.Printf("Failed to load exchange %s. Err: %s", name, err)
continue
Expand Down Expand Up @@ -92,7 +92,7 @@ func main() {
log.Println("Testing exchange wrappers..")
var exchangeResponses []ExchangeResponses

exchs := engine.GetExchanges()
exchs := bot.GetExchanges()
for x := range exchs {
base := exchs[x].GetBase()
if !base.Config.Enabled {
Expand Down
2 changes: 2 additions & 0 deletions currency/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ func (s *Storage) IsVerbose() bool {

// Shutdown shuts down the currency storage system and saves to currency.json
func (s *Storage) Shutdown() error {
s.mtx.Lock()
defer s.mtx.Unlock()
close(s.shutdown)
s.wg.Wait()
return s.WriteCurrencyDataToFile(s.path, true)
Expand Down
2 changes: 1 addition & 1 deletion engine/addr_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func (d *DepositAddressManager) GetDepositAddressesByExchange(exchName string) (

// Sync synchronises all deposit addresses
func (d *DepositAddressManager) Sync() {
result := GetExchangeCryptocurrencyDepositAddresses()
result := Bot.GetExchangeCryptocurrencyDepositAddresses()
d.Store.Seed(result)
}
9 changes: 5 additions & 4 deletions engine/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"sync/atomic"

"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/connchecker"
"github.com/thrasher-corp/gocryptotrader/log"
)
Expand All @@ -21,16 +22,16 @@ func (c *connectionManager) Started() bool {
}

// Start starts an instance of the connection manager
func (c *connectionManager) Start() error {
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(Bot.Config.ConnectionMonitor.DNSList,
Bot.Config.ConnectionMonitor.PublicDomainList,
Bot.Config.ConnectionMonitor.CheckInterval)
c.conn, err = connchecker.New(conf.DNSList,
conf.PublicDomainList,
conf.CheckInterval)
if err != nil {
atomic.CompareAndSwapInt32(&c.started, 1, 0)
return err
Expand Down
Loading

0 comments on commit 991dfed

Please sign in to comment.