2022-10-02 11:34:52 +00:00
|
|
|
package bbgo
|
|
|
|
|
2022-10-03 08:01:08 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const IsolationContextKey = "bbgo"
|
|
|
|
|
2022-10-03 10:39:07 +00:00
|
|
|
var defaultIsolation = NewIsolation()
|
2022-10-02 11:34:52 +00:00
|
|
|
|
2022-10-03 08:22:41 +00:00
|
|
|
type Isolation struct {
|
2022-10-02 11:34:52 +00:00
|
|
|
gracefulShutdown GracefulShutdown
|
|
|
|
}
|
|
|
|
|
2022-10-03 08:22:41 +00:00
|
|
|
func NewIsolation() *Isolation {
|
|
|
|
return &Isolation{}
|
2022-10-02 11:34:52 +00:00
|
|
|
}
|
2022-10-03 08:01:08 +00:00
|
|
|
|
2022-10-03 08:22:41 +00:00
|
|
|
func NewIsolationFromContext(ctx context.Context) *Isolation {
|
|
|
|
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)
|
|
|
|
}
|