Files
kami_gateway/internal/proxy/example.go
danial e566dca395 refactor(proxy): 重构代理池并添加环境变量支持
-重构了代理配置结构体,更名为 Config
- 添加了从环境变量读取代理列表的功能
- 更新了代理池相关代码,使用新的配置结构体
- 修改了 Heepay 相关代码,优化了商品信息处理
2025-04-12 14:13:29 +08:00

56 lines
1.1 KiB
Go

package proxy
import (
"context"
"fmt"
"gateway/internal/otelTrace"
"time"
)
func Example() {
// 获取全局代理池实例
pool := GetGlobalProxyPool()
// 创建配置
config := &Config{
Proxies: []string{
"http://proxy1.example.com:8080",
"http://proxy2.example.com:8080",
"http://proxy3.example.com:8080",
},
TestURL: "https://www.baidu.com",
Timeout: 5,
}
// 使用配置初始化代理池
pool.InitWithConfig(config)
// 启动定期更新代理状态的goroutine
go func() {
ticker := time.NewTicker(time.Duration(config.Timeout) * time.Second)
for range ticker.C {
pool.UpdateProxies()
}
}()
// 使用代理的示例
startTime := time.Now()
proxy, err := pool.GetRandomProxy()
if err != nil {
otelTrace.Logger.WithContext(context.Background()).Error("获取代理失败")
return
}
// 模拟使用代理
time.Sleep(100 * time.Millisecond)
// 标记代理使用完成
pool.MarkProxyUsed(proxy, time.Since(startTime))
// 获取代理统计信息
stats := pool.GetProxyStats()
for _, stat := range stats {
fmt.Printf("代理统计: %+v\n", stat)
}
}