Skip to content

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

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

Closed
wants to merge 1 commit into from

Conversation

bznein
Copy link
Collaborator

@bznein bznein commented Jan 21, 2025

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

@@ -50,17 +52,22 @@ const ERC20GasErr = "insufficient funds for gas * price + value"
type EtherScan struct {
url string
httpClient *http.Client
limiter 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.

usually *rate.Limiter so you don't need to dereference *rate.NewLimiter(...) below, or is there a reason for making this a value instead of a pointer?

@@ -278,38 +278,36 @@ func (updater *RateUpdater) fetchGeckoMarketRange(ctx context.Context, coin, fia
}

// Make the call, abiding the upstream rate limits.
msg := fmt.Sprintf("fetch coingecko coin=%s fiat=%s start=%s", coin, fiat, timeRange.start)
// msg := fmt.Sprintf("fetch coingecko coin=%s fiat=%s start=%s", coin, fiat, timeRange.start)
Copy link
Contributor

Choose a reason for hiding this comment

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

undo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hey yeah, the issue is that this was passed to a fn call that doesn't exist anymore, I think for logging purposes and this is a bit harder now to achieve. I was planning to find a way around this and only then open for review :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think there is a way here to have the log message show up periodically as we did before. Wait() is a blocking call and we have no way of loggin periodically as we did before.

@bznein bznein force-pushed the RateLimiting branch 3 times, most recently from 976c710 to ca3e504 Compare January 22, 2025 15:30
Copy link
Collaborator Author

@bznein bznein left a comment

Choose a reason for hiding this comment

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

@benma this is overall ready for review.

the changes are mostly on backend/rates and backend/coins/eth/etherscan

I was able to test them in rates by adding a log line whenever we make the API call and decrease massively the number of calls allowed per second.

However, as discussed offline, I currently have no way to test them on etherscan, so I won't be anyway merging this until I can.

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

Choose a reason for hiding this comment

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

~= 1/0.26 (previous duration between requests)

}
}

func (etherScan *EtherScan) call(params url.Values, result interface{}) error {
if err := etherScan.limiter.Wait(context.TODO()); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wait needs a context that we do not have here. I'm open to suggestions. FWIW we didn't have it before either

bznein

This comment was marked as duplicate.

@@ -323,7 +330,7 @@ func (etherScan *EtherScan) Transactions(
result := struct {
Result []*Transaction
}{}
if err := etherScan.call(params, &result); err != nil {
if err := etherScan.call(context.TODO(), params, &result); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the linter was forcing me to pass the context down the stack (which I think makes absolutely sense!) but there are two places (this function and another one) in which we don't have it, so I'm open to suggestions here

@bznein bznein requested a review from benma January 22, 2025 15:55
Removes the ratelimiter package and replaces its
usages with x/time/rate.
@bznein
Copy link
Collaborator Author

bznein commented Feb 4, 2025

Closing in favour of #3155

@bznein bznein closed this Feb 4, 2025
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