-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update retry store to separate out update and add and remove mutator. Leave the retry attempt increment up to the flow processor.
- Loading branch information
Showing
5 changed files
with
81 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...oroutines-retry/src/main/kotlin/tech/figure/coroutines/retry/flow/ObjectStoreRetryFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tech.figure.coroutines.retry.flow | ||
|
||
import java.time.OffsetDateTime | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.asFlow | ||
import tech.figure.coroutines.retry.store.RetryRecord | ||
import tech.figure.coroutines.retry.store.RetryRecordStore | ||
|
||
/** | ||
* Retry a flow of objects using the backing [store]. | ||
* | ||
* @param handler The handler to reprocess with. | ||
* @param store [RetryRecordStore] to save and retrieve data object of type [T] from. | ||
* @param groupSize Process a max of this many elements each poll loop (aka: limit). | ||
*/ | ||
fun <T> recordStoreFlowRetry( | ||
handler: suspend (T) -> Unit, | ||
store: RetryRecordStore<T>, | ||
groupSize: Int = 40, | ||
): FlowRetry<T> = RecordStoreFlowRetry(handler, store, groupSize) | ||
|
||
/** | ||
* Retry a flow of objects using the backing [store]. | ||
* | ||
* @param handler The handler to reprocess with. | ||
* @param store [RetryRecordStore] to save and retrieve data object of type [T] from. | ||
* @param groupSize Process a max of this many elements each poll loop (aka: limit). | ||
*/ | ||
internal open class RecordStoreFlowRetry<T>( | ||
private val handler: suspend (T) -> Unit, | ||
private val store: RetryRecordStore<T>, | ||
private val groupSize: Int = 40, | ||
) : FlowRetry<T> { | ||
override suspend fun produceNext( | ||
attemptRange: IntRange, | ||
olderThan: OffsetDateTime, | ||
limit: Int | ||
): Flow<RetryRecord<T>> = | ||
store | ||
.select(attemptRange, olderThan, limit) | ||
.sortedByDescending { it.lastAttempted } | ||
.take(groupSize) | ||
.asFlow() | ||
|
||
override suspend fun send(item: T, e: Throwable) = | ||
store.update(item, e) | ||
|
||
override suspend fun onSuccess(item: RetryRecord<T>) = | ||
store.remove(item.data) | ||
|
||
override suspend fun onFailure(item: RetryRecord<T>, e: Throwable) = | ||
store.update(item.data, e) | ||
|
||
override suspend fun process(item: T, attempt: Int) = | ||
handler(item) | ||
|
||
override suspend fun hasNext() = | ||
!store.isEmpty() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters