fun mytimer() = Schedule.forever<String>().delay { duration ->
Duration.Companion.seconds(5)
}
Hi all,
What is the input argument 'duration'.. when has it a value other than 0s (in this example it is always 0s) ?
s
simon.vergauwen
11/12/2022, 10:49 AM
Hey @Jörg Winter,
It depends on the constructor used.
Schedule.forever
is the equivalent of
while(true)
.
In contrast
Schedule.spaced(5.seconds)
is
Schedule.forever
with a constant delay in between every loop of 5s.
Schedule.linear(5.seconds)
builds a linear delay schedule with an initial value of
5.seconds
.
j
Jörg Winter
11/12/2022, 11:01 AM
thx, so I got this nice contruction working
Copy code
@OptIn(ExperimentalTime::class)
fun mytimer() = Schedule.linear<Unit>(1.seconds).modify { n, duration ->
val nextDuration = duration.plus(1.seconds) // or determine next duration however you want...
nextDuration
}
Jörg Winter
11/12/2022, 11:01 AM
this would be like linear backoff
s
simon.vergauwen
11/12/2022, 1:14 PM
That is what the
linear
constructor already does. It relies on
forever
which outputs the count of it's iteration and multiplies it with the
base = 1.seconds
.
Copy code
delayed(forever<A>().map { base * it })
simon.vergauwen
11/12/2022, 1:16 PM
The idea of
Schedule
is that it already exposes most of the things you need, and you can compose different pieces together. I.e.
Schedule.linear(1.seconds).jittered() and Schedule.recurs(5)
will have a jittered linear back-off delay, with a max of 5 tries.
So if you use it as
Schedule.linear(1.seconds).jittered() and Schedule.recurs(5).retry { f() }