request/ratelimit: Add context value check and fix bug (#2073)

* Add WithNoDelayPermitted and fix bug on cancel all

* rm reservations as it is only for last reservation when cancelling and needed to take into account of the actual offset delay for correct returning of tokens, update tests

* export error

* misc fix

* more misc fix

* Add concurrent protection, cancel in reverse and add tests

* lint: fix

* Update exchanges/request/limit.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/request/limit.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/request/limit.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/request/limit.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits doo

* linter: fix

* boss king: nits

* crank: nits

* crank: test patch which was cooked and had to be done manually

* Update exchanges/request/limit.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits

* linter: fix

* thrasher: nits

* use error collector in tests

* nolint: direction

* gk: fixup!

* my life has elapsed

* thrasher-: Because of synctest, we can now be deterministic with values. This rids a lot of the redundant wait calls which served no purpose

* thrasher-: patched

---------

Co-authored-by: shazbert <ryan.oharareid@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
Co-authored-by: shazbert <shazbert@DESKTOP-3QKKR6J.localdomain>
This commit is contained in:
Ryan O'Hara-Reid
2025-11-27 11:10:11 +11:00
committed by GitHub
parent 2943a7f800
commit 719e6bebfe
4 changed files with 348 additions and 114 deletions

View File

@@ -20,7 +20,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/exchanges/nonce"
"golang.org/x/time/rate"
)
const unexpected = "unexpected values"
@@ -54,7 +53,7 @@ func TestMain(m *testing.M) {
w.WriteHeader(http.StatusGatewayTimeout)
})
sm.HandleFunc("/rate", func(w http.ResponseWriter, _ *http.Request) {
if !serverLimit.Allow() {
if !serverLimit.limiter.Allow() {
http.Error(w,
http.StatusText(http.StatusTooManyRequests),
http.StatusTooManyRequests)
@@ -70,7 +69,7 @@ func TestMain(m *testing.M) {
}
})
sm.HandleFunc("/rate-retry", func(w http.ResponseWriter, _ *http.Request) {
if !serverLimitRetry.Allow() {
if !serverLimitRetry.limiter.Allow() {
w.Header().Add("Retry-After", strconv.Itoa(int(math.Round(serverLimitInterval.Seconds()))))
http.Error(w,
http.StatusText(http.StatusTooManyRequests),
@@ -102,31 +101,6 @@ func TestMain(m *testing.M) {
os.Exit(issues)
}
func TestNewRateLimitWithWeight(t *testing.T) {
t.Parallel()
r := NewRateLimitWithWeight(time.Second*10, 5, 1)
if r.Limit() != 0.5 {
t.Fatal(unexpected)
}
// Ensures rate limiting factor is the same
r = NewRateLimitWithWeight(time.Second*2, 1, 1)
if r.Limit() != 0.5 {
t.Fatal(unexpected)
}
// Test for open rate limit
r = NewRateLimitWithWeight(time.Second*2, 0, 1)
if r.Limit() != rate.Inf {
t.Fatal(unexpected)
}
r = NewRateLimitWithWeight(0, 69, 1)
if r.Limit() != rate.Inf {
t.Fatal(unexpected)
}
}
func TestCheckRequest(t *testing.T) {
t.Parallel()
@@ -235,7 +209,7 @@ func TestDoRequest(t *testing.T) {
// Invalid/missing endpoint limit
err = r.SendPayload(ctx, Unset, func() (*Item, error) { return &Item{Path: testURL}, nil }, UnauthenticatedRequest)
require.ErrorIs(t, err, errSpecificRateLimiterIsNil)
require.ErrorIs(t, err, common.ErrNilPointer)
// Force debug
err = r.SendPayload(ctx, UnAuth, func() (*Item, error) {