Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
a05087d1
提交
a05087d1
authored
5月 23, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
retry
上级
8cc1c024
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
172 行增加
和
0 行删除
+172
-0
merge.go
merger/merge.go
+1
-0
xretry.go
utils/xretry/xretry.go
+171
-0
没有找到文件。
merger/merge.go
浏览文件 @
a05087d1
...
...
@@ -33,6 +33,7 @@ func (f *FanIn) Start() {
go
f
.
start
(
st
)
<-
st
}
func
(
f
*
FanIn
)
start
(
st
chan
struct
{})
{
st
<-
struct
{}{}
for
{
...
...
utils/xretry/xretry.go
0 → 100644
浏览文件 @
a05087d1
package
xretry
import
(
"context"
"errors"
"fmt"
"math"
"math/rand"
"reflect"
"runtime"
"strings"
"time"
)
const
(
DefaultRetryTimes
=
5
DefaultRetryLinearInterval
=
time
.
Second
*
3
)
type
RetryConfig
struct
{
context
context
.
Context
retryTimes
uint
backoffStrategy
BackoffStrategy
}
type
RetryFunc
func
()
error
type
Option
func
(
*
RetryConfig
)
func
RetryTimes
(
n
uint
)
Option
{
return
func
(
rc
*
RetryConfig
)
{
rc
.
retryTimes
=
n
}
}
func
RetryWithCustomBackoff
(
backoffStrategy
BackoffStrategy
)
Option
{
if
backoffStrategy
==
nil
{
panic
(
"backoffStrategy不能为空"
)
}
return
func
(
rc
*
RetryConfig
)
{
rc
.
backoffStrategy
=
backoffStrategy
}
}
func
RetryWithLinearBackoff
(
interval
time
.
Duration
)
Option
{
if
interval
<=
0
{
panic
(
"retry间隔必须大于0"
)
}
return
func
(
rc
*
RetryConfig
)
{
rc
.
backoffStrategy
=
&
linear
{
interval
:
interval
,
}
}
}
func
RetryWithExponentialWithJitterBackoff
(
interval
time
.
Duration
,
base
uint64
,
maxJitter
time
.
Duration
)
Option
{
if
interval
<=
0
{
panic
(
"retry间隔必须大于0"
)
}
if
maxJitter
<
0
{
panic
(
"maxJitter必须大于0"
)
}
if
base
%
2
==
0
{
return
func
(
rc
*
RetryConfig
)
{
rc
.
backoffStrategy
=
&
shiftExponentialWithJitter
{
interval
:
interval
,
maxJitter
:
maxJitter
,
shifter
:
uint64
(
math
.
Log2
(
float64
(
base
))),
}
}
}
return
func
(
rc
*
RetryConfig
)
{
rc
.
backoffStrategy
=
&
exponentialWithJitter
{
interval
:
interval
,
base
:
time
.
Duration
(
base
),
maxJitter
:
maxJitter
,
}
}
}
func
Context
(
ctx
context
.
Context
)
Option
{
return
func
(
rc
*
RetryConfig
)
{
rc
.
context
=
ctx
}
}
func
Retry
(
retryFunc
RetryFunc
,
opts
...
Option
)
error
{
config
:=
&
RetryConfig
{
retryTimes
:
DefaultRetryTimes
,
context
:
context
.
TODO
(),
}
for
_
,
opt
:=
range
opts
{
opt
(
config
)
}
if
config
.
backoffStrategy
==
nil
{
config
.
backoffStrategy
=
&
linear
{
interval
:
DefaultRetryLinearInterval
,
}
}
var
i
uint
for
i
<
config
.
retryTimes
{
err
:=
retryFunc
()
if
err
!=
nil
{
select
{
case
<-
time
.
After
(
config
.
backoffStrategy
.
CalculateInterval
())
:
case
<-
config
.
context
.
Done
()
:
return
errors
.
New
(
"retry is cancelled"
)
}
}
else
{
return
nil
}
i
++
}
funcPath
:=
runtime
.
FuncForPC
(
reflect
.
ValueOf
(
retryFunc
)
.
Pointer
())
.
Name
()
lastSlash
:=
strings
.
LastIndex
(
funcPath
,
"/"
)
funcName
:=
funcPath
[
lastSlash
+
1
:
]
return
fmt
.
Errorf
(
"function %s run failed after %d times retry"
,
funcName
,
i
)
}
type
BackoffStrategy
interface
{
CalculateInterval
()
time
.
Duration
}
type
linear
struct
{
interval
time
.
Duration
}
func
(
l
linear
)
CalculateInterval
()
time
.
Duration
{
return
l
.
interval
}
type
exponentialWithJitter
struct
{
base
time
.
Duration
interval
time
.
Duration
maxJitter
time
.
Duration
}
func
(
e
*
exponentialWithJitter
)
CalculateInterval
()
time
.
Duration
{
current
:=
e
.
interval
e
.
interval
=
e
.
interval
*
e
.
base
return
current
+
jitter
(
e
.
maxJitter
)
}
type
shiftExponentialWithJitter
struct
{
interval
time
.
Duration
maxJitter
time
.
Duration
shifter
uint64
}
func
(
e
shiftExponentialWithJitter
)
CalculateInterval
()
time
.
Duration
{
current
:=
e
.
interval
e
.
interval
=
e
.
interval
<<
e
.
shifter
return
current
+
jitter
(
e
.
maxJitter
)
}
func
jitter
(
maxJitter
time
.
Duration
)
time
.
Duration
{
if
maxJitter
==
0
{
return
0
}
return
time
.
Duration
(
rand
.
Int63n
(
int64
(
maxJitter
))
+
1
)
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论