Class RateMeter


  • public class RateMeter
    extends java.lang.Object
    A rate limiting meter that works by granting tickets. The tickets are closable - do not leak tickets. The tickets are auto-closable so should be granted in a try with resources:
     int duration = 30000;
     int queueSize = 5;
     RateMeter instance = new RateMeter(queueSize, duration);
     try (RateMeter.Ticket t = instance.getTicket()) {
         // do something
     }
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  RateMeter.Ticket  
    • Constructor Summary

      Constructors 
      Constructor Description
      RateMeter​(int quantity, long durationMilliseconds)
      Creates a new rate meter to limit how quickly an operation can take place.
    • Constructor Detail

      • RateMeter

        public RateMeter​(int quantity,
                         long durationMilliseconds)
        Creates a new rate meter to limit how quickly an operation can take place.
        Parameters:
        quantity - the number of tickets available
        durationMilliseconds - the duration of a ticket in milliseconds
    • Method Detail

      • getTicket

        public RateMeter.Ticket getTicket()
                                   throws java.lang.InterruptedException
        Grants a ticket to proceed given the rate limit. Tickets are closable - do not leak tickets; remember to close them.
        Returns:
        a ticket
        Throws:
        java.lang.InterruptedException - thrown if interrupted
      • getQuantity

        public int getQuantity()
      • setQuantity

        public void setQuantity​(int quantity)
      • getDurationMilliseconds

        public long getDurationMilliseconds()
      • setDurationMilliseconds

        public void setDurationMilliseconds​(long durationMilliseconds)