mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Slight enhance of Coinbase tests Continual enhance of Coinbase tests The revamp continues Oh jeez the Orderbook part's unfinished don't look Coinbase revamp, Orderbook still unfinished * Coinbase revamp; CreateReport is still WIP * More coinbase improvements; onto sandbox testing * Coinbase revamp continues * Coinbase revamp continues * Coinbasepro revamp is ceaseless * Coinbase revamp, starting on advanced trade API * Coinbase Advanced Trade Starts in Ernest V3 done, onto V2 Coinbase revamp nears completion Coinbase revamp nears completion Test commit should fail Coinbase revamp nears completion * Coinbase revamp stage wrapper * Coinbase wrapper coherence continues * Coinbase wrapper continues writhing * Coinbase wrapper & codebase cleanup * Coinbase updates & wrap progress * More Coinbase wrapper progress * Wrapper is wrapped, kinda * Test & type checking * Coinbase REST revamp finished * Post-merge fix * WS revamp begins * WS Main Revamp Done? * CB websocket tidying up * Coinbase WS wrapperupperer * Coinbase revamp done?? * Linter progress * Continued lint cleanup * Further lint cleanup * Increased lint coverage * Does this fix all sloppy reassigns & shadowing? * Undoing retry policy change * Documentation regeneration * Coinbase code improvements * Providing warning about known issue * Updating an error to new format * Making gocritic happy * Review adherence * Endpoints moved to V3 & nil pointer fixes * Removing seemingly superfluous constant * Glorious improvements * Removing unused error * Partial public endpoint addition * Slight improvements * Wrapper improvements; still a few errors left in other packages * A lil Coinbase progress * Json cleaning * Lint appeasement * Config repair * Config fix (real) * Little fix * New public endpoint incorporation * Additional fixes * Improvements & Appeasements * LineSaver * Additional fixes * Another fix * Fixing picked nits * Quick fixies * Lil fixes * Subscriptions: Add List.Enabled * CoinbasePro: Add subscription templating * fixup! CoinbasePro: Add subscription templating * fixup! CoinbasePro: Add subscription templating * Comment fix * Subsequent fixes * Issues hopefully fixed * Lint fix * Glorious fixes * Json formatting * ShazNits * (L/N)i(n/)t * Adding a test * Tiny test improvement * Template patch testing * Fixes * Further shaznits * Lint nit * JWT move and other fixes * Small nits * Shaznit, singular * Post-merge fix * Post-merge fixes * Typo fix * Some glorious nits * Required changes * Stop going * Alias attempt * Alias fix & test cleanup * Test fix * GetDepositAddress logic improvement * Status update: Fixed * Lint fix * Happy birthday to PR 1480 * Cleanups * Necessary nit corrections * Fixing sillybug * As per request * Programming progress * Order fixes * Further fixies * Test fix * Pre-merge fixes * More shaznits * Context * Sonic error handling * Import fix * Better Sonic error handling * Perfect Sonic error handling? * F purge * Coinbase improvements * API Update Conformity * Coinbase continuation * Coinbase order improvements * Coinbase order improvements * CreateOrderConfig improvements * Managing API updates * Coinbase API update progression * jwt rename * Comment link fix * Coinbase v2 cleanup * Post-merge fixes * Review fixes * GK's suggestions * Linter fix * Minor gbjk fixes * Nit fixes * Merge fix * Lint fixes * Coinbase rename stage 1 * Coinbase rename stage 2 * Coinbase rename stage 3 * Coinbase rename stage 4 * Coinbase rename final fix * Coinbase: PoC on converting to request structs * Applying requested changes * Many review fixes, handled * Thrashed by nits * More minor modifications * The last nit!? --------- Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
196 lines
5.4 KiB
Go
196 lines
5.4 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
// vars related to exchange functions
|
|
var (
|
|
ErrNoExchangesLoaded = errors.New("no exchanges have been loaded")
|
|
ErrExchangeNotFound = errors.New("exchange not found")
|
|
ErrExchangeAlreadyLoaded = errors.New("exchange already loaded")
|
|
ErrExchangeFailedToLoad = errors.New("exchange failed to load")
|
|
|
|
errExchangeIsNil = errors.New("exchange is nil")
|
|
)
|
|
|
|
// CustomExchangeBuilder interface allows external applications to create
|
|
// custom/unsupported exchanges that satisfy the IBotExchange interface.
|
|
type CustomExchangeBuilder interface {
|
|
NewExchangeByName(name string) (exchange.IBotExchange, error)
|
|
}
|
|
|
|
// ExchangeManager manages what exchanges are loaded
|
|
type ExchangeManager struct {
|
|
mtx sync.Mutex
|
|
exchanges map[string]exchange.IBotExchange
|
|
Builder CustomExchangeBuilder
|
|
}
|
|
|
|
// NewExchangeManager creates a new exchange manager
|
|
func NewExchangeManager() *ExchangeManager {
|
|
return &ExchangeManager{
|
|
exchanges: make(map[string]exchange.IBotExchange),
|
|
}
|
|
}
|
|
|
|
// Add adds an exchange
|
|
func (m *ExchangeManager) Add(exch exchange.IBotExchange) error {
|
|
if m == nil {
|
|
return fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
|
|
}
|
|
if exch == nil {
|
|
return fmt.Errorf("exchange manager: %w", errExchangeIsNil)
|
|
}
|
|
m.mtx.Lock()
|
|
defer m.mtx.Unlock()
|
|
_, ok := m.exchanges[strings.ToLower(exch.GetName())]
|
|
if ok {
|
|
return fmt.Errorf("exchange manager: %s %w", exch.GetName(), ErrExchangeAlreadyLoaded)
|
|
}
|
|
m.exchanges[strings.ToLower(exch.GetName())] = exch
|
|
return nil
|
|
}
|
|
|
|
// GetExchanges returns all stored exchanges
|
|
func (m *ExchangeManager) GetExchanges() ([]exchange.IBotExchange, error) {
|
|
if m == nil {
|
|
return nil, fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
|
|
}
|
|
m.mtx.Lock()
|
|
defer m.mtx.Unlock()
|
|
exchs := make([]exchange.IBotExchange, 0, len(m.exchanges))
|
|
for _, exch := range m.exchanges {
|
|
exchs = append(exchs, exch)
|
|
}
|
|
return exchs, nil
|
|
}
|
|
|
|
// RemoveExchange removes an exchange from the manager
|
|
func (m *ExchangeManager) RemoveExchange(exchangeName string) error {
|
|
if m == nil {
|
|
return fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
|
|
}
|
|
|
|
if exchangeName == "" {
|
|
return fmt.Errorf("exchange manager: %w", common.ErrExchangeNameNotSet)
|
|
}
|
|
|
|
m.mtx.Lock()
|
|
defer m.mtx.Unlock()
|
|
exch, ok := m.exchanges[strings.ToLower(exchangeName)]
|
|
if !ok {
|
|
return fmt.Errorf("exchange manager: %s %w", exchangeName, ErrExchangeNotFound)
|
|
}
|
|
err := exch.Shutdown()
|
|
if err != nil {
|
|
return fmt.Errorf("exchange manager: %w", err)
|
|
}
|
|
delete(m.exchanges, strings.ToLower(exchangeName))
|
|
log.Infof(log.ExchangeSys, "%s exchange unloaded successfully.\n", exchangeName)
|
|
return nil
|
|
}
|
|
|
|
// GetExchangeByName returns an exchange by its name if it exists
|
|
func (m *ExchangeManager) GetExchangeByName(exchangeName string) (exchange.IBotExchange, error) {
|
|
if m == nil {
|
|
return nil, fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
|
|
}
|
|
if exchangeName == "" {
|
|
return nil, fmt.Errorf("exchange manager: %w", common.ErrExchangeNameNotSet)
|
|
}
|
|
m.mtx.Lock()
|
|
defer m.mtx.Unlock()
|
|
exch, ok := m.exchanges[strings.ToLower(exchangeName)]
|
|
if !ok {
|
|
return nil, fmt.Errorf("exchange manager: %s %w", exchangeName, ErrExchangeNotFound)
|
|
}
|
|
return exch, nil
|
|
}
|
|
|
|
// NewExchangeByName helps create a new exchange to be loaded
|
|
func (m *ExchangeManager) NewExchangeByName(name string) (exchange.IBotExchange, error) {
|
|
nameLower := strings.ToLower(name)
|
|
_, err := m.GetExchangeByName(nameLower)
|
|
if err != nil {
|
|
if !errors.Is(err, ErrExchangeNotFound) {
|
|
return nil, fmt.Errorf("exchange manager: %s %w", name, err)
|
|
}
|
|
} else {
|
|
return nil, fmt.Errorf("exchange manager: %s %w", name, ErrExchangeAlreadyLoaded)
|
|
}
|
|
|
|
if m.Builder != nil {
|
|
return m.Builder.NewExchangeByName(nameLower)
|
|
}
|
|
|
|
return NewSupportedExchangeByName(nameLower)
|
|
}
|
|
|
|
// Shutdown shuts down all exchanges and unloads them
|
|
func (m *ExchangeManager) Shutdown(shutdownTimeout time.Duration) error {
|
|
if m == nil {
|
|
return fmt.Errorf("exchange manager: %w", ErrNilSubsystem)
|
|
}
|
|
|
|
if shutdownTimeout < 0 {
|
|
shutdownTimeout = 0
|
|
}
|
|
|
|
var lockout sync.Mutex
|
|
timer := time.NewTimer(shutdownTimeout)
|
|
var wg sync.WaitGroup
|
|
|
|
m.mtx.Lock()
|
|
defer m.mtx.Unlock()
|
|
|
|
lockout.Lock()
|
|
for _, exch := range m.exchanges {
|
|
wg.Add(1)
|
|
go func(wg *sync.WaitGroup, mtx *sync.Mutex, exch exchange.IBotExchange) {
|
|
err := exch.Shutdown()
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s failed to shutdown %v.\n", exch.GetName(), err)
|
|
} else {
|
|
mtx.Lock()
|
|
delete(m.exchanges, strings.ToLower(exch.GetName()))
|
|
mtx.Unlock()
|
|
}
|
|
wg.Done()
|
|
}(&wg, &lockout, exch)
|
|
}
|
|
lockout.Unlock()
|
|
|
|
ch := make(chan struct{})
|
|
go func(wg *sync.WaitGroup, finish chan<- struct{}) {
|
|
wg.Wait()
|
|
finish <- struct{}{}
|
|
}(&wg, ch)
|
|
|
|
select {
|
|
case <-timer.C:
|
|
// Possible deadlock in a number of operating exchanges.
|
|
lockout.Lock()
|
|
for name := range m.exchanges {
|
|
log.Warnf(log.ExchangeSys, "%s has failed to shutdown within %s, please review.\n", name, shutdownTimeout)
|
|
}
|
|
lockout.Unlock()
|
|
case <-ch:
|
|
// Every exchange has finished their shutdown call.
|
|
lockout.Lock()
|
|
for name := range m.exchanges {
|
|
log.Errorf(log.ExchangeSys, "%s has failed to shutdown due to error, please review.\n", name)
|
|
}
|
|
lockout.Unlock()
|
|
}
|
|
return nil
|
|
}
|