A
- the Iterable
element typepublic final class RateLimit<A> extends Object implements Fn4<Fn0<Instant>,Long,Duration,Iterable<A>,Iterable<A>>
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
.
Modifier and Type | Method and Description |
---|---|
Iterable<A> |
checkedApply(Fn0<Instant> instantFn0,
Long limit,
Duration duration,
Iterable<A> as) |
static <A> RateLimit<A> |
rateLimit() |
static <A> Fn3<Long,Duration,Iterable<A>,Iterable<A>> |
rateLimit(Fn0<Instant> instantFn0) |
static <A> Fn2<Duration,Iterable<A>,Iterable<A>> |
rateLimit(Fn0<Instant> instantFn0,
Long limit) |
static <A> Fn1<Iterable<A>,Iterable<A>> |
rateLimit(Fn0<Instant> instantFn0,
Long limit,
Duration duration) |
static <A> Iterable<A> |
rateLimit(Fn0<Instant> instantFn0,
Long limit,
Duration duration,
Iterable<A> as) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
apply, apply, apply, apply, checkedApply, compose, contraMap, diMapL, discardR, flip, fn4, fn4, fn4, fn4, uncurry, widen
checkedApply, fn3, fn3, fn3
checkedApply, curried, curry, fn2, fromBiFunction, toBiFunction
public Iterable<A> checkedApply(Fn0<Instant> instantFn0, Long limit, Duration duration, Iterable<A> as)
public static <A> RateLimit<A> rateLimit()
public static <A> Fn3<Long,Duration,Iterable<A>,Iterable<A>> rateLimit(Fn0<Instant> instantFn0)
public static <A> Fn2<Duration,Iterable<A>,Iterable<A>> rateLimit(Fn0<Instant> instantFn0, Long limit)
public static <A> Fn1<Iterable<A>,Iterable<A>> rateLimit(Fn0<Instant> instantFn0, Long limit, Duration duration)