forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AbstractRetryingClientBuilder.java
149 lines (127 loc) · 5.7 KB
/
AbstractRetryingClientBuilder.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2017 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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 com.linecorp.armeria.client.retry;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import java.time.Duration;
import javax.annotation.Nullable;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.linecorp.armeria.client.Client;
import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.Response;
/**
* Builds a new {@link AbstractRetryingClient} or its decorator function.
*
* @param <O> the type of incoming {@link Response} of the {@link Client}
*/
public abstract class AbstractRetryingClientBuilder<O extends Response> {
@Nullable
private final RetryRule retryRule;
@Nullable
private final RetryRuleWithContent<O> retryRuleWithContent;
private int maxTotalAttempts = Flags.defaultMaxTotalAttempts();
private long responseTimeoutMillisForEachAttempt = Flags.defaultResponseTimeoutMillis();
/**
* Creates a new builder with the specified {@link RetryRule}.
*/
AbstractRetryingClientBuilder(RetryRule retryRule) {
this(requireNonNull(retryRule, "retryRule"), null);
}
/**
* Creates a new builder with the specified {@link RetryRuleWithContent}.
*/
AbstractRetryingClientBuilder(RetryRuleWithContent<O> retryRuleWithContent) {
this(null, requireNonNull(retryRuleWithContent, "retryRuleWithContent"));
}
private AbstractRetryingClientBuilder(@Nullable RetryRule retryRule,
@Nullable RetryRuleWithContent<O> retryRuleWithContent) {
this.retryRule = retryRule;
this.retryRuleWithContent = retryRuleWithContent;
}
final RetryRule retryRule() {
checkState(retryRule != null, "retryRule is not set.");
return retryRule;
}
final RetryRuleWithContent<O> retryRuleWithContent() {
checkState(retryRuleWithContent != null, "retryRuleWithContent is not set.");
return retryRuleWithContent;
}
/**
* Sets the maximum allowed number of total attempts. If unspecified, the value from
* {@link Flags#defaultMaxTotalAttempts()} will be used.
*
* @return {@code this} to support method chaining.
*/
public AbstractRetryingClientBuilder<O> maxTotalAttempts(int maxTotalAttempts) {
checkArgument(maxTotalAttempts > 0,
"maxTotalAttempts: %s (expected: > 0)", maxTotalAttempts);
this.maxTotalAttempts = maxTotalAttempts;
return this;
}
final int maxTotalAttempts() {
return maxTotalAttempts;
}
/**
* Sets the response timeout for each attempt in milliseconds.
* When requests in {@link AbstractRetryingClient} are made,
* corresponding responses are timed out by this value. {@code 0} disables the timeout.
* It will be set by the default value in {@link Flags#defaultResponseTimeoutMillis()}, if the client
* dose not specify.
*
* @return {@code this} to support method chaining.
*
* @see <a href="https://armeria.dev/docs/client-retry#per-attempt-timeout">Per-attempt timeout</a>
*/
public AbstractRetryingClientBuilder<O> responseTimeoutMillisForEachAttempt(
long responseTimeoutMillisForEachAttempt) {
checkArgument(responseTimeoutMillisForEachAttempt >= 0,
"responseTimeoutMillisForEachAttempt: %s (expected: >= 0)",
responseTimeoutMillisForEachAttempt);
this.responseTimeoutMillisForEachAttempt = responseTimeoutMillisForEachAttempt;
return this;
}
final long responseTimeoutMillisForEachAttempt() {
return responseTimeoutMillisForEachAttempt;
}
/**
* Sets the response timeout for each attempt. When requests in {@link AbstractRetryingClient} are made,
* corresponding responses are timed out by this value. {@code 0} disables the timeout.
*
* @return {@code this} to support method chaining.
*
* @see <a href="https://armeria.dev/docs/client-retry#per-attempt-timeout">Per-attempt timeout</a>
*/
public AbstractRetryingClientBuilder<O> responseTimeoutForEachAttempt(
Duration responseTimeoutForEachAttempt) {
checkArgument(
!requireNonNull(responseTimeoutForEachAttempt, "responseTimeoutForEachAttempt").isNegative(),
"responseTimeoutForEachAttempt: %s (expected: >= 0)", responseTimeoutForEachAttempt);
return responseTimeoutMillisForEachAttempt(responseTimeoutForEachAttempt.toMillis());
}
@Override
public String toString() {
return toStringHelper().toString();
}
final ToStringHelper toStringHelper() {
return MoreObjects.toStringHelper(this).omitNullValues()
.add("retryRule", retryRule)
.add("retryRuleWithContent", retryRuleWithContent)
.add("maxTotalAttempts", maxTotalAttempts)
.add("responseTimeoutMillisForEachAttempt", responseTimeoutMillisForEachAttempt);
}
}