Class RateLimit<A>
- java.lang.Object
-
- com.jnape.palatable.lambda.functions.builtin.fn4.RateLimit<A>
-
- Type Parameters:
A- theIterableelement 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 anFn0ofInstants(presumably backed by a clock), alimit, aDuration, and anIterableas, return anIterablethat iteratesasaccording to the threshold specified by the limit per duration, using theFn0to advance time.As an example, the following will print at most 10 elements per second:
Currying allows different rate limits to be combined naturally:rateLimit(Clock.systemUTC()::instant, 10L, Duration.ofSeconds(1), iterate(x -> x + 1, 1)) .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.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);If the host
Threadisinterruptedwhile the returnedIterableis waiting for the next available time slice, anIterationInterruptedExceptionwill immediately be thrown.Note that the returned
Iterablewill 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 originalIterable.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateRateLimit()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Iterable<A>checkedApply(Fn0<java.time.Instant> instantFn0, java.lang.Long limit, java.time.Duration duration, java.lang.Iterable<A> as)static <A> RateLimit<A>rateLimit()static <A> Fn3<java.lang.Long,java.time.Duration,java.lang.Iterable<A>,java.lang.Iterable<A>>rateLimit(Fn0<java.time.Instant> instantFn0)static <A> Fn2<java.time.Duration,java.lang.Iterable<A>,java.lang.Iterable<A>>rateLimit(Fn0<java.time.Instant> instantFn0, java.lang.Long limit)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)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)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.jnape.palatable.lambda.functions.Fn1
andThen, carry, cartesian, censor, choose, cocartesian, diMap, diMapR, discardL, flatMap, fmap, lazyZip, listens, local, pure, self, thunk, toFunction, trampolineM, zip, zip
-
Methods inherited from interface com.jnape.palatable.lambda.functions.Fn2
checkedApply, toBiFunction
-
Methods inherited from interface com.jnape.palatable.lambda.functions.Fn3
checkedApply
-
-
-
-
Field Detail
-
INSTANCE
private static final RateLimit<?> INSTANCE
-
-
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:
checkedApplyin interfaceFn4<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)
-
-