1717package org.funfix.delayedqueue.jvm
1818
1919import java.security.MessageDigest
20- import java.sql.SQLException
2120import java.time.Clock
2221import java.time.Instant
2322import java.util.UUID
@@ -45,8 +44,6 @@ import org.funfix.delayedqueue.jvm.internals.jdbc.sqlite.SqliteMigrations
4544import org.funfix.delayedqueue.jvm.internals.jdbc.withConnection
4645import org.funfix.delayedqueue.jvm.internals.jdbc.withDbRetries
4746import org.funfix.delayedqueue.jvm.internals.jdbc.withTransaction
48- import org.funfix.delayedqueue.jvm.internals.utils.Raise
49- import org.funfix.delayedqueue.jvm.internals.utils.unsafeSneakyRaises
5047import org.slf4j.LoggerFactory
5148
5249/* *
@@ -118,14 +115,9 @@ private constructor(
118115 * This method has Raise context for ResourceUnavailableException and InterruptedException,
119116 * which matches what the public API declares via @Throws.
120117 */
121- context(_: Raise <ResourceUnavailableException >, _: Raise <InterruptedException >)
122- private fun <T > withRetries (
123- block :
124- context(Raise <SQLException >, Raise <InterruptedException >)
125- () -> T
126- ): T {
118+ private fun <T > withRetries (block : () -> T ): T {
127119 return if (config.retryPolicy == null ) {
128- block(Raise . _PRIVATE_AND_UNSAFE , Raise . _PRIVATE_AND_UNSAFE )
120+ block()
129121 } else {
130122 withDbRetries(
131123 config = config.retryPolicy,
@@ -138,17 +130,16 @@ private constructor(
138130
139131 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
140132 override fun offerOrUpdate (key : String , payload : A , scheduleAt : Instant ): OfferOutcome =
141- unsafeSneakyRaises {
142- withRetries { offer(key, payload, scheduleAt, canUpdate = true ) }
133+ withRetries {
134+ offer(key, payload, scheduleAt, canUpdate = true )
143135 }
144136
145137 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
146138 override fun offerIfNotExists (key : String , payload : A , scheduleAt : Instant ): OfferOutcome =
147- unsafeSneakyRaises {
148- withRetries { offer(key, payload, scheduleAt, canUpdate = false ) }
139+ withRetries {
140+ offer(key, payload, scheduleAt, canUpdate = false )
149141 }
150142
151- context(_: Raise <InterruptedException >, _: Raise <SQLException >)
152143 private fun offer (
153144 key : String ,
154145 payload : A ,
@@ -232,11 +223,10 @@ private constructor(
232223
233224 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
234225 override fun <In > offerBatch (messages : List <BatchedMessage <In , A >>): List <BatchedReply <In , A >> =
235- unsafeSneakyRaises {
236- withRetries { offerBatchImpl(messages) }
226+ withRetries {
227+ offerBatchImpl(messages)
237228 }
238229
239- context(_: Raise <InterruptedException >, _: Raise <SQLException >)
240230 private fun <In > offerBatchImpl (
241231 messages : List <BatchedMessage <In , A >>
242232 ): List <BatchedReply <In , A >> {
@@ -344,25 +334,20 @@ private constructor(
344334 }
345335
346336 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
347- override fun tryPoll (): AckEnvelope <A >? = unsafeSneakyRaises { withRetries { tryPollImpl() } }
337+ override fun tryPoll (): AckEnvelope <A >? = withRetries { tryPollImpl() }
348338
349339 private fun acknowledgeByLockUuid (lockUuid : String ): AcknowledgeFun = {
350- unsafeSneakyRaises {
351- withRetries {
352- database.withTransaction { conn -> adapter.deleteRowsWithLock(conn, lockUuid) }
353- }
340+ withRetries {
341+ database.withTransaction { conn -> adapter.deleteRowsWithLock(conn, lockUuid) }
354342 }
355343 }
356344
357345 private fun acknowledgeByFingerprint (row : DBTableRowWithId ): AcknowledgeFun = {
358- unsafeSneakyRaises {
359- withRetries {
360- database.withTransaction { conn -> adapter.deleteRowByFingerprint(conn, row) }
361- }
346+ withRetries {
347+ database.withTransaction { conn -> adapter.deleteRowByFingerprint(conn, row) }
362348 }
363349 }
364350
365- context(_: Raise <InterruptedException >, _: Raise <SQLException >)
366351 private fun tryPollImpl (): AckEnvelope <A >? {
367352 // Retry loop to handle failed acquires (concurrent modifications)
368353 // This matches the original Scala implementation which retries if acquire fails
@@ -422,11 +407,10 @@ private constructor(
422407 }
423408
424409 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
425- override fun tryPollMany (batchMaxSize : Int ): AckEnvelope <List <A >> = unsafeSneakyRaises {
426- withRetries { tryPollManyImpl(batchMaxSize) }
410+ override fun tryPollMany (batchMaxSize : Int ): AckEnvelope <List <A >> = withRetries {
411+ tryPollManyImpl(batchMaxSize)
427412 }
428413
429- context(_: Raise <InterruptedException >, _: Raise <SQLException >)
430414 private fun tryPollManyImpl (batchMaxSize : Int ): AckEnvelope <List <A >> {
431415 // Handle edge case: non-positive batch size
432416 if (batchMaxSize <= 0 ) {
@@ -508,11 +492,8 @@ private constructor(
508492 }
509493
510494 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
511- override fun read (key : String ): AckEnvelope <A >? = unsafeSneakyRaises {
512- withRetries { readImpl(key) }
513- }
495+ override fun read (key : String ): AckEnvelope <A >? = withRetries { readImpl(key) }
514496
515- context(_: Raise <InterruptedException >, _: Raise <SQLException >)
516497 private fun readImpl (key : String ): AckEnvelope <A >? {
517498 return database.withConnection { connection ->
518499 val row = adapter.selectByKey(connection, pKind, key) ? : return @withConnection null
@@ -539,19 +520,13 @@ private constructor(
539520 }
540521
541522 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
542- override fun dropMessage (key : String ): Boolean = unsafeSneakyRaises {
543- withRetries {
544- database.withTransaction { connection -> adapter.deleteOneRow(connection, key, pKind) }
545- }
523+ override fun dropMessage (key : String ): Boolean = withRetries {
524+ database.withTransaction { connection -> adapter.deleteOneRow(connection, key, pKind) }
546525 }
547526
548527 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
549- override fun containsMessage (key : String ): Boolean = unsafeSneakyRaises {
550- withRetries {
551- database.withConnection { connection ->
552- adapter.checkIfKeyExists(connection, key, pKind)
553- }
554- }
528+ override fun containsMessage (key : String ): Boolean = withRetries {
529+ database.withConnection { connection -> adapter.checkIfKeyExists(connection, key, pKind) }
555530 }
556531
557532 @Throws(
@@ -564,12 +539,8 @@ private constructor(
564539 " To drop all messages, you must provide the exact confirmation string"
565540 }
566541
567- return unsafeSneakyRaises {
568- withRetries {
569- database.withTransaction { connection ->
570- adapter.dropAllMessages(connection, pKind)
571- }
572- }
542+ return withRetries {
543+ database.withTransaction { connection -> adapter.dropAllMessages(connection, pKind) }
573544 }
574545 }
575546
@@ -607,6 +578,8 @@ private constructor(
607578 public companion object {
608579 private val logger = LoggerFactory .getLogger(DelayedQueueJDBC ::class .java)
609580
581+ private fun <T > withRetries (block : () -> T ): T = block()
582+
610583 /* *
611584 * Runs database migrations for the specified configuration.
612585 *
@@ -619,7 +592,7 @@ private constructor(
619592 */
620593 @JvmStatic
621594 @Throws(ResourceUnavailableException ::class , InterruptedException ::class )
622- public fun runMigrations (config : DelayedQueueJDBCConfig ): Unit = unsafeSneakyRaises {
595+ public fun runMigrations (config : DelayedQueueJDBCConfig ): Unit = withRetries {
623596 val database = Database (config.db)
624597 database.use {
625598 database.withConnection { connection ->
@@ -671,7 +644,7 @@ private constructor(
671644 serializer : MessageSerializer <A >,
672645 config : DelayedQueueJDBCConfig ,
673646 clock : Clock = Clock .systemUTC(),
674- ): DelayedQueueJDBC <A > = unsafeSneakyRaises {
647+ ): DelayedQueueJDBC <A > = withRetries {
675648 val database = Database (config.db)
676649 val adapter = SQLVendorAdapter .create(config.db.driver, config.tableName)
677650 DelayedQueueJDBC (
0 commit comments