1
15
16 package software.amazon.awssdk.core.internal.retry;
17
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.core.exception.SdkException;
20 import software.amazon.awssdk.core.retry.RetryUtils;
21 import software.amazon.awssdk.core.retry.conditions.TokenBucketExceptionCostFunction;
22 import software.amazon.awssdk.utils.ToString;
23 import software.amazon.awssdk.utils.Validate;
24
25 @SdkInternalApi
26 public class DefaultTokenBucketExceptionCostFunction implements TokenBucketExceptionCostFunction {
27 private final Integer throttlingExceptionCost;
28 private final int defaultExceptionCost;
29
30 private DefaultTokenBucketExceptionCostFunction(Builder builder) {
31 this.throttlingExceptionCost = builder.throttlingExceptionCost;
32 this.defaultExceptionCost = Validate.paramNotNull(builder.defaultExceptionCost, "defaultExceptionCost");
33 }
34
35 @Override
36 public Integer apply(SdkException e) {
37 if (throttlingExceptionCost != null && RetryUtils.isThrottlingException(e)) {
38 return throttlingExceptionCost;
39 }
40
41 return defaultExceptionCost;
42 }
43
44 @Override
45 public String toString() {
46 return ToString.builder("TokenBucketExceptionCostCalculator")
47 .add("throttlingExceptionCost", throttlingExceptionCost)
48 .add("defaultExceptionCost", defaultExceptionCost)
49 .build();
50 }
51
52 @Override
53 public boolean equals(Object o) {
54 if (this == o) {
55 return true;
56 }
57 if (o == null || getClass() != o.getClass()) {
58 return false;
59 }
60
61 DefaultTokenBucketExceptionCostFunction that = (DefaultTokenBucketExceptionCostFunction) o;
62
63 if (defaultExceptionCost != that.defaultExceptionCost) {
64 return false;
65 }
66
67 return throttlingExceptionCost != null ?
68 throttlingExceptionCost.equals(that.throttlingExceptionCost) :
69 that.throttlingExceptionCost == null;
70 }
71
72 @Override
73 public int hashCode() {
74 int result = throttlingExceptionCost != null ? throttlingExceptionCost.hashCode() : 0;
75 result = 31 * result + defaultExceptionCost;
76 return result;
77 }
78
79 public static final class Builder implements TokenBucketExceptionCostFunction.Builder {
80 private Integer throttlingExceptionCost;
81 private Integer defaultExceptionCost;
82
83 public TokenBucketExceptionCostFunction.Builder throttlingExceptionCost(int cost) {
84 this.throttlingExceptionCost = cost;
85 return this;
86 }
87
88 public TokenBucketExceptionCostFunction.Builder defaultExceptionCost(int cost) {
89 this.defaultExceptionCost = cost;
90 return this;
91 }
92
93 public TokenBucketExceptionCostFunction build() {
94 return new DefaultTokenBucketExceptionCostFunction(this);
95 }
96
97 }
98 }
99