-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
136 lines (98 loc) Β· 3.43 KB
/
doc.go
File metadata and controls
136 lines (98 loc) Β· 3.43 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
package hacktheconn provides HTTP client strategies for dynamic transport selection based on various criteria.
It includes configurable strategies for efficient connection pooling and request routing, tailored for
real-time, high-throughput applications.
# Overview
The package introduces pluggable transport selection strategies to dynamically choose the most suitable
HTTP transport based on custom metrics, such as response time or load. It also supports advanced proxy
handling for distributed systems.
# Features
- **Round-Robin Strategy**: Distributes requests evenly across all available transports.
- **Fill Holes Strategy**: Selects the transport with the fewest concurrent requests.
- **Least Response Time Strategy**: Dynamically picks the transport with the lowest response time using:
- Last Response Time
- Moving Average
- Weighted Average
- **Customizable Strategies**: Extendable with user-defined selection algorithms.
- **Proxy Support**: Fully compatible with both HTTP and SOCKS5 proxies.
# Strategies
## Round-Robin Strategy
Distributes requests evenly across all available transports in a cyclic order.
Example:
transports := []http.RoundTripper{
&http.Transport{},
&http.Transport{},
}
strategy := NewRoundRobinStrategy(transports)
client := &http.Client{
Transport: Transport(strategy),
}
resp, err := client.Get("https://example.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
## Fill Holes Strategy
Chooses the transport with the fewest concurrent requests to ensure fair resource utilization.
Example:
transports := []http.RoundTripper{
&http.Transport{},
&http.Transport{},
}
strategy := NewFillHolesStrategy(transports)
client := &http.Client{
Transport: Transport(strategy),
}
resp, err := client.Get("https://example.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
## Least Response Time Strategy
Selects the transport with the lowest response time. Response time calculation can be configured using
predefined calculators or custom ones.
### Predefined Calculators:
- `LeastResponseTimeLastResponseTimeCalculator`: Uses the most recent response time.
- `LeastResponseTimeMovingAverageCalculator(windowSize int)`: Averages the response times of the last `windowSize` requests.
- `LeastResponseTimeWeightedAverageCalculator(weight float64)`: Applies a weighted average with higher importance to recent response times.
Example with Weighted Average:
transports := []http.RoundTripper{
&http.Transport{},
&http.Transport{},
}
strategy := NewLeastResponseTimeStrategy(
transports,
time.Now,
LeastResponseTimeWeightedAverageCalculator(0.8),
)
client := &http.Client{
Transport: Transport(strategy),
}
resp, err := client.Get("https://example.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
## Custom Strategies
To define a custom strategy, implement the `strategy` interface:
type strategy interface {
Acquire() (http.RoundTripper, error)
Release(http.RoundTripper)
}
Example:
type CustomStrategy struct { ... }
func (cs *CustomStrategy) Acquire() (http.RoundTripper, error) {
// Custom logic
}
func (cs *CustomStrategy) Release(rt http.RoundTripper) {
// Custom cleanup logic
}
# Contributing
Contributions are welcome! Please ensure your code is documented and tested.
1. Fork the repository.
2. Create a feature branch.
3. Submit a pull request.
# License
This package is licensed under the MIT License.
*/
package hacktheconn