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 public boolean isSoKeepalive() {
135 return this.soKeepAlive;
136 }
137
138
139
140
141 public boolean isTcpNoDelay() {
142 return tcpNoDelay;
143 }
144
145
146
147
148
149
150 public int getTrafficClass() {
151 return trafficClass;
152 }
153
154
155
156
157 public int getSndBufSize() {
158 return sndBufSize;
159 }
160
161
162
163
164 public int getRcvBufSize() {
165 return rcvBufSize;
166 }
167
168
169
170
171 public int getBacklogSize() {
172 return backlogSize;
173 }
174
175
176
177
178 public SocketAddress getSocksProxyAddress() {
179 return this.socksProxyAddress;
180 }
181
182
183
184
185 public String getSocksProxyUsername() {
186 return this.socksProxyUsername;
187 }
188
189
190
191
192 public String getSocksProxyPassword() {
193 return this.socksProxyPassword;
194 }
195
196 public static Builder custom() {
197 return new Builder();
198 }
199
200 public static Builder copy(final IOReactorConfig config) {
201 Args.notNull(config, "I/O reactor config");
202 return new Builder()
203 .setSelectInterval(config.getSelectInterval())
204 .setIoThreadCount(config.getIoThreadCount())
205 .setSoTimeout(config.getSoTimeout())
206 .setSoReuseAddress(config.isSoReuseAddress())
207 .setSoLinger(config.getSoLinger())
208 .setSoKeepAlive(config.isSoKeepalive())
209 .setTcpNoDelay(config.isTcpNoDelay())
210 .setSndBufSize(config.getSndBufSize())
211 .setRcvBufSize(config.getRcvBufSize())
212 .setBacklogSize(config.getBacklogSize())
213 .setSocksProxyAddress(config.getSocksProxyAddress())
214 .setSocksProxyUsername(config.getSocksProxyUsername())
215 .setSocksProxyPassword(config.getSocksProxyPassword());
216 }
217
218 public static class Builder {
219
220 private static int defaultMaxIOThreadCount = -1;
221
222
223
224
225
226
227
228
229
230 public static int getDefaultMaxIOThreadCount() {
231 return defaultMaxIOThreadCount > 0 ? defaultMaxIOThreadCount : Runtime.getRuntime().availableProcessors();
232 }
233
234
235
236
237
238
239
240
241
242
243 public static void setDefaultMaxIOThreadCount(final int defaultMaxIOThreadCount) {
244 Builder.defaultMaxIOThreadCount = defaultMaxIOThreadCount;
245 }
246
247 private TimeValue selectInterval;
248 private int ioThreadCount;
249 private Timeout soTimeout;
250 private boolean soReuseAddress;
251 private TimeValue soLinger;
252 private boolean soKeepAlive;
253 private boolean tcpNoDelay;
254 private int trafficClass;
255 private int sndBufSize;
256 private int rcvBufSize;
257 private int backlogSize;
258 private SocketAddress socksProxyAddress;
259 private String socksProxyUsername;
260 private String socksProxyPassword;
261
262 Builder() {
263 this.selectInterval = TimeValue.ofSeconds(1);
264 this.ioThreadCount = Builder.getDefaultMaxIOThreadCount();
265 this.soTimeout = Timeout.ZERO_MILLISECONDS;
266 this.soReuseAddress = false;
267 this.soLinger = TimeValue.NEG_ONE_SECOND;
268 this.soKeepAlive = false;
269 this.tcpNoDelay = true;
270 this.trafficClass = 0;
271 this.sndBufSize = 0;
272 this.rcvBufSize = 0;
273 this.backlogSize = 0;
274 this.socksProxyAddress = null;
275 this.socksProxyUsername = null;
276 this.socksProxyPassword = null;
277 }
278
279
280
281
282
283
284
285
286 public Builder setSelectInterval(final TimeValue selectInterval) {
287 this.selectInterval = selectInterval;
288 return this;
289 }
290
291
292
293
294
295
296
297 public Builder setIoThreadCount(final int ioThreadCount) {
298 this.ioThreadCount = ioThreadCount;
299 return this;
300 }
301
302
303
304
305
306
307
308
309
310 public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) {
311 this.soTimeout = Timeout.of(soTimeout, timeUnit);
312 return this;
313 }
314
315
316
317
318
319
320
321
322
323 public Builder setSoTimeout(final Timeout soTimeout) {
324 this.soTimeout = soTimeout;
325 return this;
326 }
327
328
329
330
331
332
333
334
335
336
337 public Builder setSoReuseAddress(final boolean soReuseAddress) {
338 this.soReuseAddress = soReuseAddress;
339 return this;
340 }
341
342
343
344
345
346
347
348
349
350
351 public Builder setSoLinger(final int soLinger, final TimeUnit timeUnit) {
352 this.soLinger = TimeValue.of(soLinger, timeUnit);
353 return this;
354 }
355
356
357
358
359
360
361
362
363
364
365 public Builder setSoLinger(final TimeValue soLinger) {
366 this.soLinger = soLinger;
367 return this;
368 }
369
370
371
372
373
374
375
376
377
378
379 public Builder setSoKeepAlive(final boolean soKeepAlive) {
380 this.soKeepAlive = soKeepAlive;
381 return this;
382 }
383
384
385
386
387
388
389
390
391
392
393 public Builder setTcpNoDelay(final boolean tcpNoDelay) {
394 this.tcpNoDelay = tcpNoDelay;
395 return this;
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409 public Builder setTrafficClass(final int trafficClass) {
410 this.trafficClass = trafficClass;
411 return this;
412 }
413
414
415
416
417
418
419
420
421
422
423 public Builder setSndBufSize(final int sndBufSize) {
424 this.sndBufSize = sndBufSize;
425 return this;
426 }
427
428
429
430
431
432
433
434
435
436
437 public Builder setRcvBufSize(final int rcvBufSize) {
438 this.rcvBufSize = rcvBufSize;
439 return this;
440 }
441
442
443
444
445
446
447
448
449
450 public Builder setBacklogSize(final int backlogSize) {
451 this.backlogSize = backlogSize;
452 return this;
453 }
454
455
456
457
458 public Builder setSocksProxyAddress(final SocketAddress socksProxyAddress) {
459 this.socksProxyAddress = socksProxyAddress;
460 return this;
461 }
462
463
464
465
466 public Builder setSocksProxyUsername(final String socksProxyUsername) {
467 this.socksProxyUsername = socksProxyUsername;
468 return this;
469 }
470
471
472
473
474 public Builder setSocksProxyPassword(final String socksProxyPassword) {
475 this.socksProxyPassword = socksProxyPassword;
476 return this;
477 }
478
479 public IOReactorConfig build() {
480 return new IOReactorConfig(
481 selectInterval != null ? selectInterval : TimeValue.ofSeconds(1),
482 ioThreadCount,
483 Timeout.defaultsToDisabled(soTimeout),
484 soReuseAddress,
485 TimeValue.defaultsToNegativeOneMillisecond(soLinger),
486 soKeepAlive,
487 tcpNoDelay,
488 trafficClass,
489 sndBufSize, rcvBufSize, backlogSize,
490 socksProxyAddress, socksProxyUsername, socksProxyPassword);
491 }
492
493 }
494
495 @Override
496 public String toString() {
497 final StringBuilder builder = new StringBuilder();
498 builder.append("[selectInterval=").append(this.selectInterval)
499 .append(", ioThreadCount=").append(this.ioThreadCount)
500 .append(", soTimeout=").append(this.soTimeout)
501 .append(", soReuseAddress=").append(this.soReuseAddress)
502 .append(", soLinger=").append(this.soLinger)
503 .append(", soKeepAlive=").append(this.soKeepAlive)
504 .append(", tcpNoDelay=").append(this.tcpNoDelay)
505 .append(", trafficClass=").append(this.trafficClass)
506 .append(", sndBufSize=").append(this.sndBufSize)
507 .append(", rcvBufSize=").append(this.rcvBufSize)
508 .append(", backlogSize=").append(this.backlogSize)
509 .append(", socksProxyAddress=").append(this.socksProxyAddress)
510 .append("]");
511 return builder.toString();
512 }
513
514 }