Go第三方库-重试(retry-go)
<p>重试机制的简单库。</p><h2 id="概要">概要</h2>
<p>http请求重试示例:</p>
<pre><code class="language-go">url := "http://example.com"
var body []byte
err := retry.Do(
func() error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
},
)
fmt.Println(body)
</code></pre>
<h2 id="使用">使用</h2>
<p><strong>func BackOffDelay</strong></p>
<pre><code class="language-go">func BackOffDelay(n uint, _ error, config *Config) time.Duration
</code></pre>
<p>BackOffDelay是一种DelayType,它增加了连续重试之间的延时。</p>
<p><strong>func Do</strong></p>
<pre><code class="language-go">func Do(retryableFunc RetryableFunc, opts ...Option) error
</code></pre>
<p><strong>func FixedDelay</strong></p>
<pre><code class="language-go">func FixedDelay(_ uint, _ error, config *Config) time.Duration
</code></pre>
<p>FixedDelay是一种DelayType,它在所有迭代中保持相同的延时。</p>
<p><strong>func IsRecoverable</strong></p>
<pre><code class="language-go">func IsRecoverable(err error) bool
</code></pre>
<p>IsRecoverable检查错误是否是unrecoverableError的实例。</p>
<p><strong>func RandomDelay</strong></p>
<pre><code class="language-go">func RandomDelay(_ uint, _ error, config *Config) time.Duration
</code></pre>
<p>RandomDelay是一种DelayType,它选择一个随机延迟到config.maxJitter。</p>
<p><strong>func Unrecoverable</strong></p>
<pre><code class="language-go">func Unrecoverable(err error) error
</code></pre>
<p>Unrecoverable在unrecoverableError结构中包装错误。</p>
<p><strong>type Config</strong></p>
<pre><code class="language-go">type Config struct {
}
</code></pre>
<p><strong>type DelayTypeFunc</strong></p>
<pre><code class="language-go">type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
</code></pre>
<p>调用DelayTypeFunc以返回在n次尝试后可重试函数失败后等待的下一个延迟。</p>
<p><strong>func CombineDelay</strong></p>
<pre><code class="language-go">func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc
</code></pre>
<p>CombineDelay是一种DelayType,它将所有指定的延迟组合成一个新的DelayTypeFunc。</p>
<p><strong>type Error</strong></p>
<pre><code class="language-go">type Error []error
</code></pre>
<p>Error类型表示重试中的错误列表。</p>
<p><strong>func (Error) Error</strong></p>
<pre><code class="language-go">func (e Error) Error() string
</code></pre>
<p>Error方法返回Error的字符串表示,是错误接口的实现。</p>
<p><strong>func (Error) WrappedError</strong></p>
<pre><code class="language-go">func (e Error) WrappedErrors() []error
</code></pre>
<p>WrappedErrors返回此错误正在包装的错误列表。它是errwrap包中errwrap.Wrapper接口的实现,因此retry.Error可以与该库一起使用。</p>
<p><strong>type OnRetryFunc</strong></p>
<pre><code class="language-go">type OnRetryFunc func(n uint, err error)
</code></pre>
<p>OnRetry函数的函数签名n等于尝试次数。</p>
<p><strong>type Option</strong></p>
<pre><code class="language-go">type Option func(*Config)
</code></pre>
<p>Option表示重试的选项。</p>
<p><strong>func Attempts</strong></p>
<pre><code class="language-go">func Attempts(attempts uint) Option
</code></pre>
<p>Attempts设置的重试次数默认为10。</p>
<p><strong>func Context</strong></p>
<pre><code class="language-go">func Context(ctx context.Context) Option
</code></pre>
<p>Context允许设置重试的context默认是BackgroundContext。</p>
<p><strong>立即取消的例子(也许这不是最好的例子,但我希望它足以作为示例):</strong></p>
<pre><code class="language-go">ctx, cancel := context.WithCancel(context.Background())
cancel()
retry.Do(
func() error {
...
},
retry.Context(ctx),
)
</code></pre>
<p><strong>func Delay</strong></p>
<pre><code class="language-go">func Delay(delay time.Duration) Option
</code></pre>
<p>Delay用于设置重试之间的延迟,设置默认为100毫秒。</p>
<p><strong>func DelayType</strong></p>
<pre><code class="language-go">func DelayType(delayType DelayTypeFunc) Option
</code></pre>
<p>DelayType设置重试之间延迟的类型默认为BackOff。</p>
<p><strong>func LastErrorOnly</strong></p>
<pre><code class="language-go">func LastErrorOnly(lastErrorOnly bool) Option
</code></pre>
<p>返回来自重试函数的直接最后一个错误,默认为false(返回所有内容的包装错误)。</p>
<p><strong>func MaxDelay</strong></p>
<pre><code class="language-go">func MaxDelay(maxDelay time.Duration) Option
</code></pre>
<p>MaxDelay设置重试之间的最大延迟,不适用于默认值。</p>
<p><strong>func MaxJitter</strong></p>
<pre><code class="language-go">func MaxJitter(maxJitter time.Duration) Option
</code></pre>
<p>MaxJitter为RandomDelay设置重试之间的最大随机抖动。</p>
<p><strong>func OnRetry</strong></p>
<pre><code class="language-go">func OnRetry(onRetry OnRetryFunc) Option
</code></pre>
<p>OnRetry函数每次重试都会被回调。</p>
<p><strong>记录每个重试示例:</strong></p>
<pre><code class="language-go">retry.Do(
func() error {
return errors.New("some error")
},
retry.OnRetry(func(n uint, err error) {
log.Printf("#%d: %s\n", n, err)
}),
)
</code></pre>
<p><strong>func RetryIf</strong></p>
<pre><code class="language-go">func RetryIf(retryIf RetryIfFunc) Option
</code></pre>
<p>RetryIf控制是否应在出现错误后尝试重试(假设还有重试剩余次数)。</p>
<p><strong>示例,如果出现特殊错误,请跳过重试:</strong></p>
<pre><code class="language-go">retry.Do(
func() error {
return errors.New("special error")
},
retry.RetryIf(func(err error) bool {
if err.Error() == "special error" {
return false
}
return true
})
)
</code></pre>
<p><strong>默认情况下,如果使用retry.Unrecoverable包装错误,则RetryIf停止执行,因此上面的示例也可以缩短为:</strong></p>
<pre><code class="language-go">retry.Do(
func() error {
return retry.Unrecoverable(errors.New("special error"))
}
)
</code></pre>
<p><strong>type RetryIfFunc</strong></p>
<pre><code class="language-go">type RetryIfFunc func(error) bool
</code></pre>
<p>重试if函数的函数签名。</p>
<p><strong>type RetryableFunc</strong></p>
<pre><code class="language-go">type RetryableFunc func() error
</code></pre>
<p>可重试函数的函数签名。</p>
<h2 id="参考">参考</h2>
<p>avast/retry-go: Simple golang library for retry mechanism (github.com)</p><br><br>
来源:https://www.cnblogs.com/ssjackx/p/15470403.html
頁:
[1]