phoenix-channel / ch.kuon.phoenix / ExTimer

ExTimer

class ExTimer

Exponential backoff timer

Example from tests:

val waiter = Waiter()

var counter = 0
val timer = ExTimer({
    counter++
    waiter.resume()
}, { tries ->
    tries * 1000
})
// This is for the example, if you call
// scheduleTimeout() in a row, the last call "wins"
// This example will call the callback only once after
// 4 seconds then once again after 1 second
timer.scheduleTimeout() // fires after 1000
timer.scheduleTimeout() // fires after 2000
timer.scheduleTimeout() // fires after 3000
timer.reset()
timer.scheduleTimeout() // fires after 1000
timer.scheduleTimeout() // fires after 2000
timer.scheduleTimeout() // fires after 3000
timer.scheduleTimeout() // fires after 4000
waiter.await(5000, 1)
waiter.assertEquals(1, counter)

timer.reset()
timer.scheduleTimeout() // fires after 1000
waiter.await(2000, 1)
waiter.assertEquals(2, counter)

Constructors

<init>

Exponential backoff timer

ExTimer(callback: Callback, timerCalc: (Int) -> Int)

Properties

callback

The function called when the timer fires

var callback: Callback

timer

var timer: Timer?

timerCalc

A function that is called when the timer is scheduled.

var timerCalc: (Int) -> Int

tries

var tries: AtomicInteger

Functions

reset

Reset the timeout and cancel the timer if it is scheduled.

fun reset(): Unit

scheduleTimeout

Schedule the timer to run once

fun scheduleTimeout(): Unit