push(core): P2 retry/бэкофф регистрации устройства (транзиентные сбои/5xx/408/429) + фикс detekt ArgumentListWrapping в F7IncomingCallQueue
This commit is contained in:
@@ -159,7 +159,12 @@ object F7IncomingCallQueue {
|
|||||||
val prefs = prefs(context)
|
val prefs = prefs(context)
|
||||||
cancelTimeout(context)
|
cancelTimeout(context)
|
||||||
cancelNotification(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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ object F7PushRegistrar {
|
|||||||
private const val TAG = "F7PushRegistrar"
|
private const val TAG = "F7PushRegistrar"
|
||||||
private const val PREFS = "f7push"
|
private const val PREFS = "f7push"
|
||||||
private const val KEY_DEVICE_ID = "device_id"
|
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 {
|
fun getDeviceId(context: Context): String {
|
||||||
val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||||
@@ -41,12 +45,40 @@ object F7PushRegistrar {
|
|||||||
.put("platform", "android")
|
.put("platform", "android")
|
||||||
.put("clientApp", "f7cloud-mobile")
|
.put("clientApp", "f7cloud-mobile")
|
||||||
.toString()
|
.toString()
|
||||||
return try {
|
|
||||||
|
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)
|
postJson(endpoint, session, body)
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
Log.w(TAG, "register failed", t)
|
Log.w(TAG, "register attempt ${attempt + 1} failed", t)
|
||||||
0
|
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 {
|
private fun postJson(endpoint: String, session: AuthSession, json: String): Int {
|
||||||
|
|||||||
Reference in New Issue
Block a user