-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thji
committed
Sep 26, 2024
1 parent
38e1b22
commit dde5ae4
Showing
6 changed files
with
99 additions
and
35 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
arex-schedule-web-api/src/main/java/com/arextest/schedule/common/RateLimiterFactory.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,54 @@ | ||
package com.arextest.schedule.common; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* the rate limiter factory | ||
* @author thji | ||
* @date 2024/8/27 | ||
* @since 1.0.0 | ||
*/ | ||
@AllArgsConstructor | ||
public class RateLimiterFactory { | ||
|
||
private final int singleCaseTasks; | ||
private final double errorBreakRatio; | ||
private final int continuousFailThreshold; | ||
private final int replaySendMaxQps; | ||
|
||
private Map<String, SendSemaphoreLimiter> rateLimiterMap = new HashMap<>(); | ||
|
||
public RateLimiterFactory(int singleCaseTasks, double errorBreakRatio, | ||
int continuousFailThreshold, int replaySendMaxQps) { | ||
this.singleCaseTasks = singleCaseTasks; | ||
this.errorBreakRatio = errorBreakRatio; | ||
this.continuousFailThreshold = continuousFailThreshold; | ||
this.replaySendMaxQps = replaySendMaxQps; | ||
} | ||
|
||
public SendSemaphoreLimiter getRateLimiter(String host) { | ||
if (StringUtils.isEmpty(host)) { | ||
return null; | ||
} | ||
return rateLimiterMap.computeIfAbsent(host, k -> { | ||
SendSemaphoreLimiter sendSemaphoreLimiter = new SendSemaphoreLimiter( | ||
replaySendMaxQps, 1); | ||
sendSemaphoreLimiter.setTotalTasks(singleCaseTasks); | ||
sendSemaphoreLimiter.setErrorBreakRatio(errorBreakRatio); | ||
sendSemaphoreLimiter.setContinuousFailThreshold(continuousFailThreshold); | ||
sendSemaphoreLimiter.setHost(host); | ||
return sendSemaphoreLimiter; | ||
} | ||
); | ||
} | ||
|
||
public Collection<SendSemaphoreLimiter> getAll() { | ||
return Collections.unmodifiableCollection(rateLimiterMap.values()); | ||
} | ||
|
||
} |
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