Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWN-999 - Retry on socket referral exception #2207

Merged
merged 2 commits into from
Feb 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package org.p2p.wallet.referral.repository
import com.google.gson.Gson
import org.near.borshj.BorshBuffer
import timber.log.Timber
import java.net.SocketTimeoutException
import java.net.URI
import java.util.Optional
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.withContext
import org.p2p.core.dispatchers.CoroutineDispatchers
import org.p2p.core.rpc.JsonRpc
Expand All @@ -17,6 +19,7 @@ import org.p2p.solanaj.utils.SolanaMessageSigner
import org.p2p.wallet.auth.gateway.repository.mapper.write
import org.p2p.wallet.common.feature_toggles.toggles.remote.ReferralProgramEnabledFeatureToggle
import org.p2p.wallet.infrastructure.network.provider.TokenKeyProvider
import org.p2p.wallet.utils.retryOnException

class ReferralRemoteRepository(
private val api: RpcApi,
Expand All @@ -29,9 +32,23 @@ class ReferralRemoteRepository(

private val url = URI("https://referral.key.app/")

/**
* Sometimes the service responds with the timeout error `operation timed out`
*
*/
private class ReferralServiceTimedOut : Exception()

override suspend fun registerReferent() {
if (referralEnabledFt.isFeatureEnabled) {
registerReferentInternal()
retryOnException(
exceptionTypes = setOf(
ReferralServiceTimedOut::class,
SocketTimeoutException::class
),
maxAttempts = 10,
delayMillis = 5.seconds.inWholeMilliseconds,
block = ::registerReferentInternal
)
}
}

Expand All @@ -57,11 +74,19 @@ class ReferralRemoteRepository(
request.parseResponse(response, gson)
} catch (rpcError: JsonRpc.ResponseError) {
val message = rpcError.message ?: return
if (message.contains("duplicate key value violates unique constraint")) {
Timber.i("User already registered")
} else {
Timber.e(rpcError, "Failed to register referent")
when {
message.contains("duplicate key value violates unique constraint") -> {
Timber.i("User already registered")
}
message.contains("timed out") -> {
throw ReferralServiceTimedOut()
}
else -> {
Timber.e(rpcError, "Failed to register referent")
}
}
} catch (error: SocketTimeoutException) {
throw error
} catch (error: Throwable) {
Timber.e(error, "Failed to register referent")
}
Expand Down
Loading