fix_: handling throughput limit error from providers

This commit is contained in:
Sale Djenic 2024-05-09 14:07:41 +02:00 committed by saledjenic
parent 627e23ffa5
commit dcc93dee96
2 changed files with 36 additions and 4 deletions

View File

@ -172,7 +172,9 @@ func isVMError(err error) bool {
}
func isRPSLimitError(err error) bool {
return strings.Contains(err.Error(), "backoff_seconds")
return strings.Contains(err.Error(), "backoff_seconds") ||
strings.Contains(err.Error(), "has exceeded its throughput limit") ||
strings.Contains(err.Error(), "request rate exceeded")
}
func (c *ClientWithFallback) SetIsConnected(value bool) {
@ -216,10 +218,25 @@ func (c *ClientWithFallback) makeCall(ctx context.Context, main func() ([]any, e
if err != nil {
if isRPSLimitError(err) {
c.mainLimiter.ReduceLimit()
} else if isVMError(err) {
err = c.mainLimiter.WaitForRequestsAvailability(1)
if err != nil {
return err
}
res, err = main()
if err == nil {
resultChan <- CommandResult{res: res}
return nil
}
}
if isVMError(err) {
resultChan <- CommandResult{err: err}
return nil
}
return err
}
resultChan <- CommandResult{res: res}
@ -238,10 +255,25 @@ func (c *ClientWithFallback) makeCall(ctx context.Context, main func() ([]any, e
if err != nil {
if isRPSLimitError(err) {
c.fallbackLimiter.ReduceLimit()
} else if isVMError(err) {
err = c.fallbackLimiter.WaitForRequestsAvailability(1)
if err != nil {
return err
}
res, err = fallback()
if err == nil {
resultChan <- CommandResult{res: res}
return nil
}
}
if isVMError(err) {
resultChan <- CommandResult{err: err}
return nil
}
return err
}
resultChan <- CommandResult{res: res}

View File

@ -9,7 +9,7 @@ import (
)
const (
defaultMaxRequestsPerSecond = 100
defaultMaxRequestsPerSecond = 50
minRequestsPerSecond = 20
requestsPerSecondStep = 10