Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
| 8 | import kotlinx.coroutines.experimental.timeunit.* |
| 9 | import kotlin.coroutines.experimental.* |
| 10 | |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 11 | /** |
| 12 | * Mode for [ticker] function. |
| 13 | */ |
| 14 | enum class TickerMode { |
| 15 | /** |
| 16 | * Adjust delay to maintain fixed period if consumer cannot keep up or is otherwise slow. |
| 17 | * **This is a default mode.** |
| 18 | * |
| 19 | * ``` |
| 20 | * val channel = ticker(delay = 100) |
| 21 | * delay(350) // 250 ms late |
| 22 | * println(channel.poll()) // prints Unit |
| 23 | * println(channel.poll()) // prints null |
| 24 | * |
| 25 | * delay(50) |
| 26 | * println(channel.poll()) // prints Unit, delay was adjusted |
| 27 | * delay(50) |
| 28 | * println(channel.poll()) // prints null, we'are not late relatively to previous element |
| 29 | * ``` |
| 30 | */ |
| 31 | FIXED_PERIOD, |
| 32 | |
| 33 | /** |
| 34 | * Maintains fixed delay between produced elements if consumer cannot keep up or it otherwise slow. |
| 35 | */ |
| 36 | FIXED_DELAY |
| 37 | } |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 38 | |
| 39 | /** |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 40 | * Creates a channel that produces the first item after the given initial delay and subsequent items with the |
| 41 | * given delay between them. |
| 42 | * |
| 43 | * The resulting channel is a [rendezvous channel][RendezvousChannel]. When receiver from this channel does not keep |
| 44 | * up with receiving the elements from this channel, they are not being being send due to backpressure. The actual |
| 45 | * timing behavior of ticker in this case is controlled by [mode] parameter which |
| 46 | * is set to [TickerMode.FIXED_PERIOD] by default. See [TickerMode] for other details. |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 47 | * |
| 48 | * This channel stops producing elements immediately after [ReceiveChannel.cancel] invocation. |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 49 | * |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 50 | * **Note** producer to this channel is dispatched via [Unconfined] dispatcher by default and started eagerly. |
| 51 | * |
| 52 | * @param delay delay between each element. |
| 53 | * @param unit unit of time that applies to [initialDelay] and [delay] (in milliseconds by default). |
| 54 | * @param initialDelay delay after which the first element will be produced (it is equal to [delay] by default). |
| 55 | * @param context context of the producing coroutine. |
| 56 | * @param mode specifies behavior when elements are not received ([FIXED_PERIOD][TickerMode.FIXED_PERIOD] by default). |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 57 | */ |
| 58 | public fun ticker( |
| 59 | delay: Long, |
| 60 | unit: TimeUnit = TimeUnit.MILLISECONDS, |
| 61 | initialDelay: Long = delay, |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 62 | context: CoroutineContext = EmptyCoroutineContext, |
| 63 | mode: TickerMode = TickerMode.FIXED_PERIOD |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 64 | ): ReceiveChannel<Unit> { |
| 65 | require(delay >= 0) { "Expected non-negative delay, but has $delay" } |
| 66 | require(initialDelay >= 0) { "Expected non-negative initial delay, but has $initialDelay" } |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 67 | return produce(Unconfined + context, capacity = 0) { |
| 68 | when(mode) { |
| 69 | TickerMode.FIXED_PERIOD -> fixedPeriodTicker(delay, unit, initialDelay, channel) |
| 70 | TickerMode.FIXED_DELAY -> fixedDelayTicker(delay, unit, initialDelay, channel) |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 71 | } |
| 72 | } |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 75 | private suspend fun fixedPeriodTicker( |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 76 | delay: Long, |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 77 | unit: TimeUnit, |
| 78 | initialDelay: Long, |
| 79 | channel: SendChannel<Unit> |
| 80 | ) { |
| 81 | var deadline = timeSource.nanoTime() + unit.toNanos(initialDelay) |
| 82 | delay(initialDelay, unit) |
| 83 | val delayNs = unit.toNanos(delay) |
| 84 | while (true) { |
| 85 | deadline += delayNs |
| 86 | channel.send(Unit) |
| 87 | val now = timeSource.nanoTime() |
| 88 | val nextDelay = (deadline - now).coerceAtLeast(0) |
| 89 | if (nextDelay == 0L && delayNs != 0L) { |
| 90 | val adjustedDelay = delayNs - (now - deadline) % delayNs |
| 91 | deadline = now + adjustedDelay |
| 92 | delay(adjustedDelay, java.util.concurrent.TimeUnit.NANOSECONDS) |
| 93 | } else { |
| 94 | delay(nextDelay, java.util.concurrent.TimeUnit.NANOSECONDS) |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 95 | } |
| 96 | } |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 97 | } |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 98 | |
Roman Elizarov | b5328a7 | 2018-06-06 18:31:21 +0300 | [diff] [blame] | 99 | private suspend fun fixedDelayTicker( |
| 100 | delay: Long, |
| 101 | unit: TimeUnit, |
| 102 | initialDelay: Long, |
| 103 | channel: SendChannel<Unit> |
| 104 | ) { |
| 105 | delay(initialDelay, unit) |
| 106 | while (true) { |
| 107 | channel.send(Unit) |
| 108 | delay(delay, unit) |
| 109 | } |
Vsevolod Tolstopyatov | 03d2ff7 | 2018-05-29 17:28:20 +0300 | [diff] [blame] | 110 | } |