Skip to content

Commit

Permalink
feat(server): infinite max. errors if lt 0
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 committed Sep 15, 2024
1 parent 9c37b4d commit 0724d5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions common/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Options:
--remove-on-error Remove proxy IP from proxy pool on failed HTTP requests
--max-errors <N> Max. errors allowed during rotation (default: 3)
Use this with --rotate-on-error
If value is less than 0 (e.g., -1), rotation will
continue indefinitely
--max-redirs <N> Max. redirects allowed (default: 10)
--max-retries <N> Max. retries for failed HTTP requests (default: 0)
-s, --sync Syncrounus mode
Expand Down
20 changes: 17 additions & 3 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (p *Proxy) onRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Reque
go func(r *http.Request) {
log.Debugf("%s %s %s", r.RemoteAddr, r.Method, r.URL)

for i := 0; i <= p.Options.MaxErrors; i++ {
i := 0
for {
proxy := p.rotateProxy()

retryablehttpClient, err := p.getClient(r, proxy)
Expand All @@ -49,6 +50,12 @@ func (p *Proxy) onRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Reque

resp, err := retryablehttpClient.Do(retryablehttpRequest)
if err != nil {
if i >= p.Options.MaxErrors && p.Options.MaxErrors >= 0 {
resChan <- err

return
}

if p.Options.RemoveOnErr {
p.removeProxy(proxy)

Expand All @@ -58,12 +65,19 @@ func (p *Proxy) onRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Reque
)
}

if p.Options.RotateOnErr && i < p.Options.MaxErrors {
if p.Options.RotateOnErr && (i < p.Options.MaxErrors || p.Options.MaxErrors <= 0) {
remaining := fmt.Sprint(p.Options.MaxErrors - i)
if p.Options.MaxErrors <= 0 {
remaining = "∞"
}

log.Debugf(
"%s Retrying (rotated) %s %s [remaining=%q]",
r.RemoteAddr, r.Method, r.URL, fmt.Sprint(p.Options.MaxErrors-i),
r.RemoteAddr, r.Method, r.URL, remaining,
)

i++

continue
} else {
resChan <- err
Expand Down

0 comments on commit 0724d5f

Please sign in to comment.