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 package org.apache.hc.core5.reactor;
29
30 import java.net.SocketAddress;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.util.Args;
36 import org.apache.hc.core5.util.TimeValue;
37 import org.apache.hc.core5.util.Timeout;
38
39
40
41
42
43
44 @Contract(threading = ThreadingBehavior.IMMUTABLE)
45 public final class IOReactorConfig {
46
47 public static final IOReactorConfig DEFAULT = new Builder().build();
48
49 private final TimeValue selectInterval;
50 private final int ioThreadCount;
51 private final Timeout soTimeout;
52 private final boolean soReuseAddress;
53 private final TimeValue soLinger;
54 private final boolean soKeepAlive;
55 private final boolean tcpNoDelay;
56 private final int trafficClass;
57 private final int sndBufSize;
58 private final int rcvBufSize;
59 private final int backlogSize;
60 private final SocketAddress socksProxyAddress;
61 private final String socksProxyUsername;
62 private final String socksProxyPassword;
63
64 IOReactorConfig(
65 final TimeValue selectInterval,
66 final int ioThreadCount,
67 final Timeout soTimeout,
68 final boolean soReuseAddress,
69 final TimeValue soLinger,
70 final boolean soKeepAlive,
71 final boolean tcpNoDelay,
72 final int trafficClass,
73 final int sndBufSize,
74 final int rcvBufSize,
75 final int backlogSize,
76 final SocketAddress socksProxyAddress,
77 final String socksProxyUsername,
78 final String socksProxyPassword) {
79 super();
80 this.selectInterval = selectInterval;
81 this.ioThreadCount = ioThreadCount;
82 this.soTimeout = soTimeout;
83 this.soReuseAddress = soReuseAddress;
84 this.soLinger = soLinger;
85 this.soKeepAlive = soKeepAlive;
86 this.tcpNoDelay = tcpNoDelay;
87 this.trafficClass = trafficClass;
88 this.sndBufSize = sndBufSize;
89 this.rcvBufSize = rcvBufSize;
90 this.backlogSize = backlogSize;
91 this.socksProxyAddress = socksProxyAddress;
92 this.socksProxyUsername = socksProxyUsername;
93 this.socksProxyPassword = socksProxyPassword;
94 }
95
96
97
98
99 public TimeValue getSelectInterval() {
100 return this.selectInterval;
101 }
102
103
104
105
106 public int getIoThreadCount() {
107 return this.ioThreadCount;
108 }
109
110
111
112
113 public Timeout getSoTimeout() {
114 return soTimeout;
115 }
116
117
118
119
120 public boolean isSoReuseAddress() {
121 return soReuseAddress;
122 }
123
124
125
126
127 public TimeValue getSoLinger() {
128 return soLinger;
129 }
130
131
132
133
134
135 public boolean isSoKeepAlive() {
136 return this.soKeepAlive;
137 }
138
139
140
141
142
143 @Deprecated
144 public boolean isSoKeepalive() {
145 return this.soKeepAlive;
146 }
147
148
149
150
151 public boolean isTcpNoDelay() {
152 return tcpNoDelay;
153 }
154
155
156
157
158
159
160 public int getTrafficClass() {
161 return trafficClass;
162 }
163
164
165
166
167 public int getSndBufSize() {
168 return sndBufSize;
169 }
170
171
172
173
174 public int getRcvBufSize() {
175 return rcvBufSize;
176 }
177
178
179
180
181 public int getBacklogSize() {
182 return backlogSize;
183 }
184
185
186
187
188 public SocketAddress getSocksProxyAddress() {
189 return this.socksProxyAddress;
190 }
191
192
193
194
195 public String getSocksProxyUsername() {
196 return this.socksProxyUsername;
197 }
198
199
200
201
202 public String getSocksProxyPassword() {
203 return this.socksProxyPassword;
204 }
205
206 public static Builder custom() {
207 return new Builder();
208 }
209
210 public static Builder copy(final IOReactorConfig config) {
211 Args.notNull(config, "I/O reactor config");
212 return new Builder()
213 .setSelectInterval(config.getSelectInterval())
214 .setIoThreadCount(config.getIoThreadCount())
215 .setSoTimeout(config.getSoTimeout())
216 .setSoReuseAddress(config.isSoReuseAddress())
217 .setSoLinger(config.getSoLinger())
218 .setSoKeepAlive(config.isSoKeepAlive())
219 .setTcpNoDelay(config.isTcpNoDelay())
220 .setSndBufSize(config.getSndBufSize())
221 .setRcvBufSize(config.getRcvBufSize())
222 .setBacklogSize(config.getBacklogSize())
223 .setSocksProxyAddress(config.getSocksProxyAddress())
224 .setSocksProxyUsername(config.getSocksProxyUsername())
225 .setSocksProxyPassword(config.getSocksProxyPassword());
226 }
227
228 public static class Builder {
229
230 private static int defaultMaxIOThreadCount = -1;
231
232
233
234
235
236
237
238
239
240 public static int getDefaultMaxIOThreadCount() {
241 return defaultMaxIOThreadCount > 0 ? defaultMaxIOThreadCount : Runtime.getRuntime().availableProcessors();
242 }
243
244
245
246
247
248
249
250
251
252
253 public static void setDefaultMaxIOThreadCount(final int defaultMaxIOThreadCount) {
254 Builder.defaultMaxIOThreadCount = defaultMaxIOThreadCount;
255 }
256
257 private TimeValue selectInterval;
258 private int ioThreadCount;
259 private Timeout soTimeout;
260 private boolean soReuseAddress;
261 private TimeValue soLinger;
262 private boolean soKeepAlive;
263 private boolean tcpNoDelay;
264 private int trafficClass;
265 private int sndBufSize;
266 private int rcvBufSize;
267 private int backlogSize;
268 private SocketAddress socksProxyAddress;
269 private String socksProxyUsername;
270 private String socksProxyPassword;
271
272 Builder() {
273 this.selectInterval = TimeValue.ofSeconds(1);
274 this.ioThreadCount = Builder.getDefaultMaxIOThreadCount();
275 this.soTimeout = Timeout.ZERO_MILLISECONDS;
276 this.soReuseAddress = false;
277 this.soLinger = TimeValue.NEG_ONE_SECOND;
278 this.soKeepAlive = false;
279 this.tcpNoDelay = true;
280 this.trafficClass = 0;
281 this.sndBufSize = 0;
282 this.rcvBufSize = 0;
283 this.backlogSize = 0;
284 this.socksProxyAddress = null;
285 this.socksProxyUsername = null;
286 this.socksProxyPassword = null;
287 }
288
289
290
291
292
293
294
295
296 public Builder setSelectInterval(final TimeValue selectInterval) {
297 this.selectInterval = selectInterval;
298 return this;
299 }
300
301
302
303
304
305
306
307 public Builder setIoThreadCount(final int ioThreadCount) {
308 this.ioThreadCount = ioThreadCount;
309 return this;
310 }
311
312
313
314
315
316
317
318
319
320 public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) {
321 this.soTimeout = Timeout.of(soTimeout, timeUnit);
322 return this;
323 }
324
325
326
327
328
329
330
331
332
333 public Builder setSoTimeout(final Timeout soTimeout) {
334 this.soTimeout = soTimeout;
335 return this;
336 }
337
338
339
340
341
342
343
344
345
346
347 public Builder setSoReuseAddress(final boolean soReuseAddress) {
348 this.soReuseAddress = soReuseAddress;
349 return this;
350 }
351
352
353
354
355
356
357
358
359
360
361 public Builder setSoLinger(final int soLinger, final TimeUnit timeUnit) {
362 this.soLinger = TimeValue.of(soLinger, timeUnit);
363 return this;
364 }
365
366
367
368
369
370
371
372
373
374
375 public Builder setSoLinger(final TimeValue soLinger) {
376 this.soLinger = soLinger;
377 return this;
378 }
379
380
381
382
383
384
385
386
387
388
389 public Builder setSoKeepAlive(final boolean soKeepAlive) {
390 this.soKeepAlive = soKeepAlive;
391 return this;
392 }
393
394
395
396
397
398
399
400
401
402
403 public Builder setTcpNoDelay(final boolean tcpNoDelay) {
404 this.tcpNoDelay = tcpNoDelay;
405 return this;
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419 public Builder setTrafficClass(final int trafficClass) {
420 this.trafficClass = trafficClass;
421 return this;
422 }
423
424
425
426
427
428
429
430
431
432
433 public Builder setSndBufSize(final int sndBufSize) {
434 this.sndBufSize = sndBufSize;
435 return this;
436 }
437
438
439
440
441
442
443
444
445
446
447 public Builder setRcvBufSize(final int rcvBufSize) {
448 this.rcvBufSize = rcvBufSize;
449 return this;
450 }
451
452
453
454
455
456
457
458
459
460 public Builder setBacklogSize(final int backlogSize) {
461 this.backlogSize = backlogSize;
462 return this;
463 }
464
465
466
467
468 public Builder setSocksProxyAddress(final SocketAddress socksProxyAddress) {
469 this.socksProxyAddress = socksProxyAddress;
470 return this;
471 }
472
473
474
475
476 public Builder setSocksProxyUsername(final String socksProxyUsername) {
477 this.socksProxyUsername = socksProxyUsername;
478 return this;
479 }
480
481
482
483
484 public Builder setSocksProxyPassword(final String socksProxyPassword) {
485 this.socksProxyPassword = socksProxyPassword;
486 return this;
487 }
488
489 public IOReactorConfig build() {
490 return new IOReactorConfig(
491 selectInterval != null ? selectInterval : TimeValue.ofSeconds(1),
492 ioThreadCount,
493 Timeout.defaultsToDisabled(soTimeout),
494 soReuseAddress,
495 TimeValue.defaultsToNegativeOneMillisecond(soLinger),
496 soKeepAlive,
497 tcpNoDelay,
498 trafficClass,
499 sndBufSize, rcvBufSize, backlogSize,
500 socksProxyAddress, socksProxyUsername, socksProxyPassword);
501 }
502
503 }
504
505 @Override
506 public String toString() {
507 final StringBuilder builder = new StringBuilder();
508 builder.append("[selectInterval=").append(this.selectInterval)
509 .append(", ioThreadCount=").append(this.ioThreadCount)
510 .append(", soTimeout=").append(this.soTimeout)
511 .append(", soReuseAddress=").append(this.soReuseAddress)
512 .append(", soLinger=").append(this.soLinger)
513 .append(", soKeepAlive=").append(this.soKeepAlive)
514 .append(", tcpNoDelay=").append(this.tcpNoDelay)
515 .append(", trafficClass=").append(this.trafficClass)
516 .append(", sndBufSize=").append(this.sndBufSize)
517 .append(", rcvBufSize=").append(this.rcvBufSize)
518 .append(", backlogSize=").append(this.backlogSize)
519 .append(", socksProxyAddress=").append(this.socksProxyAddress)
520 .append("]");
521 return builder.toString();
522 }
523
524 }