From bd276d54008213d667df93879f9d457b625d1851 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 14 Dec 2021 10:20:37 +0000 Subject: [PATCH] perf(delivery): improve the memory and performance overhead of handling the delivery response status code --- CHANGELOG.md | 7 +++++++ .../java/com/bugsnag/android/DefaultDelivery.kt | 15 ++++++++------- .../test/java/com/bugsnag/android/DeliveryTest.kt | 5 +++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51fcab261..16491facf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## TBD + +### Enhancements + +* Improve the memory use and performance overhead when handling the delivery response status codes + [#1558](https://github.com/bugsnag/bugsnag-android/pull/1558) + ## 5.17.0 (2021-12-08) ### Enhancements diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/DefaultDelivery.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/DefaultDelivery.kt index 5587ba7491..8c8431e10d 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/DefaultDelivery.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/DefaultDelivery.kt @@ -123,14 +123,15 @@ internal class DefaultDelivery( } internal fun getDeliveryStatus(responseCode: Int): DeliveryStatus { - val unrecoverableCodes = IntRange(HTTP_BAD_REQUEST, 499).filter { - it != HTTP_CLIENT_TIMEOUT && it != 429 - } - - return when (responseCode) { - in HTTP_OK..299 -> DeliveryStatus.DELIVERED - in unrecoverableCodes -> DeliveryStatus.FAILURE + return when { + responseCode in HTTP_OK..299 -> DeliveryStatus.DELIVERED + isUnrecoverableStatusCode(responseCode) -> DeliveryStatus.FAILURE else -> DeliveryStatus.UNDELIVERED } } + + private fun isUnrecoverableStatusCode(responseCode: Int) = + responseCode in HTTP_BAD_REQUEST..499 && // 400-499 are considered unrecoverable + responseCode != HTTP_CLIENT_TIMEOUT && // except for 408 + responseCode != 429 // and 429 } diff --git a/bugsnag-android-core/src/test/java/com/bugsnag/android/DeliveryTest.kt b/bugsnag-android-core/src/test/java/com/bugsnag/android/DeliveryTest.kt index 1dbc2a3fc8..243bc15a38 100644 --- a/bugsnag-android-core/src/test/java/com/bugsnag/android/DeliveryTest.kt +++ b/bugsnag-android-core/src/test/java/com/bugsnag/android/DeliveryTest.kt @@ -14,5 +14,10 @@ class DeliveryTest { assertEquals(DeliveryStatus.UNDELIVERED, delivery.getDeliveryStatus(408)) assertEquals(DeliveryStatus.UNDELIVERED, delivery.getDeliveryStatus(429)) assertEquals(DeliveryStatus.FAILURE, delivery.getDeliveryStatus(400)) + assertEquals(DeliveryStatus.FAILURE, delivery.getDeliveryStatus(401)) + assertEquals(DeliveryStatus.FAILURE, delivery.getDeliveryStatus(498)) + assertEquals(DeliveryStatus.FAILURE, delivery.getDeliveryStatus(499)) + assertEquals(DeliveryStatus.UNDELIVERED, delivery.getDeliveryStatus(408)) + assertEquals(DeliveryStatus.UNDELIVERED, delivery.getDeliveryStatus(429)) } }