From c70b5cfe3b67e80274dc71c6366d05ac477a6cc8 Mon Sep 17 00:00:00 2001 From: pantia Date: Thu, 12 Mar 2026 16:17:05 +0200 Subject: [PATCH] Channel: add resetConnectBackoff method Adds resetConnectBackoff to Channel to allow resetting the connection backoff state and forcing the channel to retry connection attempts without waiting for the current backoff delay --- .../kotlin/io/github/timortel/kmpgrpc/core/Channel.kt | 5 ++++- .../kotlin/io/github/timortel/kmpgrpc/core/Channel.kt | 10 ++++++++++ .../kotlin/io/github/timortel/kmpgrpc/core/Channel.kt | 2 ++ .../kotlin/io/github/timortel/kmpgrpc/core/Channel.kt | 2 ++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/kmp-grpc-core/src/androidJvmCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt b/kmp-grpc-core/src/androidJvmCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt index 696d0d4e..e2f4fefb 100644 --- a/kmp-grpc-core/src/androidJvmCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt +++ b/kmp-grpc-core/src/androidJvmCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt @@ -102,7 +102,6 @@ actual class Channel private constructor(val channel: ManagedChannel) { actual suspend fun shutdown() { channel.shutdown() - awaitTermination() } @@ -129,4 +128,8 @@ actual class Channel private constructor(val channel: ManagedChannel) { } } } + + actual fun resetConnectBackoff() { + channel.resetConnectBackoff() + } } diff --git a/kmp-grpc-core/src/commonMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt b/kmp-grpc-core/src/commonMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt index 8edb735c..63aab2d8 100644 --- a/kmp-grpc-core/src/commonMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt +++ b/kmp-grpc-core/src/commonMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt @@ -69,6 +69,7 @@ expect class Channel { */ fun withClientIdentity(certificate: Certificate, key: PrivateKey): Builder + /** * Construct the channel * @@ -93,6 +94,15 @@ expect class Channel { */ suspend fun shutdown() + + /** + * For subchannels that are in TRANSIENT_FAILURE state, short-circuit the backoff timer and make them reconnect immediately + * + * This is primarily intended for Android users, where the network may experience frequent temporary drops. Rather than waiting for gRPC's name resolution and reconnect timers to elapse before reconnecting, the app may use this method as a mechanism to notify gRPC that the network is now available and a reconnection attempt may occur immediately. + * + * No-op if not supported by the implementation. + */ + fun resetConnectBackoff() /** * Initiates a forceful shutdown of the gRPC channel. After invoking this method, no new calls * can be started, and ongoing calls are immediately canceled. diff --git a/kmp-grpc-core/src/jsTargetCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt b/kmp-grpc-core/src/jsTargetCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt index f9a8bb8b..d27dab1c 100644 --- a/kmp-grpc-core/src/jsTargetCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt +++ b/kmp-grpc-core/src/jsTargetCommon/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt @@ -66,4 +66,6 @@ actual class Channel private constructor( override fun cleanupResources() { client.close() } + + actual fun resetConnectBackoff() {} } diff --git a/kmp-grpc-core/src/nativeMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt b/kmp-grpc-core/src/nativeMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt index c1a4feb7..e87f05b0 100644 --- a/kmp-grpc-core/src/nativeMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt +++ b/kmp-grpc-core/src/nativeMain/kotlin/io/github/timortel/kmpgrpc/core/Channel.kt @@ -179,6 +179,8 @@ actual class Channel private constructor( init(ENABLE_TRACE_LOGGING) } } + + actual fun resetConnectBackoff() {} } private fun installCertificates(