From a6bda894b028991923598d14d81658e315779d52 Mon Sep 17 00:00:00 2001 From: b-mob-notify Date: Mon, 13 Jul 2026 09:28:30 +0000 Subject: [PATCH] =?UTF-8?q?push(core):=20P2=20retry/=D0=B1=D1=8D=D0=BA?= =?UTF-8?q?=D0=BE=D1=84=D1=84=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D1=83=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=B0=20(=D1=82=D1=80=D0=B0=D0=BD=D0=B7?= =?UTF-8?q?=D0=B8=D0=B5=D0=BD=D1=82=D0=BD=D1=8B=D0=B5=20=D1=81=D0=B1=D0=BE?= =?UTF-8?q?=D0=B8/5xx/408/429)=20+=20=D1=84=D0=B8=D0=BA=D1=81=20detekt=20A?= =?UTF-8?q?rgumentListWrapping=20=D0=B2=20F7IncomingCallQueue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../f7cloud/core/push/F7IncomingCallQueue.kt | 7 +++- .../f7cloud/core/push/F7PushRegistrar.kt | 42 ++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt index 8d393bc..6df60be 100644 --- a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt +++ b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt @@ -159,7 +159,12 @@ object F7IncomingCallQueue { val prefs = prefs(context) cancelTimeout(context) cancelNotification(context) - prefs.edit().remove(KEY_QUEUE).remove(KEY_ACTIVE_TOKEN).remove(KEY_ACTIVE_AT).remove(KEY_ACTIVE_CALL).apply() + prefs.edit() + .remove(KEY_QUEUE) + .remove(KEY_ACTIVE_TOKEN) + .remove(KEY_ACTIVE_AT) + .remove(KEY_ACTIVE_CALL) + .apply() } } diff --git a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt index 6d98c89..ef97d9e 100644 --- a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt +++ b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt @@ -17,6 +17,10 @@ object F7PushRegistrar { private const val TAG = "F7PushRegistrar" private const val PREFS = "f7push" private const val KEY_DEVICE_ID = "device_id" + private const val MAX_ATTEMPTS = 3 + + // Паузы перед 2-й и 3-й попытками (мс); экспоненциальный бэкофф. + private val BACKOFF_MS = longArrayOf(1000L, 3000L) fun getDeviceId(context: Context): String { val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) @@ -41,12 +45,40 @@ object F7PushRegistrar { .put("platform", "android") .put("clientApp", "f7cloud-mobile") .toString() - return try { - postJson(endpoint, session, body) - } catch (t: Throwable) { - Log.w(TAG, "register failed", t) - 0 + + var lastCode = 0 + for (attempt in 0 until MAX_ATTEMPTS) { + if (attempt > 0) { + val wait = BACKOFF_MS[(attempt - 1).coerceAtMost(BACKOFF_MS.size - 1)] + try { + Thread.sleep(wait) + } catch (_: InterruptedException) { + Thread.currentThread().interrupt() + return lastCode + } + } + lastCode = try { + postJson(endpoint, session, body) + } catch (t: Throwable) { + Log.w(TAG, "register attempt ${attempt + 1} failed", t) + 0 + } + if (!isRetryable(lastCode)) { + return lastCode + } + Log.w( + TAG, + "register attempt ${attempt + 1} got $lastCode, " + + if (attempt + 1 < MAX_ATTEMPTS) "retrying" else "giving up" + ) } + return lastCode + } + + // Транзиентные сбои: сетевое исключение (0), ошибка сервера (5xx), 408/429. + // 2xx — успех, 4xx (кроме 408/429) — постоянная ошибка (кривой app-password/запрос), повтор не поможет. + private fun isRetryable(code: Int): Boolean { + return code == 0 || code >= 500 || code == 408 || code == 429 } private fun postJson(endpoint: String, session: AuthSession, json: String): Int {