63 Star 181 Fork 3

Gitee 极速下载 / hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
retry.go 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package kafka
import (
"fmt"
"time"
localconfig "github.com/hyperledger/fabric/orderer/common/localconfig"
)
type retryProcess struct {
shortPollingInterval, shortTimeout time.Duration
longPollingInterval, longTimeout time.Duration
exit chan struct{}
channel channel
msg string
fn func() error
}
func newRetryProcess(retryOptions localconfig.Retry, exit chan struct{}, channel channel, msg string, fn func() error) *retryProcess {
return &retryProcess{
shortPollingInterval: retryOptions.ShortInterval,
shortTimeout: retryOptions.ShortTotal,
longPollingInterval: retryOptions.LongInterval,
longTimeout: retryOptions.LongTotal,
exit: exit,
channel: channel,
msg: msg,
fn: fn,
}
}
func (rp *retryProcess) retry() error {
if err := rp.try(rp.shortPollingInterval, rp.shortTimeout); err != nil {
logger.Debugf("[channel: %s] Switching to the long retry interval", rp.channel.topic())
return rp.try(rp.longPollingInterval, rp.longTimeout)
}
return nil
}
func (rp *retryProcess) try(interval, total time.Duration) (err error) {
// Configuration validation will not allow non-positive ticker values
// (which would result in panic). The path below is for those test cases
// when we cannot avoid the creation of a retriable process but we wish
// to terminate it right away.
if rp.shortPollingInterval == 0 {
return fmt.Errorf("illegal value")
}
// If initial operation is successful, we don't bother start retry process
logger.Debugf("[channel: %s] "+rp.msg, rp.channel.topic())
if err = rp.fn(); err == nil {
logger.Debugf("[channel: %s] Error is nil, breaking the retry loop", rp.channel.topic())
return
}
logger.Debugf("[channel: %s] Initial attempt failed = %s", rp.channel.topic(), err)
tickInterval := time.NewTicker(interval)
tickTotal := time.NewTicker(total)
defer tickTotal.Stop()
defer tickInterval.Stop()
logger.Debugf("[channel: %s] Retrying every %s for a total of %s", rp.channel.topic(), interval.String(), total.String())
for {
select {
case <-rp.exit:
logger.Warningf("[channel: %s] process asked to exit", rp.channel.topic())
return fmt.Errorf("process asked to exit")
case <-tickTotal.C:
return
case <-tickInterval.C:
logger.Debugf("[channel: %s] "+rp.msg, rp.channel.topic())
if err = rp.fn(); err == nil {
logger.Debugf("[channel: %s] Error is nil, breaking the retry loop", rp.channel.topic())
return
}
logger.Debugf("[channel: %s] Need to retry because process failed = %s", rp.channel.topic(), err)
}
}
}
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v2.1.1

搜索帮助