(fix): polling retry-after
(tests): polling rate-limit (doc): changelog update
This commit is contained in:
+11
-1
@@ -255,6 +255,12 @@ func (r TelegramRequest[R, P]) doRequest(ctx context.Context, api *API) (R, erro
|
||||
}
|
||||
|
||||
if !response.Ok {
|
||||
responseErr := &ResponseError{
|
||||
Code: response.ErrorCode,
|
||||
Description: response.Description,
|
||||
Parameters: response.Parameters,
|
||||
}
|
||||
|
||||
// Handle rate limiting (429)
|
||||
if response.ErrorCode == 429 && response.Parameters != nil && response.Parameters.RetryAfter != nil {
|
||||
after := *response.Parameters.RetryAfter
|
||||
@@ -269,6 +275,10 @@ func (r TelegramRequest[R, P]) doRequest(ctx context.Context, api *API) (R, erro
|
||||
}
|
||||
}
|
||||
|
||||
if r.method == "getUpdates" {
|
||||
return zero, responseErr
|
||||
}
|
||||
|
||||
// Wait and retry
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -279,7 +289,7 @@ func (r TelegramRequest[R, P]) doRequest(ctx context.Context, api *API) (R, erro
|
||||
}
|
||||
|
||||
// Other API errors
|
||||
return zero, fmt.Errorf("[%d] %s", response.ErrorCode, response.Description)
|
||||
return zero, responseErr
|
||||
}
|
||||
|
||||
return response.Result, nil
|
||||
|
||||
+19
-1
@@ -1,6 +1,9 @@
|
||||
package tgapi
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ErrPoolUnexpected reports an unexpected result type returned from the worker pool.
|
||||
var ErrPoolUnexpected = errors.New("unexpected response from pool")
|
||||
@@ -10,3 +13,18 @@ var ErrPoolQueueFull = errors.New("worker pool queue full")
|
||||
|
||||
// ErrPoolStopped reports that a request was submitted after the worker pool stopped.
|
||||
var ErrPoolStopped = errors.New("worker pool stopped")
|
||||
|
||||
// ResponseError reports an unsuccessful Telegram API response.
|
||||
type ResponseError struct {
|
||||
Code int
|
||||
Description string
|
||||
Parameters *ResponseParameters
|
||||
}
|
||||
|
||||
// Error returns the Telegram API error code and description.
|
||||
func (e *ResponseError) Error() string {
|
||||
if e == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
return fmt.Sprintf("[%d] %s", e.Code, e.Description)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user