Class RateLimit<A>
java.lang.Object
com.jnape.palatable.lambda.functions.builtin.fn4.RateLimit<A>
- Type Parameters:
A
- theIterable
element type
- All Implemented Interfaces:
Fn1<Fn0<Instant>,
,Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>> Fn2<Fn0<Instant>,
,Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>> Fn3<Fn0<Instant>,
,Long, Duration, Fn1<Iterable<A>, Iterable<A>>> Fn4<Fn0<Instant>,
,Long, Duration, Iterable<A>, Iterable<A>> Applicative<Fn1<Long,
,Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>> Cartesian<Fn0<Instant>,
,Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<?, ?>> Cocartesian<Fn0<Instant>,
,Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<?, ?>> Contravariant<Fn0<Instant>,
,Profunctor<?, Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<?, ?>>> Functor<Fn1<Long,
,Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>> Profunctor<Fn0<Instant>,
,Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<?, ?>> Monad<Fn1<Long,
,Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>> MonadReader<Fn0<Instant>,
,Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>> MonadRec<Fn1<Long,
,Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>> MonadWriter<Fn0<Instant>,
Fn1<Long, Fn1<Duration, Fn1<Iterable<A>, Iterable<A>>>>, Fn1<Fn0<Instant>, ?>>
public final class RateLimit<A>
extends Object
implements Fn4<Fn0<Instant>,Long,Duration,Iterable<A>,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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
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 Details
-
INSTANCE
-
-
Constructor Details
-
RateLimit
private RateLimit()
-
-
Method Details