-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathProperties.java
More file actions
314 lines (275 loc) · 10.4 KB
/
Copy pathProperties.java
File metadata and controls
314 lines (275 loc) · 10.4 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2021 The Dapr Authors
* 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 io.dapr.config;
import io.dapr.utils.NetworkUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Global properties for Dapr's SDK, using Supplier so they are dynamically resolved.
*/
public class Properties {
/**
* Dapr's default IP for HTTP and gRPC communication.
*/
private static final String DEFAULT_SIDECAR_IP = NetworkUtils.getHostLoopbackAddress();
/**
* Dapr's default HTTP port.
*/
private static final Integer DEFAULT_HTTP_PORT = 3500;
/**
* Dapr's default gRPC port.
*/
private static final Integer DEFAULT_GRPC_PORT = 50001;
/**
* Dapr's default max retries.
*/
private static final Integer DEFAULT_API_MAX_RETRIES = 0;
/**
* Dapr's default timeout in seconds.
*/
private static final Duration DEFAULT_API_TIMEOUT = Duration.ofMillis(0L);
/**
* Dapr's default String encoding: UTF-8.
*/
private static final Charset DEFAULT_STRING_CHARSET = StandardCharsets.UTF_8;
/**
* Dapr's default timeout in seconds for HTTP client reads.
*/
private static final Integer DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS = 60;
/**
* Dapr's default maximum number of requests for HTTP client to execute concurrently.
*
* <p>Above this requests queue in memory, waiting for the running calls to complete.
* Default is 64 in okhttp which is OK for most case, but for some special case
* which is slow response and high concurrency, the value should set to a little big.
*/
private static final Integer DEFAULT_HTTP_CLIENT_MAX_REQUESTS = 1024;
/**
* Dapr's default maximum number of idle connections of HTTP connection pool.
*
* <p>Attention! This is max IDLE connection, NOT max connection!
* It is also very important for high concurrency cases.
*/
private static final Integer DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS = 128;
/**
* IP for Dapr's sidecar.
*/
public static final Property<String> SIDECAR_IP = new StringProperty(
"dapr.sidecar.ip",
"DAPR_SIDECAR_IP",
DEFAULT_SIDECAR_IP);
/**
* HTTP port for Dapr after checking system property and environment variable.
*/
public static final Property<Integer> HTTP_PORT = new IntegerProperty(
"dapr.http.port",
"DAPR_HTTP_PORT",
DEFAULT_HTTP_PORT);
/**
* GRPC port for Dapr after checking system property and environment variable.
*/
public static final Property<Integer> GRPC_PORT = new IntegerProperty(
"dapr.grpc.port",
"DAPR_GRPC_PORT",
DEFAULT_GRPC_PORT);
/**
* GRPC TLS cert path for Dapr after checking system property and environment variable.
*/
public static final Property<String> GRPC_TLS_CERT_PATH = new StringProperty(
"dapr.grpc.tls.cert.path",
"DAPR_GRPC_TLS_CERT_PATH",
null);
/**
* GRPC TLS key path for Dapr after checking system property and environment variable.
*/
public static final Property<String> GRPC_TLS_KEY_PATH = new StringProperty(
"dapr.grpc.tls.key.path",
"DAPR_GRPC_TLS_KEY_PATH",
null);
/**
* GRPC TLS CA cert path for Dapr after checking system property and environment variable.
* This is used for TLS connections to servers with self-signed certificates.
*/
public static final Property<String> GRPC_TLS_CA_PATH = new StringProperty(
"dapr.grpc.tls.ca.path",
"DAPR_GRPC_TLS_CA_PATH",
null);
/**
* Use insecure TLS mode which still uses TLS but doesn't verify certificates.
* This uses InsecureTrustManagerFactory to trust all certificates.
* This should only be used for testing or in secure environments.
*/
public static final Property<Boolean> GRPC_TLS_INSECURE = new BooleanProperty(
"dapr.grpc.tls.insecure",
"DAPR_GRPC_TLS_INSECURE",
false);
/**
* GRPC endpoint for remote sidecar connectivity.
*/
public static final Property<String> GRPC_ENDPOINT = new StringProperty(
"dapr.grpc.endpoint",
"DAPR_GRPC_ENDPOINT",
null);
/**
* GRPC enable keep alive.
* Environment variable: DAPR_GRPC_ENABLE_KEEP_ALIVE
* System property: dapr.grpc.enable.keep.alive
* Default: false
*/
public static final Property<Boolean> GRPC_ENABLE_KEEP_ALIVE = new BooleanProperty(
"dapr.grpc.enable.keep.alive",
"DAPR_GRPC_ENABLE_KEEP_ALIVE",
false);
/**
* GRPC keep alive time in seconds.
* Environment variable: DAPR_GRPC_KEEP_ALIVE_TIME_SECONDS
* System property: dapr.grpc.keep.alive.time.seconds
* Default: 10 seconds
*/
public static final Property<Duration> GRPC_KEEP_ALIVE_TIME_SECONDS = new SecondsDurationProperty(
"dapr.grpc.keep.alive.time.seconds",
"DAPR_GRPC_KEEP_ALIVE_TIME_SECONDS",
Duration.ofSeconds(10));
/**
* GRPC keep alive timeout in seconds.
* Environment variable: DAPR_GRPC_KEEP_ALIVE_TIMEOUT_SECONDS
* System property: dapr.grpc.keep.alive.timeout.seconds
* Default: 5 seconds
*/
public static final Property<Duration> GRPC_KEEP_ALIVE_TIMEOUT_SECONDS = new SecondsDurationProperty(
"dapr.grpc.keep.alive.timeout.seconds",
"DAPR_GRPC_KEEP_ALIVE_TIMEOUT_SECONDS",
Duration.ofSeconds(5));
/**
* GRPC keep alive without calls.
* Environment variable: DAPR_GRPC_KEEP_ALIVE_WITHOUT_CALLS
* System property: dapr.grpc.keep.alive.without.calls
* Default: true
*/
public static final Property<Boolean> GRPC_KEEP_ALIVE_WITHOUT_CALLS = new BooleanProperty(
"dapr.grpc.keep.alive.without.calls",
"DAPR_GRPC_KEEP_ALIVE_WITHOUT_CALLS",
true);
/**
* GRPC endpoint for remote sidecar connectivity.
*/
public static final Property<String> HTTP_ENDPOINT = new StringProperty(
"dapr.http.endpoint",
"DAPR_HTTP_ENDPOINT",
null);
/**
* Maximum number of retries for retriable exceptions.
*/
public static final Property<Integer> MAX_RETRIES = new IntegerProperty(
"dapr.api.maxRetries",
"DAPR_API_MAX_RETRIES",
DEFAULT_API_MAX_RETRIES);
/**
* Timeout for API calls.
*/
public static final Property<Duration> TIMEOUT = new MillisecondsDurationProperty(
"dapr.api.timeoutMilliseconds",
"DAPR_API_TIMEOUT_MILLISECONDS",
DEFAULT_API_TIMEOUT);
/**
* API token for authentication between App and Dapr's side car.
*/
public static final Property<String> API_TOKEN = new StringProperty(
"dapr.api.token",
"DAPR_API_TOKEN",
null);
/**
* Determines which string encoding is used in Dapr's Java SDK.
*/
public static final Property<Charset> STRING_CHARSET = new GenericProperty<>(
"dapr.string.charset",
"DAPR_STRING_CHARSET",
DEFAULT_STRING_CHARSET,
(s) -> Charset.forName(s));
/**
* Dapr's timeout in seconds for HTTP client reads.
*/
public static final Property<Integer> HTTP_CLIENT_READ_TIMEOUT_SECONDS = new IntegerProperty(
"dapr.http.client.readTimeoutSeconds",
"DAPR_HTTP_CLIENT_READ_TIMEOUT_SECONDS",
DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS);
/**
* Dapr's default maximum number of requests for HTTP client to execute concurrently.
*/
public static final Property<Integer> HTTP_CLIENT_MAX_REQUESTS = new IntegerProperty(
"dapr.http.client.maxRequests",
"DAPR_HTTP_CLIENT_MAX_REQUESTS",
DEFAULT_HTTP_CLIENT_MAX_REQUESTS);
/**
* Dapr's default maximum number of idle connections for HTTP connection pool.
*/
public static final Property<Integer> HTTP_CLIENT_MAX_IDLE_CONNECTIONS = new IntegerProperty(
"dapr.http.client.maxIdleConnections",
"DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS",
DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS);
/**
* Dapr's default maximum inbound message size for GRPC in bytes.
*/
public static final Property<Integer> GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES = new IntegerProperty(
"dapr.grpc.max.inbound.message.size.bytes",
"DAPR_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES",
4194304);
/**
* Dapr's default maximum inbound metadata size for GRPC in bytes.
*/
public static final Property<Integer> GRPC_MAX_INBOUND_METADATA_SIZE_BYTES = new IntegerProperty(
"dapr.grpc.max.inbound.metadata.size.bytes",
"DAPR_GRPC_MAX_INBOUND_METADATA_SIZE_BYTES",
8192);
/**
* Mechanism to override properties set in a static context.
*/
private final Map<String, String> overrides;
/**
* Creates a new instance to handle Properties per instance.
*/
public Properties() {
this.overrides = null;
}
/**
* Creates a new instance to handle Properties per instance.
* @param overridesInput to override static properties
*/
public Properties(Map<?, String> overridesInput) {
this.overrides = overridesInput == null ? Map.of() :
Map.copyOf(overridesInput.entrySet().stream()
.filter(e -> e.getKey() != null)
.filter(e -> e.getValue() != null)
.collect(Collectors.toMap(
entry -> entry.getKey().toString(),
entry -> entry.getValue()
)));
}
/**
* Gets a property value taking in consideration the override values.
* @param <T> type of the property that we want to get the value from
* @param property to override static property value from overrides
* @return the property's value
*/
public <T> T getValue(Property<T> property) {
if (overrides != null) {
String override = overrides.get(property.getName());
return property.get(override);
} else {
return property.get();
}
}
}