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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: IntensityTracker properly returns error when limit is exceeded #55

Merged
merged 3 commits into from
Feb 15, 2024

Conversation

kodumbeats
Copy link
Contributor

@kodumbeats kodumbeats commented Feb 10, 2024

Summary: The IntensityTracker does not short-circuit due to a logic bug in recursion.

Expected behavior: If I set a limit of 5 restarts per minute, I expect six restarts to throw the error TooIntense

Actual behavior: The error does not throw, no matter how many times the supervisor restarts a child.

Explanation:

On the first event sent to the intensity_tracker:

now == event

So the subsequent logical check into recursion:

now >= event + period

always evaluated False given the default restart period. The event was always stripped from internal state, so the intensity_tracker never held more than one event at a time. The code in question:

pub fn trim_window(events: List(Int), now: Int, period: Int) -> List(Int) {
case events {
[] -> []
[event, ..events] ->
case now >= event + period {
True -> [event, ..trim_window(events, now, period)]
False -> []
}
}
}

This patch also includes tests for expected behavior. When testing, I discovered event maximum limit check was off by one, so this patch corrects and tests that edge case.

This was one of those pull-your-hair-out kind of bugs 馃悰 Happy to make any changes!

@kodumbeats kodumbeats force-pushed the fix/intensity_tracker_enforcement branch from 94d76b2 to 820036c Compare February 10, 2024 18:57
@kodumbeats kodumbeats changed the title fix: intensity_tracker errors when too_intense Fix: IntensityTracker properly returns error when limit is exceeded Feb 12, 2024
On the first event sent to the intensity_tracker, `now == event`, so
`now >= now + period` was always False, given a positive restart period.
Also, the event limit was off-by-one, so this patch corrects that edge
case.

This patch also includes tests for expected behavior.
@kodumbeats kodumbeats force-pushed the fix/intensity_tracker_enforcement branch 2 times, most recently from c9b62cb to 0698128 Compare February 13, 2024 14:02
@kodumbeats kodumbeats force-pushed the fix/intensity_tracker_enforcement branch from 0698128 to 7a9e68f Compare February 13, 2024 14:04
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you!!

@lpil lpil merged commit 946a0e9 into gleam-lang:main Feb 15, 2024
1 check passed
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.

None yet

2 participants