mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add IsolationContext
This commit is contained in:
parent
77ca1a9c75
commit
a940e88016
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,17 +8,17 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var graceful = &Graceful{}
|
|
||||||
|
|
||||||
type ShutdownHandler func(ctx context.Context, wg *sync.WaitGroup)
|
type ShutdownHandler func(ctx context.Context, wg *sync.WaitGroup)
|
||||||
|
|
||||||
//go:generate callbackgen -type Graceful
|
var graceful = &GracefulShutdown{}
|
||||||
type Graceful struct {
|
|
||||||
|
//go:generate callbackgen -type GracefulShutdown
|
||||||
|
type GracefulShutdown struct {
|
||||||
shutdownCallbacks []ShutdownHandler
|
shutdownCallbacks []ShutdownHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown is a blocking call to emit all shutdown callbacks at the same time.
|
// 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
|
var wg sync.WaitGroup
|
||||||
wg.Add(len(g.shutdownCallbacks))
|
wg.Add(len(g.shutdownCallbacks))
|
||||||
|
|
||||||
|
|
18
pkg/bbgo/gracefulshutdown_callbacks.go
Normal file
18
pkg/bbgo/gracefulshutdown_callbacks.go
Normal 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
15
pkg/bbgo/isolation.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package bbgo
|
||||||
|
|
||||||
|
var currentIsolationContext *IsolationContext
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
currentIsolationContext = NewIsolationContext()
|
||||||
|
}
|
||||||
|
|
||||||
|
type IsolationContext struct {
|
||||||
|
gracefulShutdown GracefulShutdown
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIsolationContext() *IsolationContext {
|
||||||
|
return &IsolationContext{}
|
||||||
|
}
|
|
@ -89,7 +89,7 @@ type Trader struct {
|
||||||
exchangeStrategies map[string][]SingleExchangeStrategy
|
exchangeStrategies map[string][]SingleExchangeStrategy
|
||||||
|
|
||||||
// graceful shutdown handlers
|
// graceful shutdown handlers
|
||||||
gracefulShutdown Graceful
|
gracefulShutdown GracefulShutdown
|
||||||
|
|
||||||
logger Logger
|
logger Logger
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user