add defaulter interface

This commit is contained in:
c9s 2022-07-19 16:59:56 +08:00
parent 808ba2fc02
commit 8af2f2f83f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -24,10 +24,15 @@ type SingleExchangeStrategy interface {
Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error
} }
// StrategyInitializer's Initialize method is called before the Subscribe method call.
type StrategyInitializer interface { type StrategyInitializer interface {
Initialize() error Initialize() error
} }
type StrategyDefaulter interface {
Defaults() error
}
// ExchangeSessionSubscriber provides an interface for collecting subscriptions from different strategies // ExchangeSessionSubscriber provides an interface for collecting subscriptions from different strategies
// Subscribe method will be called before the user data stream connection is created. // Subscribe method will be called before the user data stream connection is created.
type ExchangeSessionSubscriber interface { type ExchangeSessionSubscriber interface {
@ -153,6 +158,12 @@ func (trader *Trader) Subscribe() {
for sessionName, strategies := range trader.exchangeStrategies { for sessionName, strategies := range trader.exchangeStrategies {
session := trader.environment.sessions[sessionName] session := trader.environment.sessions[sessionName]
for _, strategy := range strategies { for _, strategy := range strategies {
if defaulter, ok := strategy.(StrategyDefaulter) ; ok {
if err := defaulter.Defaults(); err != nil {
panic(err)
}
}
if initializer, ok := strategy.(StrategyInitializer); ok { if initializer, ok := strategy.(StrategyInitializer); ok {
if err := initializer.Initialize(); err != nil { if err := initializer.Initialize(); err != nil {
panic(err) panic(err)