feat: add BackoffUntilNil func in backoff
This commit is contained in:
parent
2d67e2e0c6
commit
2d4049d78d
@ -282,7 +282,6 @@ func (svr *Service) login() (conn net.Conn, connector Connector, err error) {
|
|||||||
|
|
||||||
func (svr *Service) loopLoginUntilSuccess(maxInterval time.Duration, firstLoginExit bool) {
|
func (svr *Service) loopLoginUntilSuccess(maxInterval time.Duration, firstLoginExit bool) {
|
||||||
xl := xlog.FromContextSafe(svr.ctx)
|
xl := xlog.FromContextSafe(svr.ctx)
|
||||||
successCh := make(chan struct{})
|
|
||||||
|
|
||||||
loginFunc := func() error {
|
loginFunc := func() error {
|
||||||
xl.Info("try to connect to server...")
|
xl.Info("try to connect to server...")
|
||||||
@ -327,13 +326,11 @@ func (svr *Service) loopLoginUntilSuccess(maxInterval time.Duration, firstLoginE
|
|||||||
}
|
}
|
||||||
svr.ctl = ctl
|
svr.ctl = ctl
|
||||||
svr.ctlMu.Unlock()
|
svr.ctlMu.Unlock()
|
||||||
|
|
||||||
close(successCh)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to reconnect to server until success
|
// try to reconnect to server until success
|
||||||
wait.BackoffUntil(loginFunc, wait.NewFastBackoffManager(
|
wait.BackoffUntilNil(loginFunc, wait.NewFastBackoffManager(
|
||||||
wait.FastBackoffOptions{
|
wait.FastBackoffOptions{
|
||||||
Duration: time.Second,
|
Duration: time.Second,
|
||||||
Factor: 2,
|
Factor: 2,
|
||||||
@ -341,7 +338,7 @@ func (svr *Service) loopLoginUntilSuccess(maxInterval time.Duration, firstLoginE
|
|||||||
MaxDuration: maxInterval,
|
MaxDuration: maxInterval,
|
||||||
}),
|
}),
|
||||||
true,
|
true,
|
||||||
wait.MergeAndCloseOnAnyStopChannel(svr.ctx.Done(), successCh))
|
wait.MergeAndCloseOnAnyStopChannel(svr.ctx.Done()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svr *Service) UpdateAllConfigurer(proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) error {
|
func (svr *Service) UpdateAllConfigurer(proxyCfgs []v1.ProxyConfigurer, visitorCfgs []v1.VisitorConfigurer) error {
|
||||||
|
@ -157,6 +157,28 @@ func BackoffUntil(f func() error, backoff BackoffManager, sliding bool, stopCh <
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BackoffUntilNil(f func() error, backoff BackoffManager, sliding bool, stopCh <-chan struct{}) {
|
||||||
|
//first try
|
||||||
|
delay := backoff.Backoff(0, false)
|
||||||
|
ticker := time.NewTicker(delay)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stopCh:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
}
|
||||||
|
if err := f(); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sliding {
|
||||||
|
delay = backoff.Backoff(delay, true)
|
||||||
|
}
|
||||||
|
ticker.Reset(delay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Jitter returns a time.Duration between duration and duration + maxFactor *
|
// Jitter returns a time.Duration between duration and duration + maxFactor *
|
||||||
// duration.
|
// duration.
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user