Skip to content

Commit

Permalink
update user agent domain url, refactor to add prefix host to state co…
Browse files Browse the repository at this point in the history
…nstants (#929)
  • Loading branch information
jeevatkm authored Dec 19, 2024
1 parent 7b5db4e commit 8422694
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
6 changes: 5 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var (

defaultAuthScheme = "Bearer"

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

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

// SetProxy method sets the Proxy URL and Port for the Resty client.
//
// // HTTP/HTTPS proxy
// client.SetProxy("http://proxyserver:8888")
//
// // SOCKS5 Proxy
// client.SetProxy("socks5://127.0.0.1:1080")
//
// OR you could also set Proxy via environment variable, refer to [http.ProxyFromEnvironment]
func (c *Client) SetProxy(proxyURL string) *Client {
transport, err := c.HTTPTransport()
Expand Down
2 changes: 2 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func TestClientCACertificateFromStringErrorTls(t *testing.T) {

// CustomRoundTripper2 just for test
type CustomRoundTripper2 struct {
http.RoundTripper
TLSClientConfiger
tlsConfig *tls.Config
returnErr bool
}
Expand Down
24 changes: 12 additions & 12 deletions load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type Host struct {
// Default value is 5
MaxFailures int

state State
state HostState
currentWeight int
failedRequests int
}
Expand All @@ -114,16 +114,16 @@ func (h *Host) resetWeight(totalWeight int) {
h.currentWeight -= totalWeight
}

type State int
type HostState int

// Host transition states
const (
StateInActive State = iota
StateActive
HostStateInActive HostState = iota
HostStateActive
)

// HostStateChangeFunc type provides feedback on host state transitions
type HostStateChangeFunc func(baseURL string, from, to State)
type HostStateChangeFunc func(baseURL string, from, to HostState)

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

Expand Down Expand Up @@ -205,9 +205,9 @@ func (wrr *WeightedRoundRobin) Feedback(f *RequestFeedback) {
host.failedRequests++
}
if host.failedRequests >= host.MaxFailures {
host.state = StateInActive
host.state = HostStateInActive
if wrr.onStateChange != nil {
wrr.onStateChange(host.BaseURL, StateActive, StateInActive)
wrr.onStateChange(host.BaseURL, HostStateActive, HostStateInActive)
}
}
break
Expand Down Expand Up @@ -240,7 +240,7 @@ func (wrr *WeightedRoundRobin) Refresh(hosts ...*Host) error {
}

h.BaseURL = baseURL
h.state = StateActive
h.state = HostStateActive
newTotalWeight += h.Weight

// assign defaults if not provided
Expand Down Expand Up @@ -274,12 +274,12 @@ func (wrr *WeightedRoundRobin) ticker() {
for range wrr.tick.C {
wrr.lock.Lock()
for _, host := range wrr.hosts {
if host.state == StateInActive {
host.state = StateActive
if host.state == HostStateInActive {
host.state = HostStateActive
host.failedRequests = 0

if wrr.onStateChange != nil {
wrr.onStateChange(host.BaseURL, StateInActive, StateActive)
wrr.onStateChange(host.BaseURL, HostStateInActive, HostStateActive)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestWeightedRoundRobin(t *testing.T) {
defer wrr.Close()

var stateChangeCalled int32
wrr.SetOnStateChange(func(baseURL string, from, to State) {
wrr.SetOnStateChange(func(baseURL string, from, to HostState) {
atomic.AddInt32(&stateChangeCalled, 1)
})

Expand Down Expand Up @@ -307,7 +307,7 @@ func TestSRVWeightedRoundRobin(t *testing.T) {
assertNil(t, err)

var stateChangeCalled int32
srv.SetOnStateChange(func(baseURL string, from, to State) {
srv.SetOnStateChange(func(baseURL string, from, to HostState) {
atomic.AddInt32(&stateChangeCalled, 1)
})

Expand Down

0 comments on commit 8422694

Please sign in to comment.