-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Aliaksandr Stsiapanay <[email protected]>
- Loading branch information
1 parent
52ba2a7
commit b3750b5
Showing
9 changed files
with
226 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/epam/aidial/core/limiter/RequestRateLimit.java
This file contains 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,39 @@ | ||
package com.epam.aidial.core.limiter; | ||
|
||
import com.epam.aidial.core.config.Limit; | ||
import com.epam.aidial.core.data.LimitStats; | ||
import com.epam.aidial.core.util.HttpStatus; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class RequestRateLimit { | ||
private final RateBucket hour = new RateBucket(RateWindow.HOUR); | ||
private final RateBucket day = new RateBucket(RateWindow.DAY); | ||
|
||
public RateLimitResult check(long timestamp, Limit limit, long count) { | ||
long hourTotal = hour.update(timestamp); | ||
long dayTotal = day.update(timestamp); | ||
|
||
boolean result = hourTotal >= limit.getRequestHour() || dayTotal >= limit.getRequestDay(); | ||
if (result) { | ||
String errorMsg = String.format(""" | ||
Hit request rate limit: | ||
- hour limit: %d / %d requests | ||
- day limit: %d / %d requests | ||
""", | ||
hourTotal, limit.getRequestHour(), dayTotal, limit.getRequestDay()); | ||
return new RateLimitResult(HttpStatus.TOO_MANY_REQUESTS, errorMsg); | ||
} else { | ||
hour.add(timestamp, count); | ||
day.add(timestamp, count); | ||
return RateLimitResult.SUCCESS; | ||
} | ||
} | ||
|
||
public void update(long timestamp, LimitStats limitStats) { | ||
long hourTotal = hour.update(timestamp); | ||
long dayTotal = day.update(timestamp); | ||
limitStats.getDayRequestStats().setUsed(dayTotal); | ||
limitStats.getHourRequestStats().setUsed(hourTotal); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.