Base implementation of RateLimiter, provides runtime data maintenance mechanism monitor.
| 27 | * Base implementation of {@link RateLimiter}, provides runtime data maintenance mechanism monitor. |
| 28 | */ |
| 29 | public abstract class RateLimiterBase implements RateLimiter { |
| 30 | |
| 31 | /** |
| 32 | * Default constructor. |
| 33 | */ |
| 34 | public RateLimiterBase() { |
| 35 | } |
| 36 | |
| 37 | private static final AtomicInteger index = new AtomicInteger(); |
| 38 | |
| 39 | TimeBucketCounterBase bucketCounter; |
| 40 | |
| 41 | int requests; |
| 42 | int actualRequests; |
| 43 | |
| 44 | int duration; |
| 45 | int actualDuration; |
| 46 | |
| 47 | // Initial policy name can be rewritten by setPolicyName() |
| 48 | private String policyName = null; |
| 49 | |
| 50 | /* |
| 51 | * The self-owned utility executor, will be instantiated only when ScheduledThreadPoolExecutor is absent during |
| 52 | * filter configure phase. |
| 53 | */ |
| 54 | private ScheduledThreadPoolExecutor internalExecutorService = null; |
| 55 | |
| 56 | /** |
| 57 | * If policy name has not been specified, the first call of {@link #getPolicyName()} returns an auto-generated |
| 58 | * policy name using the default policy name as prefix and followed by auto-increase index. |
| 59 | * |
| 60 | * @return default policy name, as a prefix of auto-generated policy name. |
| 61 | */ |
| 62 | protected abstract String getDefaultPolicyName(); |
| 63 | |
| 64 | |
| 65 | @Override |
| 66 | public String getPolicyName() { |
| 67 | if (policyName == null) { |
| 68 | policyName = getDefaultPolicyName() + "-" + index.incrementAndGet(); |
| 69 | } |
| 70 | return policyName; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | @Override |
| 75 | public void setPolicyName(String name) { |
| 76 | Objects.requireNonNull(name); |
| 77 | this.policyName = name; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @Override |
| 82 | public int getDuration() { |
| 83 | return actualDuration; |
| 84 | } |
| 85 | |
| 86 |
nothing calls this directly
no outgoing calls
no test coverage detected