add IsolationContext

This commit is contained in:
c9s 2022-10-02 19:34:52 +08:00
parent 77ca1a9c75
commit a940e88016
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 39 additions and 24 deletions

View File

@ -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)
}
}

View File

@ -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))

View File

@ -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)
}
}

15
pkg/bbgo/isolation.go Normal file
View File

@ -0,0 +1,15 @@
package bbgo
var currentIsolationContext *IsolationContext
func init() {
currentIsolationContext = NewIsolationContext()
}
type IsolationContext struct {
gracefulShutdown GracefulShutdown
}
func NewIsolationContext() *IsolationContext {
return &IsolationContext{}
}

View File

@ -89,7 +89,7 @@ type Trader struct {
exchangeStrategies map[string][]SingleExchangeStrategy
// graceful shutdown handlers
gracefulShutdown Graceful
gracefulShutdown GracefulShutdown
logger Logger
}