-
Notifications
You must be signed in to change notification settings - Fork 362
/
S3ClientProducer.scala
81 lines (73 loc) · 2.59 KB
/
S3ClientProducer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2019 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.store.s3
import geotrellis.store.s3.conf.S3Config
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.core.retry.backoff.FullJitterBackoffStrategy
import software.amazon.awssdk.core.retry.conditions.{OrRetryCondition, RetryCondition}
import software.amazon.awssdk.awscore.retry.conditions.RetryOnErrorCodeCondition
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration
import software.amazon.awssdk.core.retry.RetryPolicy
import java.time.Duration
/**
* Singleton which can be used to customize the default S3 client used throughout geotrellis
*
* Various classes are loaded based on URI patterns and because SPI requires
* parameterless constructors, it is necessary to register any customizations
* to the S3Client that should be applied by default here.
*
*/
object S3ClientProducer {
@transient private lazy val client = {
val retryCondition =
OrRetryCondition.create(
RetryCondition.defaultRetryCondition(),
RetryOnErrorCodeCondition.create("RequestTimeout")
)
val backoffStrategy =
FullJitterBackoffStrategy.builder()
.baseDelay(Duration.ofMillis(50))
.maxBackoffTime(Duration.ofMillis(15))
.build()
val retryPolicy =
RetryPolicy.defaultRetryPolicy()
.toBuilder
.retryCondition(retryCondition)
.backoffStrategy(backoffStrategy)
.build()
val overrideConfig =
ClientOverrideConfiguration.builder()
.retryPolicy(retryPolicy)
.requestPayer(S3Config.requestPayer)
.build()
S3Client.builder()
.overrideConfiguration(overrideConfig)
.build()
}
private var summonClient: () => S3Client = {
() => client
}
/**
* Set an alternative default function for summoning S3Clients
*/
def set(getClient: () => S3Client): Unit =
summonClient = getClient
/**
* Get the current function registered as default for summong S3Clients
*/
def get: () => S3Client =
summonClient
}