Skip to content

Commit

Permalink
improve refill logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Jan 6, 2025
1 parent 2d365d8 commit e908496
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,31 @@ internal class ConnectionPool(
}
}

private tailrec suspend fun refill(attempt: Int = 1) {
private suspend fun refill() {
val cfg = client.cfg.reconnectionStrategy
if (cfg.reconnectAttempts <= 0) return
var attempt = 0
var ex: Throwable? = null
if (cfg.reconnectAttempts >= attempt) {
if (ex == null) logger.warn("Connection refills failed, maximum attempts reached")
else logger.warn("Connection refills failed, maximum attempts reached", ex)
return

while (attempt < cfg.reconnectAttempts) {
attempt++
logger.trace("Refilling ConnectionPool. Attempt $attempt")
runCatching { createConn() }
.onSuccess {
connections.send(it)
logger.trace("Connection refilled with $it")
return
}.onFailure {
if (ex != null) ex.addSuppressed(it) else ex = it
}

logger.debug("Connection refill failed, remaining attempts: ${cfg.reconnectAttempts - attempt}")
delay(attempt * cfg.reconnectDelay)
}
delay(attempt * cfg.reconnectDelay)
logger.trace("Refilling ConnectionPool. Attempt $attempt")
runCatching { createConn() }
.onSuccess {
connections.send(it)
logger.trace("Connection refilled with $it")
return
}.onFailure {
if (ex != null) ex.addSuppressed(it) else ex = it
}
logger.debug("Connection refill failed, remaining attempts: ${cfg.reconnectAttempts - attempt}")
refill(attempt + 1)

val logMsg = "Connection refills failed, maximum attempts reached"
if (ex == null) logger.warn(logMsg)
else logger.warn(logMsg, ex)
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down

0 comments on commit e908496

Please sign in to comment.