Class SimpleTimeBasedGuard
java.lang.Object
ch.qos.logback.core.util.SimpleTimeBasedGuard
A simple time-based guard that limits the number of allowed operations within a sliding time window.
This class is useful for rate limiting or preventing excessive actions over time periods.
It supports time injection for testing purposes.
- Since:
- 1.5.22
- Author:
- Ceki Gülcü
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of allows per window: 2.static final longDefault window duration in milliseconds: 30 minutes. -
Constructor Summary
ConstructorsConstructorDescriptionConvenience: uses defaults — 2 allows every 30 minutesSimpleTimeBasedGuard(long windowDurationMs, int maxAllows) Creates a guard with custom limits. -
Method Summary
Modifier and TypeMethodDescriptionbooleanallow()Checks if an operation is allowed based on the current time window.voidClears the artificial time, reverting to usingSystem.currentTimeMillis().intReturns the number of allows remaining in the current window.intReturns the number of allows used in the current window.intReturns the maximum number of allows per window.longReturns the number of milliseconds until the next window starts.longReturns the window duration in milliseconds.voidsetCurrentTimeMillis(long timestamp) Sets the artificial current time for testing purposes.
-
Field Details
-
DEFAULT_WINDOW_MS
Default window duration in milliseconds: 30 minutes.- See Also:
-
DEFAULT_MAX_ALLOWS
Default maximum number of allows per window: 2.- See Also:
-
-
Constructor Details
-
SimpleTimeBasedGuard
Creates a guard with custom limits.- Parameters:
windowDurationMs- how many millis per window (e.g. 30_000 for 30 minutes)maxAllows- how many allows per window (e.g. 2)
-
SimpleTimeBasedGuard
public SimpleTimeBasedGuard()Convenience: uses defaults — 2 allows every 30 minutes
-
-
Method Details
-
allow
Checks if an operation is allowed based on the current time window. If allowed, increments the usage count for the current window. If the window has expired, resets the window and allows the operation.- Returns:
- true if the operation is allowed, false otherwise
-
setCurrentTimeMillis
Sets the artificial current time for testing purposes. When set,currentTimeMillis()will return this value instead ofSystem.currentTimeMillis().- Parameters:
timestamp- the artificial timestamp in milliseconds
-
clearCurrentTime
Clears the artificial time, reverting to usingSystem.currentTimeMillis(). -
getAllowsUsed
Returns the number of allows used in the current window.- Returns:
- the number of allows used
-
getAllowsRemaining
Returns the number of allows remaining in the current window.- Returns:
- the number of allows remaining
-
getWindowDuration
Returns the window duration in milliseconds.- Returns:
- the window duration in milliseconds
-
getMaxAllows
Returns the maximum number of allows per window.- Returns:
- the maximum number of allows
-
getMillisUntilNextWindow
Returns the number of milliseconds until the next window starts. If no window has started yet, returns the full window duration.- Returns:
- milliseconds until next window
-