Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: replace ratelimiter with x/time/rate. #3155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bznein
Copy link
Collaborator

@bznein bznein commented Feb 4, 2025

Removes the ratelimiter package and replaces its
usages with x/time/rate.

@bznein
Copy link
Collaborator Author

bznein commented Feb 4, 2025

@benma I made a mess on the other PR trying to merge master into it. I opened a new one with the same diff.

I managed to test these changes also on the etherscan.go path in the same way (adding a log line and artificially limiting even further the number of calls per second) and confirmed that they do in fact happen less often.

@bznein bznein force-pushed the ratelimiting branch 2 times, most recently from f6692ae to d6b1e28 Compare February 5, 2025 09:04
@bznein
Copy link
Collaborator Author

bznein commented Feb 11, 2025

@benma friendly ping on this one :)

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested and seems to work, but left a few comments

go.mod Outdated
@@ -22,6 +22,7 @@ require (
golang.org/x/crypto v0.32.0
golang.org/x/mobile v0.0.0-20240716161057-1ad2df20a8b6
golang.org/x/net v0.34.0
golang.org/x/time v0.5.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why v0.5.0? v0.10.0 is available also (even v0.11.0 is, but that seems to require Go 1.23).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure, I think it was auto-added. Will change it

// to one of the supported exchange rates providers.
func apiRateLimit(baseURL string) time.Duration {
func apiRateLimit(baseURL string) float64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the return type rate.Limit for clarity? then the callsite does not need to convert either.

Comment on lines 130 to 131
// To stay within acceptable rate limits defined by CoinGeckoRateLimit, callers can
// use util/ratelimit package.
// use https://pkg.go.dev/golang.org/x/time/rate package.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this part? users don't have to do anything, rate limiting is includd in the RatesUpdater

@@ -275,36 +276,42 @@ func (updater *RateUpdater) updateLast(ctx context.Context) {
}

var geckoRates map[string]map[string]float64
callErr := updater.geckoLimiter.Call(ctx, "updateLast", func() error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you missed this line below in your http request, which should time out.

if callErr != nil {
updater.log.WithError(callErr).Errorf("updateLast")
if err := updater.geckoLimiter.Wait(ctx); err != nil {
updater.log.WithError(err).Error("could not wait for rate limiter")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please prefix all msgs with updateLast: like it used to be before, so the error is more easily identified in the logs.

// Etherscan rate limits to one request per 0.2 seconds.
var CallInterval = 260 * time.Millisecond
var CallsPerSec = 3.8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can unexport this, it's only used locally.

Removes the ratelimiter package and replaces its
usages with x/time/rate.
@bznein bznein requested a review from benma March 12, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants