跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

retry.go

61 行 · 1018 B历史下载
1package utils
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/sirupsen/logrus"
9)
10
11const maxRetries = 60
12const initialBackoff = 100 * time.Millisecond
13
14func Retry[T comparable](ctx context.Context, retryFunc func() (T, error), retryMsg string) (T, error) {
15 var (
16 returnVal T
17 err error
18 )
19
20 backoff := initialBackoff
21
22loop:
23 for i := 0; i < maxRetries; i++ {
24 select {
25 case <-ctx.Done():
26 break loop
27 default:
28 // proceed
29 }
30
31 returnVal, err = retryFunc()
32 if err == nil {
33 return returnVal, nil
34 }
35 logrus.Debugf("%s (%s)", retryMsg, backoff)
36 Sleep(ctx, backoff)
37 backoff = backOff(backoff)
38 }
39 return returnVal, fmt.Errorf("timeout: %w", err)
40}
41
42func backOff(delay time.Duration) time.Duration {
43 if delay == 0 {
44 delay = 5 * time.Millisecond
45 } else {
46 delay *= 2
47 }
48 if delay > time.Second {
49 delay = time.Second
50 }
51 return delay
52}
53
54func Sleep(ctx context.Context, wait time.Duration) bool {
55 select {
56 case <-ctx.Done():
57 return false
58 case <-time.After(wait):
59 return true
60 }
61}