-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-5977 create UploadSchedulerStrategy interface and default impleme…
…ntation
- Loading branch information
Showing
13 changed files
with
418 additions
and
539 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...id-core/src/main/kotlin/com/datadog/android/core/configuration/UploadSchedulerStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.core.configuration | ||
|
||
/** | ||
* Defines the strategy used to schedule the waiting period between batch uploads. | ||
*/ | ||
interface UploadSchedulerStrategy { | ||
|
||
/** | ||
* Should return the delay in millisecond to wait until the next upload attempt | ||
* is performed. | ||
* @param featureName the name of the feature for which a new upload will be scheduled | ||
* @param uploadAttempts the number of requests that were attempted during this run. Will be zero if the device | ||
* is not ready (e.g.: when offline or with low battery). If multiple batches can be uploaded, the attempts will | ||
* stop at the first failure. | ||
* @param lastStatusCode the HTTP status code of the last request (if available). A successful upload will have a | ||
* status code 202 (Accepted). When null, it means that | ||
* @param throwable the exception thrown during the upload process (if any). | ||
*/ | ||
fun getMsDelayUntilNextUpload( | ||
featureName : String, | ||
uploadAttempts: Int, | ||
lastStatusCode: Int?, | ||
throwable: Throwable? | ||
): Long | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...in/kotlin/com/datadog/android/core/internal/data/upload/DefaultUploadSchedulerStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.core.internal.data.upload | ||
|
||
import com.datadog.android.core.configuration.UploadSchedulerStrategy | ||
import com.datadog.android.core.internal.configuration.DataUploadConfiguration | ||
import com.datadog.android.core.internal.data.upload.DataOkHttpUploader.Companion.HTTP_ACCEPTED | ||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
import kotlin.math.roundToLong | ||
|
||
internal class DefaultUploadSchedulerStrategy( | ||
internal val uploadConfiguration: DataUploadConfiguration | ||
) : UploadSchedulerStrategy { | ||
|
||
private val currentDelays = mutableMapOf<String, Long>() | ||
|
||
// region UploadSchedulerStrategy | ||
|
||
override fun getMsDelayUntilNextUpload( | ||
featureName: String, | ||
uploadAttempts: Int, | ||
lastStatusCode: Int?, | ||
throwable: Throwable? | ||
): Long { | ||
val previousDelay = currentDelays.getOrPut(featureName) { uploadConfiguration.defaultDelayMs } | ||
val updatedDelay = if (uploadAttempts > 0 && throwable == null && lastStatusCode == HTTP_ACCEPTED) { | ||
decreaseInterval(previousDelay) | ||
} else { | ||
increaseInterval(previousDelay, throwable) | ||
} | ||
currentDelays[featureName] = updatedDelay | ||
return updatedDelay | ||
} | ||
|
||
// endregion | ||
|
||
// region Internal | ||
|
||
private fun decreaseInterval(previousDelay: Long): Long { | ||
@Suppress("UnsafeThirdPartyFunctionCall") // not a NaN | ||
val newDelayMs = (previousDelay * DECREASE_PERCENT).roundToLong() | ||
return max(uploadConfiguration.minDelayMs, newDelayMs) | ||
} | ||
|
||
private fun increaseInterval(previousDelay: Long, throwable: Throwable?): Long { | ||
@Suppress("UnsafeThirdPartyFunctionCall") // not a NaN | ||
val newDelayMs = (previousDelay * INCREASE_PERCENT).roundToLong() | ||
|
||
return if (throwable is IOException) { | ||
// An IOException can mean a DNS error, or network connection loss | ||
// Those aren't likely to be a fluke or flakiness, so we use a longer delay to avoid infinite looping | ||
// and prevent battery draining | ||
NETWORK_ERROR_DELAY_MS | ||
} else { | ||
min(uploadConfiguration.maxDelayMs, newDelayMs) | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val DECREASE_PERCENT = 0.90 | ||
internal const val INCREASE_PERCENT = 1.10 | ||
internal val NETWORK_ERROR_DELAY_MS = TimeUnit.MINUTES.toMillis(1) // 1 minute delay | ||
} | ||
} |
Oops, something went wrong.