Skip to content

Commit

Permalink
GH-1985 Use ExponentialBackoffStrategy for S3Client (Resolve #1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Dec 20, 2023
1 parent f79e2f3 commit 83b3af3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.reposilite.storage.s3

import java.time.Duration
import kotlin.math.min

import software.amazon.awssdk.core.retry.RetryPolicyContext
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy

internal class ExponentialBackoffStrategy(
private val baseDelay: Duration,
private val maxBackoff: Duration
) : BackoffStrategy {

override fun computeDelayBeforeNextRetry(context: RetryPolicyContext): Duration {
val retriesAttempted = context.retriesAttempted().toLong()

val delayMillis: Long = min(
baseDelay.toMillis() * (1L shl retriesAttempted.toInt()),
maxBackoff.toMillis()
)

return Duration.ofMillis(delayMillis)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.S3Configuration
import java.net.URI
import java.nio.file.Path
import java.time.Duration

class S3StorageProviderFactory : StorageProviderFactory<S3StorageProvider, S3StorageProviderSettings> {

Expand Down Expand Up @@ -70,6 +71,13 @@ class S3StorageProviderFactory : StorageProviderFactory<S3StorageProvider, S3Sto
?.let { URI.create(it) }
?.also { client.endpointOverride(it) }

client.overrideConfiguration {
it.retryPolicy { cfg ->
cfg.backoffStrategy(ExponentialBackoffStrategy(Duration.ofSeconds(1), Duration.ofMinutes(1)))
cfg.numRetries(5)
}
}

return try {
S3StorageProvider(
failureFacade = failureFacade,
Expand Down

0 comments on commit 83b3af3

Please sign in to comment.