Dynamic rate limiter
This commit is contained in:
parent
da9a243ee4
commit
27a22ba023
@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -14,7 +15,18 @@ import (
|
|||||||
|
|
||||||
const apiURL = "https://snapshot.debian.org/mr"
|
const apiURL = "https://snapshot.debian.org/mr"
|
||||||
|
|
||||||
var Limiter = rate.NewLimiter(rate.Every(time.Second), 3)
|
var (
|
||||||
|
limiterTimeout time.Duration = time.Second
|
||||||
|
limiterBurst int = 3
|
||||||
|
|
||||||
|
Limiter = rate.NewLimiter(rate.Every(limiterTimeout), limiterBurst)
|
||||||
|
)
|
||||||
|
|
||||||
|
func lowerLimit() {
|
||||||
|
limiterTimeout = limiterTimeout * 2
|
||||||
|
log.Info().Msgf("limiter timeout set to %v", limiterTimeout)
|
||||||
|
Limiter.SetLimit(rate.Every(limiterTimeout))
|
||||||
|
}
|
||||||
|
|
||||||
// Retries in case of 5xx errors
|
// Retries in case of 5xx errors
|
||||||
var Retries = 10
|
var Retries = 10
|
||||||
@ -90,6 +102,11 @@ func getJson(query string, target interface{}) (err error) {
|
|||||||
flog.Trace().Msg("start")
|
flog.Trace().Msg("start")
|
||||||
resp, err = http.Get(query)
|
resp, err = http.Get(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "reset by peer") {
|
||||||
|
flog.Debug().Err(err).Msg("")
|
||||||
|
lowerLimit()
|
||||||
|
continue
|
||||||
|
}
|
||||||
flog.Error().Err(err).Msg("")
|
flog.Error().Err(err).Msg("")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user