```fun mytimer() = Schedule.forever<String>(...
# arrow
j
Copy code
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
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
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
}
this would be like linear backoff
s
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 })
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() }
it will retry
f
5 times with a jittered linear back-off.