Documentation
¶
Index ¶
- Variables
- func GetTimeoutControllerStats() map[string]TimeoutControllerStats
- func HTTPClientMiddleware(tc *TimeoutController) func(http.RoundTripper) http.RoundTripper
- func HTTPMiddleware(tc *TimeoutController) func(next http.Handler) http.Handler
- func IsHTTPTimeoutError(err error) bool
- func IsTimeoutError(err error) bool
- func ResetAllTimeoutControllerStats()
- func ResetTimeoutControllerStats(name string) bool
- func StreamClientInterceptor(tc *TimeoutController) grpc.StreamClientInterceptor
- func StreamServerInterceptor(tc *TimeoutController) grpc.StreamServerInterceptor
- func UnaryClientInterceptor(tc *TimeoutController) grpc.UnaryClientInterceptor
- func UnaryServerInterceptor(tc *TimeoutController) grpc.UnaryServerInterceptor
- func WrapTimeoutError(err error) error
- type Config
- type HTTPTimeoutError
- type Manager
- func (m *Manager) Get(name string) (*TimeoutController, bool)
- func (m *Manager) GetOrCreate(name string, timeout ...time.Duration) *TimeoutController
- func (m *Manager) List() []string
- func (m *Manager) Remove(name string) bool
- func (m *Manager) ResetStats()
- func (m *Manager) ResetStatsByName(name string) bool
- func (m *Manager) Stats() map[string]TimeoutControllerStats
- type ManagerConfig
- type Stats
- type TimeoutController
- func (tc *TimeoutController) Execute(ctx context.Context, fn func(ctx context.Context) error) error
- func (tc *TimeoutController) ExecuteWithResult(ctx context.Context, fn func(ctx context.Context) (any, error)) (any, error)
- func (tc *TimeoutController) Name() string
- func (tc *TimeoutController) ResetStats()
- func (tc *TimeoutController) SlowRate() float64
- func (tc *TimeoutController) SlowThreshold() time.Duration
- func (tc *TimeoutController) Stats() Stats
- func (tc *TimeoutController) String() string
- func (tc *TimeoutController) Timeout() time.Duration
- func (tc *TimeoutController) TimeoutRate() float64
- type TimeoutControllerStats
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTimeout 超时错误 ErrTimeout = errors.New("request timeout") // ErrTimeoutCanceled 请求被取消错误 ErrTimeoutCanceled = errors.New("request canceled") )
Functions ¶
func GetTimeoutControllerStats ¶
func GetTimeoutControllerStats() map[string]TimeoutControllerStats
GetTimeoutControllerStats 从默认管理器获取统计信息
func HTTPClientMiddleware ¶
func HTTPClientMiddleware(tc *TimeoutController) func(http.RoundTripper) http.RoundTripper
HTTPClientMiddleware 返回一个 HTTP 客户端中间件,用于超时控制
func HTTPMiddleware ¶
func HTTPMiddleware(tc *TimeoutController) func(next http.Handler) http.Handler
HTTPMiddleware 返回一个 HTTP 中间件,用于超时控制。 使用标准库 http.TimeoutHandler 实现,避免 goroutine 泄漏和超时后写入已关闭 ResponseWriter 的数据竞争。
func IsHTTPTimeoutError ¶
IsHTTPTimeoutError 检查错误是否为 HTTP 超时错误
func ResetAllTimeoutControllerStats ¶
func ResetAllTimeoutControllerStats()
ResetAllTimeoutControllerStats 重置默认管理器中的所有超时控制器统计信息
func ResetTimeoutControllerStats ¶
ResetTimeoutControllerStats 重置默认管理器中的超时控制器统计信息
func StreamClientInterceptor ¶
func StreamClientInterceptor(tc *TimeoutController) grpc.StreamClientInterceptor
StreamClientInterceptor 返回一个 gRPC 流客户端拦截器,用于超时控制
func StreamServerInterceptor ¶
func StreamServerInterceptor(tc *TimeoutController) grpc.StreamServerInterceptor
StreamServerInterceptor 返回一个 gRPC 流服务器拦截器,用于超时控制
func UnaryClientInterceptor ¶
func UnaryClientInterceptor(tc *TimeoutController) grpc.UnaryClientInterceptor
UnaryClientInterceptor 返回一个 gRPC 一元客户端拦截器,用于超时控制
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(tc *TimeoutController) grpc.UnaryServerInterceptor
UnaryServerInterceptor 返回一个 gRPC 一元服务器拦截器,用于超时控制
Types ¶
type Config ¶
type Config struct {
// Name 超时控制器名称
Name string
// Timeout 超时时间
Timeout time.Duration
// SlowThreshold 慢请求阈值,超过此时间会记录警告日志
SlowThreshold time.Duration
// OnTimeout 超时时的回调函数
OnTimeout func(name string, duration time.Duration)
// OnSlow 慢请求时的回调函数
OnSlow func(name string, duration time.Duration)
// EnableMetrics 是否启用指标统计
EnableMetrics bool
}
Config 超时配置
type HTTPTimeoutError ¶
type HTTPTimeoutError struct {
StatusCode int
}
HTTPTimeoutError 标识 HTTP 响应状态码层面的错误(非超时,用于客户端 Transport)
func (*HTTPTimeoutError) Error ¶
func (e *HTTPTimeoutError) Error() string
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 超时控制器管理器,用于管理多个超时控制器实例
func (*Manager) GetOrCreate ¶
func (m *Manager) GetOrCreate(name string, timeout ...time.Duration) *TimeoutController
GetOrCreate 获取或创建超时控制器
func (*Manager) ResetStatsByName ¶
ResetStatsByName 重置指定名称的超时控制器统计信息
func (*Manager) Stats ¶
func (m *Manager) Stats() map[string]TimeoutControllerStats
Stats 获取所有超时控制器的统计信息
type ManagerConfig ¶
type ManagerConfig struct {
// DefaultTimeout 默认超时时间
DefaultTimeout time.Duration
// DefaultSlowThreshold 默认慢请求阈值
DefaultSlowThreshold time.Duration
// EnableLogging 是否启用日志
EnableLogging bool
// EnableMetrics 是否启用指标统计
EnableMetrics bool
}
ManagerConfig 管理器配置
type Stats ¶
type Stats struct {
TotalRequests uint64 `json:"total_requests"` // 总请求数
TimeoutRequests uint64 `json:"timeout_requests"` // 超时请求数
SlowRequests uint64 `json:"slow_requests"` // 慢请求数
AvgDuration time.Duration `json:"avg_duration"` // 平均响应时间
MaxDuration time.Duration `json:"max_duration"` // 最大响应时间
MinDuration time.Duration `json:"min_duration"` // 最小响应时间
}
Stats 统计信息
type TimeoutController ¶
type TimeoutController struct {
// contains filtered or unexported fields
}
TimeoutController 超时控制器
func GetTimeoutController ¶
func GetTimeoutController(name string, timeout ...time.Duration) *TimeoutController
GetTimeoutController 从默认管理器获取或创建超时控制器
func NewTimeoutController ¶
func NewTimeoutController(config Config) *TimeoutController
NewTimeoutController 创建超时控制器
func (*TimeoutController) ExecuteWithResult ¶
func (tc *TimeoutController) ExecuteWithResult(ctx context.Context, fn func(ctx context.Context) (any, error)) (any, error)
ExecuteWithResult 执行带超时控制和返回值的函数
func (*TimeoutController) SlowRate ¶
func (tc *TimeoutController) SlowRate() float64
SlowRate 返回慢请求率
func (*TimeoutController) SlowThreshold ¶
func (tc *TimeoutController) SlowThreshold() time.Duration
SlowThreshold 返回慢请求阈值
func (*TimeoutController) Timeout ¶
func (tc *TimeoutController) Timeout() time.Duration
Timeout 返回超时时间
func (*TimeoutController) TimeoutRate ¶
func (tc *TimeoutController) TimeoutRate() float64
TimeoutRate 返回超时率
type TimeoutControllerStats ¶
type TimeoutControllerStats struct {
Name string `json:"name"`
Timeout time.Duration `json:"timeout"`
SlowThreshold time.Duration `json:"slow_threshold"`
Stats Stats `json:"stats"`
TimeoutRate float64 `json:"timeout_rate"`
SlowRate float64 `json:"slow_rate"`
}
TimeoutControllerStats 超时控制器统计信息
func (TimeoutControllerStats) String ¶
func (s TimeoutControllerStats) String() string
String 返回统计信息的字符串表示