Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
alias(libs.plugins.stream.android.application)
alias(libs.plugins.kotlin.android)
Expand Down
16 changes: 16 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension
import com.vanniktech.maven.publish.MavenPublishBaseExtension
Expand Down
15 changes: 15 additions & 0 deletions config/license/generated/license-2025.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
16 changes: 16 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

include(":stream-android-core-annotations")


Expand Down
16 changes: 16 additions & 0 deletions stream-android-core-annotations/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
alias(libs.plugins.jetbrains.kotlin.jvm)
alias(libs.plugins.stream.java.library)
Expand Down
16 changes: 16 additions & 0 deletions stream-android-core-lint/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
alias(libs.plugins.jetbrains.kotlin.jvm)
alias(libs.plugins.stream.java.library)
Expand Down
16 changes: 16 additions & 0 deletions stream-android-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:OptIn(ExperimentalAbiValidation::class)

import org.jetbrains.kotlin.gradle.dsl.JvmTarget
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.android.core.api.model.retry

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

/**
* Snapshot describing the state of the current retry loop iteration.
*
* Instances are delivered to the `block` passed into
* [StreamRetryProcessor.retry][io.getstream.android.core.api.processing.StreamRetryProcessor.retry]
* before each attempt so callers can log, alter metrics, or branch based on the retry count.
*
* @property attempt 1-based attempt index. `1` represents the first invocation after the initial
* failure.
* @property currentDelay Delay (in milliseconds) that was applied before this attempt. Equals the
* policy's `initialDelayMillis` for the first retry.
* @property previousAttemptError The error thrown by the previous attempt, if any. `null` for the
* first attempt.
* @property policy The retry policy governing this run; useful if callers need to inspect limits
* (max retries, back-off window, etc.).
*/
@StreamInternalApi
public data class StreamRetryAttemptInfo(
val attempt: Int,
val currentDelay: Long,
val previousAttemptError: Throwable? = null,
val policy: StreamRetryPolicy,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.getstream.android.core.api.model
package io.getstream.android.core.api.model.retry
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package io.getstream.android.core.api.processing

import io.getstream.android.core.annotations.StreamInternalApi
import io.getstream.android.core.api.log.StreamLogger
import io.getstream.android.core.api.model.StreamRetryPolicy
import io.getstream.android.core.api.model.retry.StreamRetryAttemptInfo
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
import io.getstream.android.core.internal.processing.StreamRetryProcessorImpl

/**
Expand Down Expand Up @@ -54,7 +55,10 @@ public interface StreamRetryProcessor {
* @param block The suspending operation to execute. It should throw on failure; the processor
* handles re-invocation.
*/
public suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend () -> T): Result<T>
public suspend fun <T> retry(
policy: StreamRetryPolicy,
block: suspend (attempt: StreamRetryAttemptInfo) -> T,
): Result<T>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,50 @@
package io.getstream.android.core.internal.processing

import io.getstream.android.core.api.log.StreamLogger
import io.getstream.android.core.api.model.StreamRetryPolicy
import io.getstream.android.core.api.model.retry.StreamRetryAttemptInfo
import io.getstream.android.core.api.model.retry.StreamRetryPolicy
import io.getstream.android.core.api.processing.StreamRetryProcessor
import io.getstream.android.core.api.utils.runCatchingCancellable
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.delay

internal class StreamRetryProcessorImpl(private val logger: StreamLogger) : StreamRetryProcessor {
override suspend fun <T> retry(policy: StreamRetryPolicy, block: suspend () -> T): Result<T> =
runCatchingCancellable {
var delayMs = policy.initialDelayMillis
var attempt = 1
while (attempt <= policy.maxRetries) {
if (delayMs > 0) {
delay(delayMs)
}
try {
return@runCatchingCancellable block()
} catch (c: CancellationException) {
throw c
} catch (t: Throwable) {
logger.v { "Retry attempt $attempt failed: ${t.message}" }
val checkGiveUp = attempt >= policy.minRetries
val shouldGiveUp = checkGiveUp && policy.giveUpFunction(attempt, t)
val isLastAttempt = attempt == policy.maxRetries
if (shouldGiveUp || isLastAttempt) {
logger.v { "Retry attempt $attempt failed: ${t.message}. Giving up." }
throw t
}
delayMs =
policy
.nextBackOffDelayFunction(attempt, delayMs)
.coerceIn(policy.minBackoffMills, policy.maxBackoffMills)
override suspend fun <T> retry(
policy: StreamRetryPolicy,
block: suspend (attempt: StreamRetryAttemptInfo) -> T,
): Result<T> = runCatchingCancellable {
var delayMs = policy.initialDelayMillis
var attempt = 1
var previousError: Throwable? = null
while (attempt <= policy.maxRetries) {
if (delayMs > 0) {
delay(delayMs)
}
try {
return@runCatchingCancellable block(
StreamRetryAttemptInfo(attempt, delayMs, previousError, policy)
)
} catch (c: CancellationException) {
throw c
} catch (t: Throwable) {
logger.v { "Retry attempt $attempt failed: ${t.message}" }
val checkGiveUp = attempt >= policy.minRetries
val shouldGiveUp = checkGiveUp && policy.giveUpFunction(attempt, t)
val isLastAttempt = attempt == policy.maxRetries
previousError = t
if (shouldGiveUp || isLastAttempt) {
logger.v { "Retry attempt $attempt failed: ${t.message}. Giving up." }
throw t
}
attempt++
delayMs =
policy
.nextBackOffDelayFunction(attempt, delayMs)
.coerceIn(policy.minBackoffMills, policy.maxBackoffMills)
}

logger.e { "Retry loop completed without success or failure. Policy: $policy" }
throw IllegalStateException("Check your policy: $policy")
attempt++
}

logger.e { "Retry loop completed without success or failure. Policy: $policy" }
throw IllegalStateException("Check your policy: $policy")
}
}
Loading
Loading