Skip to content

Commit b3133de

Browse files
committed
Use an impossible deadline when the timeout is negative or infinite
1 parent 7b23461 commit b3133de

File tree

1 file changed

+3
-6
lines changed
  • src/main/kotlin/io/github/freya022/botcommands/internal/core

1 file changed

+3
-6
lines changed

src/main/kotlin/io/github/freya022/botcommands/internal/core/BContextImpl.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,14 @@ internal class BContextImpl internal constructor(
229229
}
230230

231231
override fun awaitShutdown(timeout: Duration): Boolean {
232-
val deadline = Clock.System.now() + timeout
232+
val deadline = Clock.System.now() + (timeout.takeIfFinite() ?: Duration.INFINITE)
233233
fun durationUntilDeadline(): Duration = deadline - Clock.System.now()
234234

235235
if (!awaitJDAShutdown(::durationUntilDeadline))
236236
return false
237237

238238
statusLock.withLock {
239239
while (status != Status.SHUTDOWN) {
240-
// TODO seems to wait until the timeout
241240
if (!statusCondition.await(durationUntilDeadline().inWholeMilliseconds, TimeUnit.MILLISECONDS)) {
242241
return false
243242
}
@@ -248,8 +247,6 @@ internal class BContextImpl internal constructor(
248247
}
249248

250249
private fun awaitJDAShutdown(durationUntilDeadlineFn: () -> Duration): Boolean {
251-
fun Duration.finiteOrZero() = takeIfFinite() ?: Duration.ZERO
252-
253250
val jda = jdaOrNull
254251
if (jda == null) {
255252
logger.debug { "Not awaiting JDA shutdown as there is no JDA instance registered" }
@@ -258,12 +255,12 @@ internal class BContextImpl internal constructor(
258255
val shardManager = jda.shardManager
259256
if (shardManager != null) {
260257
shardManager.shards.forEach { shard ->
261-
if (!shard.awaitShutdown(durationUntilDeadlineFn().finiteOrZero())) {
258+
if (!shard.awaitShutdown(durationUntilDeadlineFn())) {
262259
return false
263260
}
264261
}
265262
} else {
266-
if (!jda.awaitShutdown(durationUntilDeadlineFn().finiteOrZero())) {
263+
if (!jda.awaitShutdown(durationUntilDeadlineFn())) {
267264
return false
268265
}
269266
}

0 commit comments

Comments
 (0)