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

fix: Apply default rate limit #660

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/sentry_ratelimiter.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ sentry__rate_limiter_update_from_http_retry_after(
return true;
}

bool
sentry__rate_limiter_update_from_429(sentry_rate_limiter_t *rl)
{
rl->disabled_until[SENTRY_RL_CATEGORY_ANY]
= sentry__monotonic_time() + 60 * 1000;
return true;
}

bool
sentry__rate_limiter_is_disabled(const sentry_rate_limiter_t *rl, int category)
{
Expand Down
6 changes: 6 additions & 0 deletions src/sentry_ratelimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ bool sentry__rate_limiter_update_from_header(
bool sentry__rate_limiter_update_from_http_retry_after(
sentry_rate_limiter_t *rl, const char *retry_after);

/**
* This will update the rate limiters internal state based on receiving a 429
* status code.
*/
bool sentry__rate_limiter_update_from_429(sentry_rate_limiter_t *rl);

/**
* This will return `true` if the specified `category` is currently rate
* limited.
Expand Down
5 changes: 5 additions & 0 deletions src/transports/sentry_transport_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,17 @@ sentry__curl_send_task(void *_envelope, void *_state)
CURLcode rv = curl_easy_perform(curl);

if (rv == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

if (info.x_sentry_rate_limits) {
sentry__rate_limiter_update_from_header(
state->ratelimiter, info.x_sentry_rate_limits);
} else if (info.retry_after) {
sentry__rate_limiter_update_from_http_retry_after(
state->ratelimiter, info.retry_after);
} else if (response_code == 429) {
sentry__rate_limiter_update_from_429(state->ratelimiter);
}
} else {
SENTRY_WARNF(
Expand Down
10 changes: 10 additions & 0 deletions src/transports/sentry_transport_winhttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ sentry__winhttp_send_task(void *_envelope, void *_state)
// lets just assume we won’t have headers > 2k
wchar_t buf[2048];
DWORD buf_size = sizeof(buf);

DWORD status_code = 0;
DWORD status_code_size = sizeof(status_code);

if (WinHttpQueryHeaders(state->request, WINHTTP_QUERY_CUSTOM,
L"x-sentry-rate-limits", buf, &buf_size,
WINHTTP_NO_HEADER_INDEX)) {
Expand All @@ -251,6 +255,12 @@ sentry__winhttp_send_task(void *_envelope, void *_state)
state->ratelimiter, h);
sentry_free(h);
}
} else if (WinHttpQueryHeaders(state->request,
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &status_code,
&status_code_size, WINHTTP_NO_HEADER_INDEX)
&& status_code == 429) {
sentry__rate_limiter_update_from_429(state->ratelimiter);
}
} else {
SENTRY_DEBUGF(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_integration_ratelimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ def test_rate_limits(cmake, httpserver):
)
run(tmp_path, "sentry_example", ["log", "capture-multiple"], check=True, env=env)
assert len(httpserver.log) == 2


def test_only_429(cmake, httpserver):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})

env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))

httpserver.expect_oneshot_request("/api/123456/envelope/").respond_with_data(
"OK", 429
)
run(tmp_path, "sentry_example", ["log", "capture-multiple"], check=True, env=env)
assert len(httpserver.log) == 1