收口审计治理与套餐任务进展
Constraint: 在线热修前必须保存当前迭代分支全部有效代码进展 Confidence: medium Scope-risk: broad Directive: 后续修改需保持审计事件与业务事务边界一致 Tested: git diff --cached --check Not-tested: 未运行全量测试,提交用于切换分支前保存既有工作
This commit is contained in:
@@ -38,6 +38,22 @@ type Client struct {
|
||||
maxRetries int
|
||||
}
|
||||
|
||||
type attemptObserverKey struct{}
|
||||
|
||||
// AttemptObserver 记录一次 Gateway HTTP 尝试的开始和结果。
|
||||
type AttemptObserver interface {
|
||||
BeforeAttempt(ctx context.Context, attempt int) error
|
||||
AfterAttempt(ctx context.Context, attempt int, callErr error) error
|
||||
}
|
||||
|
||||
// WithAttemptObserver 为当前 Gateway 调用注入逐次 HTTP 尝试观察器。
|
||||
func WithAttemptObserver(ctx context.Context, observer AttemptObserver) context.Context {
|
||||
if observer == nil {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, attemptObserverKey{}, observer)
|
||||
}
|
||||
|
||||
// requestWrapper 用于将请求参数包装为 Gateway 的 {"params": ...} 格式
|
||||
type requestWrapper struct {
|
||||
Params interface{} `json:"params"`
|
||||
@@ -101,6 +117,7 @@ func (c *Client) doRequest(ctx context.Context, path string, params interface{})
|
||||
|
||||
// 带重试的 HTTP 请求
|
||||
var lastErr error
|
||||
observer, _ := ctx.Value(attemptObserverKey{}).(AttemptObserver)
|
||||
for attempt := 0; attempt <= c.maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// 检查用户 Context 是否已取消
|
||||
@@ -120,7 +137,18 @@ func (c *Client) doRequest(ctx context.Context, path string, params interface{})
|
||||
time.Sleep(delay)
|
||||
}
|
||||
|
||||
attemptNumber := attempt + 1
|
||||
if observer != nil {
|
||||
if err := observer.BeforeAttempt(ctx, attemptNumber); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result, retryable, err := c.executeHTTPRequest(ctx, path, encryptedData)
|
||||
if observer != nil {
|
||||
if observeErr := observer.AfterAttempt(ctx, attemptNumber, err); observeErr != nil {
|
||||
return nil, observeErr
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
// 仅对网络级错误重试
|
||||
|
||||
Reference in New Issue
Block a user