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

Logger API: Add rate limiting #1142

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
16 changes: 16 additions & 0 deletions packages/playground/website/public/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

header('Content-Type: application/json');

/**
* Rate limit logger requests
*/
$max_requests_per_hour = 5;

session_start();
$rate_limit_key = md5('logger_requests_' . date('Y-m-d H'));
if (!isset($_SESSION[$rate_limit_key])) {
$_SESSION[$rate_limit_key] = 0;
}
$_SESSION[$rate_limit_key]++;

if ($_SESSION[$rate_limit_key] > $max_requests_per_hour) {
response(false, 'Too many requests');
Copy link
Member

Choose a reason for hiding this comment

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

would it be helpful to send a 429 here instead of what I presume is a 200?

also, these response() responses seem quite sparse. maybe we could help clue-in the browsers to what is happening?

header( 'Content-type: application/json', true, 429 );

Do we expect the clients to heed this response and wait to send new logs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This API is currently used only by one modal and it shows the same error message whenever a request fails, so it's not needed today.

If the API gets used more in the future, using the correct response code and improving errors would be good.

Do we expect the clients to heed this response and wait to send new logs?

No, users might click on the form again and if it fails, it would result in the same error.
Thinking about it now, the UX isn't great, but let's see how much it will be used before investing more time into it.

}

$channel = getenv('SLACK_CHANNEL');
$token = getenv('SLACK_TOKEN');

Expand Down
Loading