2022-10-02 11:34:52 +00:00
|
|
|
package bbgo
|
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2022-10-03 10:40:49 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2022-10-03 08:01:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const IsolationContextKey = "bbgo"
|
|
|
|
|
2022-10-04 09:23:43 +00:00
|
|
|
var defaultIsolation = NewDefaultIsolation()
|
2022-10-02 11:34:52 +00:00
|
|
|
|
2022-10-03 08:22:41 +00:00
|
|
|
type Isolation struct {
|
2022-10-03 10:40:49 +00:00
|
|
|
gracefulShutdown GracefulShutdown
|
|
|
|
persistenceServiceFacade *service.PersistenceServiceFacade
|
2022-10-02 11:34:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 09:23:43 +00:00
|
|
|
func NewDefaultIsolation() *Isolation {
|
2022-10-03 10:40:49 +00:00
|
|
|
return &Isolation{
|
|
|
|
gracefulShutdown: GracefulShutdown{},
|
2022-10-04 09:23:43 +00:00
|
|
|
persistenceServiceFacade: persistenceServiceFacade,
|
2022-10-03 10:40:49 +00:00
|
|
|
}
|
2022-10-02 11:34:52 +00:00
|
|
|
}
|
2022-10-03 08:01:08 +00:00
|
|
|
|
2022-10-05 10:42:31 +00:00
|
|
|
func NewIsolation(persistenceFacade *service.PersistenceServiceFacade) *Isolation {
|
|
|
|
return &Isolation{
|
|
|
|
gracefulShutdown: GracefulShutdown{},
|
|
|
|
persistenceServiceFacade: persistenceFacade,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 09:23:43 +00:00
|
|
|
func GetIsolationFromContext(ctx context.Context) *Isolation {
|
2022-10-03 08:22:41 +00:00
|
|
|
isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation)
|
2022-10-03 08:01:08 +00:00
|
|
|
if ok {
|
|
|
|
return isolatedContext
|
|
|
|
}
|
|
|
|
|
2022-10-03 08:22:41 +00:00
|
|
|
return defaultIsolation
|
2022-10-03 08:01:08 +00:00
|
|
|
}
|
2022-10-03 10:39:07 +00:00
|
|
|
|
|
|
|
func NewContextWithIsolation(parent context.Context, isolation *Isolation) context.Context {
|
|
|
|
return context.WithValue(parent, IsolationContextKey, isolation)
|
|
|
|
}
|
2022-10-03 10:39:39 +00:00
|
|
|
|
|
|
|
func NewContextWithDefaultIsolation(parent context.Context) context.Context {
|
|
|
|
return context.WithValue(parent, IsolationContextKey, defaultIsolation)
|
|
|
|
}
|