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