/** * Constructs a new {@link RateLimiterExecutor}. Tasks will be scheduled on the provided * scheduler, so it is assumed that the scheduler will have enough threads to handle the * average permit amount per task, per second. * <p> * This constructor accepts a maximum schedule delay. If a task requires being scheduled out * beyond this delay, then the provided {@link RejectedExecutionHandler} will be invoked. * * @since 4.8.0 * @param scheduler scheduler to schedule/execute tasks on * @param permitsPerSecond how many permits should be allowed per second * @param maxScheduleDelayMillis Maximum amount of time delay tasks in order to maintain rate * @param rejectedExecutionHandler Handler to accept tasks which could not be executed */ public RateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis, RejectedExecutionHandler rejectedExecutionHandler) { ArgumentVerifier.assertNotNull(scheduler, "scheduler"); this.scheduler = scheduler; if (rejectedExecutionHandler == null) { rejectedExecutionHandler = RejectedExecutionHandler.THROW_REJECTED_EXECUTION_EXCEPTION; } this.rejectedExecutionHandler = rejectedExecutionHandler; this.permitLock = new Object(); this.lastScheduleTime = Clock.lastKnownForwardProgressingMillis(); setPermitsPerSecond(permitsPerSecond); setMaxScheduleDelayMillis(maxScheduleDelayMillis); }
/** * Constructs a new {@link RateLimiterExecutor}. Tasks will be scheduled on the provided * scheduler, so it is assumed that the scheduler will have enough threads to handle the * average permit amount per task, per second. * <p> * This constructor accepts a maximum schedule delay. If a task requires being scheduled out * beyond this delay, then the provided {@link RejectedExecutionHandler} will be invoked. * * @since 4.8.0 * @param scheduler scheduler to schedule/execute tasks on * @param permitsPerSecond how many permits should be allowed per second * @param maxScheduleDelayMillis Maximum amount of time delay tasks in order to maintain rate * @param rejectedExecutionHandler Handler to accept tasks which could not be executed */ public RateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis, RejectedExecutionHandler rejectedExecutionHandler) { ArgumentVerifier.assertNotNull(scheduler, "scheduler"); this.scheduler = scheduler; if (rejectedExecutionHandler == null) { rejectedExecutionHandler = RejectedExecutionHandler.THROW_REJECTED_EXECUTION_EXCEPTION; } this.rejectedExecutionHandler = rejectedExecutionHandler; this.permitLock = new Object(); this.lastScheduleTime = Clock.lastKnownForwardProgressingMillis(); setPermitsPerSecond(permitsPerSecond); setMaxScheduleDelayMillis(maxScheduleDelayMillis); }