Skip to content

Commit bfe66fb

Browse files
Move retry policy in another package and add attempt info in the callback
1 parent b28d084 commit bfe66fb

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.getstream.android.core.api.model.retry
2+
3+
import io.getstream.android.core.annotations.StreamInternalApi
4+
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
5+
6+
/**
7+
* Snapshot describing the state of the current retry loop iteration.
8+
*
9+
* Instances are delivered to the `block` passed into
10+
* [StreamRetryProcessor.retry][io.getstream.android.core.api.processing.StreamRetryProcessor.retry]
11+
* before each attempt so callers can log, alter metrics, or branch based on the retry count.
12+
*
13+
* @property attempt 1-based attempt index. `1` represents the first invocation after the initial
14+
* failure.
15+
* @property currentDelay Delay (in milliseconds) that was applied before this attempt. Equals the
16+
* policy's `initialDelayMillis` for the first retry.
17+
* @property previousAttemptError The error thrown by the previous attempt, if any. `null` for the
18+
* first attempt.
19+
* @property policy The retry policy governing this run; useful if callers need to inspect limits
20+
* (max retries, back-off window, etc.).
21+
*/
22+
@StreamInternalApi
23+
public data class StreamRetryAttemptInfo(
24+
val attempt: Int,
25+
val currentDelay: Long,
26+
val previousAttemptError: Throwable? = null,
27+
val policy: StreamRetryPolicy,
28+
)

stream-android-core/src/main/java/io/getstream/android/core/api/model/StreamRetryPolicy.kt renamed to stream-android-core/src/main/java/io/getstream/android/core/api/model/retry/StreamRetryPolicy.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
/*
2-
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3-
*
4-
* Licensed under the Stream License;
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package io.getstream.android.core.api.model
1+
package io.getstream.android.core.api.model.retry
182

193
import io.getstream.android.core.annotations.StreamInternalApi
204

@@ -234,4 +218,4 @@ private constructor(
234218
return this
235219
}
236220
}
237-
}
221+
}

stream-android-core/src/main/java/io/getstream/android/core/api/processing/StreamRetryProcessor.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ package io.getstream.android.core.api.processing
1818

1919
import io.getstream.android.core.annotations.StreamInternalApi
2020
import io.getstream.android.core.api.log.StreamLogger
21-
import io.getstream.android.core.api.model.StreamRetryPolicy
21+
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
22+
import io.getstream.android.core.api.model.retry.StreamRetryAttemptInfo
2223
import io.getstream.android.core.internal.processing.StreamRetryProcessorImpl
2324

2425
/**
@@ -54,7 +55,7 @@ public interface StreamRetryProcessor {
5455
* @param block The suspending operation to execute. It should throw on failure; the processor
5556
* handles re-invocation.
5657
*/
57-
public suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend () -> T): Result<T>
58+
public suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend (attempt: StreamRetryAttemptInfo) -> T): Result<T>
5859
}
5960

6061
/**

stream-android-core/src/main/java/io/getstream/android/core/internal/processing/StreamRetryProcessorImpl.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,38 @@
1717
package io.getstream.android.core.internal.processing
1818

1919
import io.getstream.android.core.api.log.StreamLogger
20-
import io.getstream.android.core.api.model.StreamRetryPolicy
20+
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
21+
import io.getstream.android.core.api.model.retry.StreamRetryAttemptInfo
2122
import io.getstream.android.core.api.processing.StreamRetryProcessor
2223
import io.getstream.android.core.api.utils.runCatchingCancellable
2324
import kotlin.coroutines.cancellation.CancellationException
2425
import kotlinx.coroutines.delay
2526

2627
internal class StreamRetryProcessorImpl(private val logger: StreamLogger) : StreamRetryProcessor {
27-
override suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend () -> T): Result<T> =
28+
override suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend (attempt: StreamRetryAttemptInfo) -> T): Result<T> =
2829
runCatchingCancellable {
2930
var delayMs = policy.initialDelayMillis
3031
var attempt = 1
32+
var previousError: Throwable? = null
3133
while (attempt <= policy.maxRetries) {
3234
if (delayMs > 0) {
3335
delay(delayMs)
3436
}
3537
try {
36-
return@runCatchingCancellable block()
38+
return@runCatchingCancellable block(StreamRetryAttemptInfo(
39+
attempt,
40+
delayMs,
41+
previousError,
42+
policy
43+
))
3744
} catch (c: CancellationException) {
3845
throw c
3946
} catch (t: Throwable) {
4047
logger.v { "Retry attempt $attempt failed: ${t.message}" }
4148
val checkGiveUp = attempt >= policy.minRetries
4249
val shouldGiveUp = checkGiveUp && policy.giveUpFunction(attempt, t)
4350
val isLastAttempt = attempt == policy.maxRetries
51+
previousError = t
4452
if (shouldGiveUp || isLastAttempt) {
4553
logger.v { "Retry attempt $attempt failed: ${t.message}. Giving up." }
4654
throw t

stream-android-core/src/test/java/io/getstream/android/core/internal/processing/StreamRetryProcessorImplTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
package io.getstream.android.core.internal.processing
2020

21-
import io.getstream.android.core.api.model.StreamRetryPolicy
21+
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
2222
import io.mockk.mockk
2323
import java.util.concurrent.atomic.AtomicInteger
2424
import kotlinx.coroutines.ExperimentalCoroutinesApi

0 commit comments

Comments
 (0)