Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8422694

Browse files
authoredDec 19, 2024··
update user agent domain url, refactor to add prefix host to state constants (#929)
1 parent 7b5db4e commit 8422694

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed
 

Diff for: ‎client.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var (
7777

7878
defaultAuthScheme = "Bearer"
7979

80-
hdrUserAgentValue = "go-resty/" + Version + " (https://github.com/go-resty/resty)"
80+
hdrUserAgentValue = "go-resty/" + Version + " (https://resty.dev)"
8181
bufPool = &sync.Pool{New: func() any { return &bytes.Buffer{} }}
8282
)
8383

@@ -1426,8 +1426,12 @@ func (c *Client) ProxyURL() *url.URL {
14261426

14271427
// SetProxy method sets the Proxy URL and Port for the Resty client.
14281428
//
1429+
// // HTTP/HTTPS proxy
14291430
// client.SetProxy("http://proxyserver:8888")
14301431
//
1432+
// // SOCKS5 Proxy
1433+
// client.SetProxy("socks5://127.0.0.1:1080")
1434+
//
14311435
// OR you could also set Proxy via environment variable, refer to [http.ProxyFromEnvironment]
14321436
func (c *Client) SetProxy(proxyURL string) *Client {
14331437
transport, err := c.HTTPTransport()

Diff for: ‎client_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ func TestClientCACertificateFromStringErrorTls(t *testing.T) {
275275

276276
// CustomRoundTripper2 just for test
277277
type CustomRoundTripper2 struct {
278+
http.RoundTripper
279+
TLSClientConfiger
278280
tlsConfig *tls.Config
279281
returnErr bool
280282
}

Diff for: ‎load_balancer.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type Host struct {
101101
// Default value is 5
102102
MaxFailures int
103103

104-
state State
104+
state HostState
105105
currentWeight int
106106
failedRequests int
107107
}
@@ -114,16 +114,16 @@ func (h *Host) resetWeight(totalWeight int) {
114114
h.currentWeight -= totalWeight
115115
}
116116

117-
type State int
117+
type HostState int
118118

119119
// Host transition states
120120
const (
121-
StateInActive State = iota
122-
StateActive
121+
HostStateInActive HostState = iota
122+
HostStateActive
123123
)
124124

125125
// HostStateChangeFunc type provides feedback on host state transitions
126-
type HostStateChangeFunc func(baseURL string, from, to State)
126+
type HostStateChangeFunc func(baseURL string, from, to HostState)
127127

128128
// ErrNoActiveHost error returned when all hosts are inactive on the load balancer
129129
var ErrNoActiveHost = errors.New("resty: no active host")
@@ -173,7 +173,7 @@ func (wrr *WeightedRoundRobin) Next() (string, error) {
173173
var best *Host
174174
total := 0
175175
for _, h := range wrr.hosts {
176-
if h.state == StateInActive {
176+
if h.state == HostStateInActive {
177177
continue
178178
}
179179

@@ -205,9 +205,9 @@ func (wrr *WeightedRoundRobin) Feedback(f *RequestFeedback) {
205205
host.failedRequests++
206206
}
207207
if host.failedRequests >= host.MaxFailures {
208-
host.state = StateInActive
208+
host.state = HostStateInActive
209209
if wrr.onStateChange != nil {
210-
wrr.onStateChange(host.BaseURL, StateActive, StateInActive)
210+
wrr.onStateChange(host.BaseURL, HostStateActive, HostStateInActive)
211211
}
212212
}
213213
break
@@ -240,7 +240,7 @@ func (wrr *WeightedRoundRobin) Refresh(hosts ...*Host) error {
240240
}
241241

242242
h.BaseURL = baseURL
243-
h.state = StateActive
243+
h.state = HostStateActive
244244
newTotalWeight += h.Weight
245245

246246
// assign defaults if not provided
@@ -274,12 +274,12 @@ func (wrr *WeightedRoundRobin) ticker() {
274274
for range wrr.tick.C {
275275
wrr.lock.Lock()
276276
for _, host := range wrr.hosts {
277-
if host.state == StateInActive {
278-
host.state = StateActive
277+
if host.state == HostStateInActive {
278+
host.state = HostStateActive
279279
host.failedRequests = 0
280280

281281
if wrr.onStateChange != nil {
282-
wrr.onStateChange(host.BaseURL, StateInActive, StateActive)
282+
wrr.onStateChange(host.BaseURL, HostStateInActive, HostStateActive)
283283
}
284284
}
285285
}

Diff for: ‎load_balancer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func TestWeightedRoundRobin(t *testing.T) {
137137
defer wrr.Close()
138138

139139
var stateChangeCalled int32
140-
wrr.SetOnStateChange(func(baseURL string, from, to State) {
140+
wrr.SetOnStateChange(func(baseURL string, from, to HostState) {
141141
atomic.AddInt32(&stateChangeCalled, 1)
142142
})
143143

@@ -307,7 +307,7 @@ func TestSRVWeightedRoundRobin(t *testing.T) {
307307
assertNil(t, err)
308308

309309
var stateChangeCalled int32
310-
srv.SetOnStateChange(func(baseURL string, from, to State) {
310+
srv.SetOnStateChange(func(baseURL string, from, to HostState) {
311311
atomic.AddInt32(&stateChangeCalled, 1)
312312
})
313313

0 commit comments

Comments
 (0)
Please sign in to comment.