Class ThrottledMeter

  • All Implemented Interfaces:
    ResourceMeter, ResourceRequest


    public class ThrottledMeter
    extends NotifyingMeter
    A ThrottledMeter with bandwidth limit, an approver callback for requests and allocates from an optional parent. The rate is limited by the throttle limit and is settable at any time. The rate-per-second is the amount that can be approved each second. An amount not used in each second is accumulated up to a maximum of one seconds.
    Since:
    8u40
    • Method Detail

      • create

        public static ThrottledMeter create​(ResourceType type,
                                            long ratePerSec,
                                            ResourceApprover approver)
        Create a new meter with a throttle rate and approver.
        Parameters:
        type - the ResourceType
        ratePerSec - the rate allowed for Throttling; bytes-per sec; must be greater than zero
        approver - the ResourceApprover to be notified; may be null
        Returns:
        a new ThrottledMeter
        Throws:
        IllegalArgumentException - if ratePerSec is less than or equal to zero
      • create

        public static ThrottledMeter create​(ResourceType type,
                                            ResourceRequest parent,
                                            ResourceApprover approver)
        Create a new meter with unlimited rate, parent and approver.
        Parameters:
        type - the ResourceType
        parent - a ResourceRequest to request from; may be null
        approver - the ResourceApprover to be notified; may be null
        Returns:
        a new ThrottledMeter
        Throws:
        IllegalArgumentException - if ratePerSec is less than or equal to zero
      • create

        public static ThrottledMeter create​(ResourceType type,
                                            long ratePerSec,
                                            ResourceRequest parent,
                                            ResourceApprover approver)
        Create a new meter with a throttle rate, parent and approver.
        Parameters:
        type - the ResourceType
        ratePerSec - the rate allowed for throttling; bytes-per sec; must be greater than zero
        parent - a ResourceRequest to request from; may be null
        approver - the ResourceApprover to be notified; may be null
        Returns:
        a new ThrottledMeter
        Throws:
        IllegalArgumentException - if ratePerSec is less than or equal to zero
      • validate

        public long validate​(long previous,
                             long amount,
                             ResourceId id)
        Validates the amount. If returning an amount, just return it. Throttling is achieved by an algorithm similar to the token bucket. An allowance is maintained of the amount than can be immediately fulfilled to satisfy a request. The allowance maximum is two seconds worth at the current rate-per-second. Requests reduce the allowance by the amount of the request. If the allowance is initially positive, the request is fulfilled immediately. If the allowance is negative, the allowance is topped up based on the elapsed time, waiting until it is positive again. If multiple threads are contending for the allowance, normal thread scheduling and synchronization primitives influence the order.

        If an InterruptedException is caught, zero is returned, failing the validation.

        The throttling of resource use is decoupled from the underlying resource implementation and only delays the start of resource consumption not the rate of consumption.

        Overrides:
        validate in class NotifyingMeter
        Parameters:
        amount - the amount requested
        id - the ResourceId for the resource instance; may be null
        previous - the previous meter value
        Returns:
        the value approved
      • getCurrentRate

        public final long getCurrentRate​()
        Returns an estimate of the current rate in amount per second.
        Returns:
        an estimate of the current rate in amount per second
      • getRatePerSec

        public final long getRatePerSec​()
        Returns the current throttle rate per second.
        Returns:
        the current throttle rate per second
      • setRatePerSec

        public final long setRatePerSec​(long ratePerSec)
        Sets the throttle rate per second.
        Parameters:
        ratePerSec - the rate per second; must be greater than zero
        Returns:
        the previous throttle rate per second
        Throws:
        IllegalArgumentException - if ratePerSec is less than or equal to zero
      • toString

        public String toString​()
        Description copied from class: SimpleMeter
        Returns a string representation of the meter.
        Overrides:
        toString in class SimpleMeter
        Returns:
        a string representation of the meter