Class InvocationLimiter<T>

  • Type Parameters:
    T - result type

    @ThreadSafe
    public class InvocationLimiter<T>
    extends Object
    Limits number of invocations of some resource. Limit is enforced per an arbitrary resource key.

    Concurrent access to the same resource is allowed. In case of reaching the limit of invocation in a period of time, the result will contain information when the next invocation can be performed for this resource.

    See Also:
    InvocationLimiterResponse
    • Constructor Detail

      • InvocationLimiter

        public InvocationLimiter​(int invocationLimit,
                                 int invocationLimitUnitCount,
                                 @NotNull
                                 @NotNull TimeUnit invocationLimitUnit)
    • Method Detail

      • invoke

        @NotNull
        public <V> @NotNull InvocationLimiterResponse<V> invoke​(@NotNull
                                                                T resource,
                                                                @NotNull
                                                                @NotNull Supplier<V> invocation)
        Try to invoke a given resource. In case of reaching the maximum number of calls, the response will contain information when the next call can be done.
        Type Parameters:
        V - type of response
        Parameters:
        resource - key of the resource for which to measure invocations
        invocation - action which should be invoked
        Returns:
        wrapper with information if invocation has been performed or rejected
      • resetSettings

        public void resetSettings​(int limit,
                                  int invocationLimitUnitCount,
                                  @NotNull
                                  @NotNull TimeUnit invocationLimitUnit)
        Resets settings for limiter, removes all data about past invocation and start counting invocation from 0.
        Parameters:
        limit - new limit
        invocationLimitUnitCount - new limit unit count
        invocationLimitUnit - new limit unit