Class RateLimit<A>

  • Type Parameters:
    A - the Iterable element type
    All Implemented Interfaces:
    Fn1<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>>, Fn2<Fn0<java.time.Instant>,​java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>, Fn3<Fn0<java.time.Instant>,​java.lang.Long,​java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>, Fn4<Fn0<java.time.Instant>,​java.lang.Long,​java.time.Duration,​java.lang.Iterable<A>,​java.lang.Iterable<A>>, Applicative<Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>, Cartesian<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<?,​?>>, Cocartesian<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<?,​?>>, Contravariant<Fn0<java.time.Instant>,​Profunctor<?,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<?,​?>>>, Functor<Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>, Profunctor<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<?,​?>>, Monad<Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>, MonadReader<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>, MonadRec<Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>, MonadWriter<Fn0<java.time.Instant>,​Fn1<java.lang.Long,​Fn1<java.time.Duration,​Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>>>>,​Fn1<Fn0<java.time.Instant>,​?>>

    public final class RateLimit<A>
    extends java.lang.Object
    implements Fn4<Fn0<java.time.Instant>,​java.lang.Long,​java.time.Duration,​java.lang.Iterable<A>,​java.lang.Iterable<A>>
    Given an Fn0 of Instants (presumably backed by a clock), a limit, a Duration, and an Iterable as, return an Iterable that iterates as according to the threshold specified by the limit per duration, using the Fn0 to advance time.

    As an example, the following will print at most 10 elements per second:

    
     rateLimit(Clock.systemUTC()::instant, 10L, Duration.ofSeconds(1), iterate(x -> x + 1, 1))
         .forEach(System.out::println);
     
    Currying allows different rate limits to be combined naturally:
    
     Iterable<Integer> elements = iterate(x -> x + 1, 1);
    
     Supplier<Instant> instantFn0 = Clock.systemUTC()::instant;
     Fn1<Iterable<Integer>, Iterable<Integer>> tenPerSecond =
         rateLimit(instantFn0, 10L, Duration.ofSeconds(1));
     Fn1<Iterable<Integer>, Iterable<Integer>> oneHundredEveryTwoMinutes =
         rateLimit(instantFn0, 100L, Duration.ofMinutes(2));
    
     tenPerSecond.fmap(oneHundredEveryTwoMinutes).apply(elements).forEach(System.out::println);
     
    In the preceding example, the elements will be printed at most 10 elements per second and 100 elements per 120 seconds.

    If the host Thread is interrupted while the returned Iterable is waiting for the next available time slice, an IterationInterruptedException will immediately be thrown.

    Note that the returned Iterable will never iterate faster than the specified rate limit, but the earliest the next element is available will be dependent on the precision of the underlying instant supplier as well as any overhead involved in producing the element from the original Iterable.

    • Field Detail

      • INSTANCE

        private static final RateLimit<?> INSTANCE
    • Constructor Detail

      • RateLimit

        private RateLimit()
    • Method Detail

      • checkedApply

        public java.lang.Iterable<A> checkedApply​(Fn0<java.time.Instant> instantFn0,
                                                  java.lang.Long limit,
                                                  java.time.Duration duration,
                                                  java.lang.Iterable<A> as)
        Specified by:
        checkedApply in interface Fn4<Fn0<java.time.Instant>,​java.lang.Long,​java.time.Duration,​java.lang.Iterable<A>,​java.lang.Iterable<A>>
      • rateLimit

        public static <A> RateLimit<A> rateLimit()
      • rateLimit

        public static <A> Fn3<java.lang.Long,​java.time.Duration,​java.lang.Iterable<A>,​java.lang.Iterable<A>> rateLimit​(Fn0<java.time.Instant> instantFn0)
      • rateLimit

        public static <A> Fn2<java.time.Duration,​java.lang.Iterable<A>,​java.lang.Iterable<A>> rateLimit​(Fn0<java.time.Instant> instantFn0,
                                                                                                                    java.lang.Long limit)
      • rateLimit

        public static <A> Fn1<java.lang.Iterable<A>,​java.lang.Iterable<A>> rateLimit​(Fn0<java.time.Instant> instantFn0,
                                                                                           java.lang.Long limit,
                                                                                           java.time.Duration duration)
      • rateLimit

        public static <A> java.lang.Iterable<A> rateLimit​(Fn0<java.time.Instant> instantFn0,
                                                          java.lang.Long limit,
                                                          java.time.Duration duration,
                                                          java.lang.Iterable<A> as)