-
Notifications
You must be signed in to change notification settings - Fork 14
rate limiter in logger, opt-in #62
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
Open
c-cube
wants to merge
5
commits into
master
Choose a base branch
from
sc/rate-limit-in-log
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| open Devkit | ||
|
|
||
| let fail expected actual = | ||
| Printf.eprintf "expected:\n%s\nactual:\n%s\n" expected actual; | ||
| exit 1 | ||
|
|
||
| let expect_invalid_rate rate = | ||
| match Control.Rate_limit.create ~allowed_per_sec:rate () with | ||
| | exception Invalid_argument _ -> () | ||
| | _ -> fail "Invalid_argument" "rate limiter created" | ||
|
|
||
| let expect_invalid_capacity burst_capacity = | ||
| match Control.Rate_limit.create ~burst_capacity ~allowed_per_sec:1. () with | ||
| | exception Invalid_argument _ -> () | ||
| | _ -> fail "Invalid_argument" "rate limiter created" | ||
|
|
||
| let logging_lines count = | ||
| let rec loop i acc = | ||
| if i < 0 then acc else loop (i - 1) (Printf.sprintf "logging %d" i :: acc) | ||
| in | ||
| loop (count - 1) [] | ||
|
|
||
| let () = | ||
| List.iter expect_invalid_rate [0.; -1.; infinity; nan]; | ||
| List.iter expect_invalid_capacity [0; -1]; | ||
|
|
||
| (* A very low rate must still have capacity for its initial token. *) | ||
| let slow = Control.Rate_limit.create ~allowed_per_sec:0.01 () in | ||
| if not (Control.Rate_limit.attempt slow) then fail "allowed" "rate limited"; | ||
| if Control.Rate_limit.attempt slow then fail "rate limited" "allowed"; | ||
| if Control.Rate_limit.take_rate_limited_count slow <> 1 then | ||
| fail "one rate-limited attempt" "unexpected count"; | ||
| if Control.Rate_limit.take_rate_limited_count slow <> 0 then | ||
| fail "reset rate-limited count" "non-zero count"; | ||
|
|
||
| let output = Buffer.create 256 in | ||
| let target = { Logger. | ||
| format = (fun _level _facility _timestamp _pairs message -> message); | ||
| output = (fun _level _facility message -> | ||
| Buffer.add_string output message; | ||
| Buffer.add_char output '\n'); | ||
| } in | ||
| let logger = Logger.put_simple target in | ||
| let log = new Log.logger ~logger (Log.facility "rate-limit-test") in | ||
| let rate_limit = Control.Rate_limit.create ~burst_capacity:7 ~allowed_per_sec:2. () in | ||
| let emit count = | ||
| for i = 0 to count - 1 do | ||
| log#info ~rate_limit "logging %d" i | ||
| done | ||
| in | ||
| emit 10_000; | ||
| Unix.sleep 2; | ||
| (* Emit only the number guaranteed to have been refilled. This keeps a | ||
| delayed test process from changing the expected output. *) | ||
| emit 4; | ||
| let expected = | ||
| String.concat "\n" | ||
| (logging_lines 7 @ | ||
| ["(9993 messages have been rate limited)"] @ | ||
| logging_lines 4 @ [""]) | ||
| in | ||
| let actual = Buffer.contents output in | ||
| if actual <> expected then fail expected actual |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it means changes to State will not matter once logger object is created? which goes against the purpose of the state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently State.logger is already not changeable (only hooks and some internal things are). It hasn't really been changeable in a while, as far as I can tell. The override is only used for testing here, for all intent and purpose we always use
Log.State.loggereverywhere.