Add a validator interface to validate strategy before run

This commit is contained in:
Larry850806 2021-04-02 10:12:55 +08:00
parent 74d313305c
commit dbf5d27f30

View File

@ -35,6 +35,10 @@ type CrossExchangeStrategy interface {
CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
}
type Validator interface {
Validate() error
}
//go:generate callbackgen -type Graceful
type Graceful struct {
shutdownCallbacks []func(ctx context.Context, wg *sync.WaitGroup)
@ -235,6 +239,13 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si
}
}
// If the strategy has Validate() method, run it and check the error
if v, ok := strategy.(Validator); ok {
if err := v.Validate(); err != nil {
return fmt.Errorf("failed to validate the config: %w", err)
}
}
return strategy.Run(ctx, orderExecutor, session)
}