move to onAuth

This commit is contained in:
chiahung 2023-10-16 16:02:43 +08:00
parent c5449374cd
commit 5ff3828ec1
2 changed files with 23 additions and 20 deletions

View File

@ -23,9 +23,29 @@ type SyncActiveOrdersOpts struct {
exchange types.Exchange
}
func (s *Strategy) initializeRecoverCh() bool {
s.mu.Lock()
defer s.mu.Unlock()
alreadyInitialize := false
if s.activeOrdersRecoverCh == nil {
s.logger.Info("initialize recover channel")
s.activeOrdersRecoverCh = make(chan struct{}, 1)
} else {
s.logger.Info("already initialize recover channel, trigger active orders recover")
alreadyInitialize = true
s.activeOrdersRecoverCh <- struct{}{}
}
return alreadyInitialize
}
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
// every time we activeOrdersRecoverCh receive signal, do active orders recover
s.activeOrdersRecoverCh = make(chan struct{}, 1)
if alreadyInitialize := s.initializeRecoverCh(); alreadyInitialize {
return
}
// make ticker's interval random in 25 min ~ 35 min
interval := util.MillisecondsJitter(25*time.Minute, 10*60*1000)

View File

@ -1967,14 +1967,8 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
// if TriggerPrice is zero, that means we need to open the grid when start up
if s.TriggerPrice.IsZero() {
// must call the openGrid method inside the OnStart callback because
// it needs to receive the trades from the user data stream
//
// should try to avoid blocking the user data stream
// callbacks are blocking operation
session.UserDataStream.OnStart(func() {
s.logger.Infof("user data stream started, initializing grid...")
session.UserDataStream.OnAuth(func() {
s.logger.Infof("user data stream authenticated, start the process")
if !bbgo.IsBackTesting {
time.AfterFunc(3*time.Second, func() {
if err := s.startProcess(ctx, session); err != nil {
@ -1989,17 +1983,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
})
}
session.UserDataStream.OnAuth(func() {
time.AfterFunc(util.MillisecondsJitter(5*time.Second, 1000*10), func() {
select {
case s.activeOrdersRecoverCh <- struct{}{}:
s.logger.Info("trigger active orders recover when on auth")
default:
s.logger.Warn("failed to trigger active orders recover when on auth")
}
})
})
return nil
}