-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathretry.go
More file actions
78 lines (71 loc) · 2.77 KB
/
Copy pathretry.go
File metadata and controls
78 lines (71 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package mcp
import (
"time"
"trpc.group/trpc-go/trpc-mcp-go/internal/retry"
)
// RetryConfig defines configuration for MCP client retry behavior.
type RetryConfig struct {
// MaxRetries specifies the maximum number of retry attempts for requests.
// Valid range: 0-10, default: 2
MaxRetries int `json:"max_retries"`
// InitialBackoff specifies the initial backoff duration before the first retry.
// Valid range: 1ms-30s, default: 500ms
InitialBackoff time.Duration `json:"initial_backoff"`
// BackoffFactor specifies the factor to multiply the backoff duration for each retry.
// For example, with factor 2.0: 100ms -> 200ms -> 400ms -> 800ms
// Valid range: 1.0-10.0, default: 2.0
BackoffFactor float64 `json:"backoff_factor"`
// MaxBackoff specifies the maximum backoff duration to cap exponential growth.
// Valid range: minimum is InitialBackoff, maximum: 5 minutes, default: 8s
MaxBackoff time.Duration `json:"max_backoff"`
}
// defaultRetryConfig provides sensible defaults for retry configuration.
// Uses industry standard values: simple and conservative settings.
var defaultRetryConfig = RetryConfig{
MaxRetries: 2, // Conservative retry count
InitialBackoff: 500 * time.Millisecond, // 0.5s initial delay
BackoffFactor: 2.0, // Standard exponential backoff
MaxBackoff: 8 * time.Second, // Maximum delay cap
}
// WithSimpleRetry enables retry with the specified maximum number of attempts.
// Uses default backoff configuration (500ms initial, 2.0 factor, 8s max).
func WithSimpleRetry(maxRetries int) ClientOption {
config := defaultRetryConfig
config.MaxRetries = maxRetries
return func(c *Client) {
internalConfig := retry.Config{
MaxRetries: config.MaxRetries,
InitialBackoff: config.InitialBackoff,
BackoffFactor: config.BackoffFactor,
MaxBackoff: config.MaxBackoff,
}
validated := internalConfig.Validate()
c.retryConfig = &validated
// Set retry config on transport if it exists
if c.transport != nil {
c.transport.setRetryConfig(c.retryConfig)
}
}
}
// WithRetry enables retry with custom configuration.
func WithRetry(config RetryConfig) ClientOption {
return func(c *Client) {
internalConfig := retry.Config{
MaxRetries: config.MaxRetries,
InitialBackoff: config.InitialBackoff,
BackoffFactor: config.BackoffFactor,
MaxBackoff: config.MaxBackoff,
}
validated := internalConfig.Validate()
c.retryConfig = &validated
// Set retry config on transport if it exists
if c.transport != nil {
c.transport.setRetryConfig(c.retryConfig)
}
}
}