diff --git a/pkg/bbgo/graceful_callbacks.go b/pkg/bbgo/graceful_callbacks.go deleted file mode 100644 index f194e7b2b..000000000 --- a/pkg/bbgo/graceful_callbacks.go +++ /dev/null @@ -1,18 +0,0 @@ -// Code generated by "callbackgen -type Graceful"; DO NOT EDIT. - -package bbgo - -import ( - "context" - "sync" -) - -func (g *Graceful) OnShutdown(cb ShutdownHandler) { - g.shutdownCallbacks = append(g.shutdownCallbacks, cb) -} - -func (g *Graceful) EmitShutdown(ctx context.Context, wg *sync.WaitGroup) { - for _, cb := range g.shutdownCallbacks { - cb(ctx, wg) - } -} diff --git a/pkg/bbgo/graceful_shutdown.go b/pkg/bbgo/graceful_shutdown.go index 4dbf6824a..ee17e4148 100644 --- a/pkg/bbgo/graceful_shutdown.go +++ b/pkg/bbgo/graceful_shutdown.go @@ -8,17 +8,17 @@ import ( "github.com/sirupsen/logrus" ) -var graceful = &Graceful{} - type ShutdownHandler func(ctx context.Context, wg *sync.WaitGroup) -//go:generate callbackgen -type Graceful -type Graceful struct { +var graceful = &GracefulShutdown{} + +//go:generate callbackgen -type GracefulShutdown +type GracefulShutdown struct { shutdownCallbacks []ShutdownHandler } // Shutdown is a blocking call to emit all shutdown callbacks at the same time. -func (g *Graceful) Shutdown(ctx context.Context) { +func (g *GracefulShutdown) Shutdown(ctx context.Context) { var wg sync.WaitGroup wg.Add(len(g.shutdownCallbacks)) diff --git a/pkg/bbgo/gracefulshutdown_callbacks.go b/pkg/bbgo/gracefulshutdown_callbacks.go new file mode 100644 index 000000000..560b956b0 --- /dev/null +++ b/pkg/bbgo/gracefulshutdown_callbacks.go @@ -0,0 +1,18 @@ +// Code generated by "callbackgen -type GracefulShutdown"; DO NOT EDIT. + +package bbgo + +import ( + "context" + "sync" +) + +func (g *GracefulShutdown) OnShutdown(cb ShutdownHandler) { + g.shutdownCallbacks = append(g.shutdownCallbacks, cb) +} + +func (g *GracefulShutdown) EmitShutdown(ctx context.Context, wg *sync.WaitGroup) { + for _, cb := range g.shutdownCallbacks { + cb(ctx, wg) + } +} diff --git a/pkg/bbgo/isolation.go b/pkg/bbgo/isolation.go new file mode 100644 index 000000000..7db6ff29c --- /dev/null +++ b/pkg/bbgo/isolation.go @@ -0,0 +1,15 @@ +package bbgo + +var currentIsolationContext *IsolationContext + +func init() { + currentIsolationContext = NewIsolationContext() +} + +type IsolationContext struct { + gracefulShutdown GracefulShutdown +} + +func NewIsolationContext() *IsolationContext { + return &IsolationContext{} +} diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 3708ed10f..8b4c33be1 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -89,7 +89,7 @@ type Trader struct { exchangeStrategies map[string][]SingleExchangeStrategy // graceful shutdown handlers - gracefulShutdown Graceful + gracefulShutdown GracefulShutdown logger Logger }