2021-04-19 13:42:47 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
package retry
|
2021-01-12 18:03:24 +08:00
|
|
|
|
|
|
|
import (
|
2021-06-23 09:24:10 +08:00
|
|
|
"context"
|
2021-01-12 18:03:24 +08:00
|
|
|
"time"
|
2022-02-24 23:53:53 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/util/errorutil"
|
2021-01-12 18:03:24 +08:00
|
|
|
)
|
|
|
|
|
2021-09-28 20:44:03 +08:00
|
|
|
// Do will run function with retry mechanism.
|
|
|
|
// fn is the func to run.
|
|
|
|
// Option can control the retry times and timeout.
|
2021-06-23 09:24:10 +08:00
|
|
|
func Do(ctx context.Context, fn func() error, opts ...Option) error {
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2021-09-18 10:49:51 +08:00
|
|
|
c := newDefaultConfig()
|
2021-06-23 09:24:10 +08:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(c)
|
|
|
|
}
|
2022-02-24 23:53:53 +08:00
|
|
|
var el errorutil.ErrorList
|
2021-06-23 09:24:10 +08:00
|
|
|
|
|
|
|
for i := uint(0); i < c.attempts; i++ {
|
|
|
|
if err := fn(); err != nil {
|
2021-10-21 12:20:35 +08:00
|
|
|
|
|
|
|
el = append(el, err)
|
|
|
|
|
2022-02-24 23:53:53 +08:00
|
|
|
if ok := IsUnRecoverable(err); ok {
|
2021-10-21 12:20:35 +08:00
|
|
|
return el
|
2021-06-23 09:24:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(c.sleep):
|
|
|
|
case <-ctx.Done():
|
2021-10-21 12:20:35 +08:00
|
|
|
el = append(el, ctx.Err())
|
|
|
|
return el
|
2021-06-23 09:24:10 +08:00
|
|
|
}
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2021-06-23 09:24:10 +08:00
|
|
|
c.sleep *= 2
|
|
|
|
if c.sleep > c.maxSleepTime {
|
|
|
|
c.sleep = c.maxSleepTime
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2021-06-23 09:24:10 +08:00
|
|
|
} else {
|
|
|
|
return nil
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 09:24:10 +08:00
|
|
|
return el
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:49:51 +08:00
|
|
|
type unrecoverableError struct {
|
2021-01-12 18:03:24 +08:00
|
|
|
error
|
|
|
|
}
|
|
|
|
|
2021-09-18 10:49:51 +08:00
|
|
|
// Unrecoverable method wrap an error to unrecoverableError. This will make retry
|
|
|
|
// quick return.
|
|
|
|
func Unrecoverable(err error) error {
|
|
|
|
return unrecoverableError{err}
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:53:53 +08:00
|
|
|
// IsUnRecoverable is used to judge whether the error is wrapped by unrecoverableError.
|
|
|
|
func IsUnRecoverable(err error) bool {
|
2021-09-18 10:49:51 +08:00
|
|
|
_, isUnrecoverable := err.(unrecoverableError)
|
|
|
|
return isUnrecoverable
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|