View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.nio.reactor;
29  
30  import org.apache.http.annotation.NotThreadSafe;
31  import org.apache.http.util.Args;
32  
33  /**
34   * I/O reactor configuration parameters.
35   *
36   * @since 4.2
37   */
38  @NotThreadSafe
39  public final class IOReactorConfig implements Cloneable {
40  
41      private static final int AVAIL_PROCS = Runtime.getRuntime().availableProcessors();
42  
43      public static final IOReactorConfig DEFAULT = new Builder().build();
44  
45      // TODO: make final
46      private long selectInterval;
47      private long shutdownGracePeriod;
48      private boolean interestOpQueued;
49      private int ioThreadCount;
50      private int soTimeout;
51      private boolean soReuseAddress;
52      private int soLinger;
53      private boolean soKeepAlive;
54      private boolean tcpNoDelay;
55      private int connectTimeout;
56      private int sndBufSize;
57      private int rcvBufSize;
58      private int backlogSize;
59  
60      @Deprecated
61      public IOReactorConfig() {
62          super();
63          this.selectInterval = 1000;
64          this.shutdownGracePeriod = 500;
65          this.interestOpQueued = false;
66          this.ioThreadCount = AVAIL_PROCS;
67          this.soTimeout = 0;
68          this.soReuseAddress = false;
69          this.soLinger = -1;
70          this.soKeepAlive = false;
71          this.tcpNoDelay = true;
72          this.connectTimeout = 0;
73          this.sndBufSize = 0;
74          this.rcvBufSize = 0;
75          this.backlogSize = 0;
76      }
77  
78      IOReactorConfig(
79              final long selectInterval,
80              final long shutdownGracePeriod,
81              final boolean interestOpQueued,
82              final int ioThreadCount,
83              final int soTimeout,
84              final boolean soReuseAddress,
85              final int soLinger,
86              final boolean soKeepAlive,
87              final boolean tcpNoDelay,
88              final int connectTimeout,
89              final int sndBufSize,
90              final int rcvBufSize,
91              final int backlogSize) {
92          super();
93          this.selectInterval = selectInterval;
94          this.shutdownGracePeriod = shutdownGracePeriod;
95          this.interestOpQueued = interestOpQueued;
96          this.ioThreadCount = ioThreadCount;
97          this.soTimeout = soTimeout;
98          this.soReuseAddress = soReuseAddress;
99          this.soLinger = soLinger;
100         this.soKeepAlive = soKeepAlive;
101         this.tcpNoDelay = tcpNoDelay;
102         this.connectTimeout = connectTimeout;
103         this.sndBufSize = sndBufSize;
104         this.rcvBufSize = rcvBufSize;
105         this.backlogSize = backlogSize;
106     }
107 
108     /**
109      * Determines time interval in milliseconds at which the I/O reactor wakes up to check for
110      * timed out sessions and session requests.
111      * <p>
112      * Default: {@code 1000} milliseconds.
113      */
114     public long getSelectInterval() {
115         return this.selectInterval;
116     }
117 
118     /**
119      * @deprecated (4.3) use {@link Builder#setSelectInterval(long)}
120      */
121     @Deprecated
122     public void setSelectInterval(final long selectInterval) {
123         Args.positive(selectInterval, "Select internal");
124         this.selectInterval = selectInterval;
125     }
126 
127     /**
128      * Determines grace period in milliseconds the I/O reactors are expected to block waiting
129      * for individual worker threads to terminate cleanly.
130      * <p>
131      * Default: {@code 500} milliseconds.
132      */
133     public long getShutdownGracePeriod() {
134         return this.shutdownGracePeriod;
135     }
136 
137     /**
138      * @deprecated (4.3) use {@link Builder#setShutdownGracePeriod(long)}
139      */
140     @Deprecated
141     public void setShutdownGracePeriod(final long gracePeriod) {
142         Args.positive(gracePeriod, "Shutdown grace period");
143         this.shutdownGracePeriod = gracePeriod;
144     }
145 
146     /**
147      * Determines whether or not I/O interest operations are to be queued and executed
148      * asynchronously by the I/O reactor thread or to be applied to the underlying
149      * {@link java.nio.channels.SelectionKey} immediately.
150      * <p>
151      * Default: {@code false}
152      *
153      * @see java.nio.channels.SelectionKey
154      * @see java.nio.channels.SelectionKey#interestOps()
155      * @see java.nio.channels.SelectionKey#interestOps(int)
156      */
157     public boolean isInterestOpQueued() {
158         return this.interestOpQueued;
159     }
160 
161     /**
162      * @deprecated (4.3) use {@link Builder#setInterestOpQueued(boolean)}
163      */
164     @Deprecated
165     public void setInterestOpQueued(final boolean interestOpQueued) {
166         this.interestOpQueued = interestOpQueued;
167     }
168 
169     /**
170      * Determines the number of I/O dispatch threads to be used by the I/O reactor.
171      * <p>
172      * Default: {@code 2}
173      */
174     public int getIoThreadCount() {
175         return this.ioThreadCount;
176     }
177 
178     /**
179      * @deprecated (4.3) use {@link Builder#setIoThreadCount(int)}
180      */
181     @Deprecated
182     public void setIoThreadCount(final int ioThreadCount) {
183         Args.positive(ioThreadCount, "I/O thread count");
184         this.ioThreadCount = ioThreadCount;
185     }
186 
187     /**
188      * Determines the default socket timeout value for non-blocking I/O operations.
189      * <p>
190      * Default: {@code 0} (no timeout)
191      *
192      * @see java.net.SocketOptions#SO_TIMEOUT
193      */
194     public int getSoTimeout() {
195         return soTimeout;
196     }
197 
198     /**
199      * @deprecated (4.3) use {@link Builder#setSoTimeout(int)}
200      */
201     @Deprecated
202     public void setSoTimeout(final int soTimeout) {
203         this.soTimeout = soTimeout;
204     }
205 
206     /**
207      * Determines the default value of the {@link java.net.SocketOptions#SO_REUSEADDR} parameter
208      * for newly created sockets.
209      * <p>
210      * Default: {@code false}
211      *
212      * @see java.net.SocketOptions#SO_REUSEADDR
213      */
214     public boolean isSoReuseAddress() {
215         return soReuseAddress;
216     }
217 
218     /**
219      * @deprecated (4.3) use {@link Builder#setSoReuseAddress(boolean)}
220      */
221     @Deprecated
222     public void setSoReuseAddress(final boolean soReuseAddress) {
223         this.soReuseAddress = soReuseAddress;
224     }
225 
226     /**
227      * Determines the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter
228      * for newly created sockets.
229      * <p>
230      * Default: {@code -1}
231      *
232      * @see java.net.SocketOptions#SO_LINGER
233      */
234     public int getSoLinger() {
235         return soLinger;
236     }
237 
238     /**
239      * @deprecated (4.3) use {@link Builder#setSoLinger(int)}
240      */
241     @Deprecated
242     public void setSoLinger(final int soLinger) {
243         this.soLinger = soLinger;
244     }
245 
246     /**
247      * Determines the default value of the {@link java.net.SocketOptions#SO_KEEPALIVE} parameter
248      * for newly created sockets.
249      * <p>
250      * Default: {@code -1}
251      *
252      * @see java.net.SocketOptions#SO_KEEPALIVE
253      */
254     public boolean isSoKeepalive() {
255         return this.soKeepAlive;
256     }
257 
258     /**
259      * @deprecated (4.3) use {@link Builder#setSoKeepAlive(boolean)}
260      */
261     @Deprecated
262     public void setSoKeepalive(final boolean soKeepAlive) {
263         this.soKeepAlive = soKeepAlive;
264     }
265 
266     /**
267      * Determines the default value of the {@link java.net.SocketOptions#TCP_NODELAY} parameter
268      * for newly created sockets.
269      * <p>
270      * Default: {@code false}
271      *
272      * @see java.net.SocketOptions#TCP_NODELAY
273      */
274     public boolean isTcpNoDelay() {
275         return tcpNoDelay;
276     }
277 
278     /**
279      * @deprecated (4.3) use {@link Builder#setTcpNoDelay(boolean)}
280      */
281     @Deprecated
282     public void setTcpNoDelay(final boolean tcpNoDelay) {
283         this.tcpNoDelay = tcpNoDelay;
284     }
285 
286     /**
287      * Determines the default connect timeout value for non-blocking connection requests.
288      * <p>
289      * Default: {@code 0} (no timeout)
290      */
291     public int getConnectTimeout() {
292         return connectTimeout;
293     }
294 
295     /**
296      * @deprecated (4.3) use {@link Builder#setConnectTimeout(int)}
297      */
298     @Deprecated
299     public void setConnectTimeout(final int connectTimeout) {
300         this.connectTimeout = connectTimeout;
301     }
302 
303     /**
304      * Determines the default value of the {@link java.net.SocketOptions#SO_SNDBUF} parameter
305      * for newly created sockets.
306      * <p>
307      * Default: {@code 0} (system default)
308      *
309      * @see java.net.SocketOptions#SO_SNDBUF
310      */
311     public int getSndBufSize() {
312         return sndBufSize;
313     }
314 
315     /**
316      * @deprecated (4.3) use {@link Builder#setSndBufSize(int)}
317      */
318     @Deprecated
319     public void setSndBufSize(final int sndBufSize) {
320         this.sndBufSize = sndBufSize;
321     }
322 
323     /**
324      * Determines the default value of the {@link java.net.SocketOptions#SO_RCVBUF} parameter
325      * for newly created sockets.
326      * <p>
327      * Default: {@code 0} (system default)
328      *
329      * @see java.net.SocketOptions#SO_RCVBUF
330      */
331     public int getRcvBufSize() {
332         return rcvBufSize;
333     }
334 
335     /**
336      * @deprecated (4.3) use {@link Builder#setRcvBufSize(int)}
337      */
338     @Deprecated
339     public void setRcvBufSize(final int rcvBufSize) {
340         this.rcvBufSize = rcvBufSize;
341     }
342 
343     /**
344      * Determines the default backlog size value for server sockets binds.
345      * <p>
346      * Default: {@code 0} (system default)
347      *
348      * @since 4.4
349      */
350     public int getBacklogSize() {
351         return backlogSize;
352     }
353 
354     @Override
355     protected IOReactorConfig clone() throws CloneNotSupportedException {
356         return (IOReactorConfig) super.clone();
357     }
358 
359     public static Builder custom() {
360         return new Builder();
361     }
362 
363     public static Builder copy(final IOReactorConfig config) {
364         Args.notNull(config, "I/O reactor config");
365         return new Builder()
366             .setSelectInterval(config.getSelectInterval())
367             .setShutdownGracePeriod(config.getShutdownGracePeriod())
368             .setInterestOpQueued(config.isInterestOpQueued())
369             .setIoThreadCount(config.getIoThreadCount())
370             .setSoTimeout(config.getSoTimeout())
371             .setSoReuseAddress(config.isSoReuseAddress())
372             .setSoLinger(config.getSoLinger())
373             .setSoKeepAlive(config.isSoKeepalive())
374             .setTcpNoDelay(config.isTcpNoDelay())
375             .setConnectTimeout(config.getConnectTimeout())
376             .setSndBufSize(config.getSndBufSize())
377             .setRcvBufSize(config.getRcvBufSize())
378             .setBacklogSize(config.getBacklogSize());
379     }
380 
381     public static class Builder {
382 
383         private long selectInterval;
384         private long shutdownGracePeriod;
385         private boolean interestOpQueued;
386         private int ioThreadCount;
387         private int soTimeout;
388         private boolean soReuseAddress;
389         private int soLinger;
390         private boolean soKeepAlive;
391         private boolean tcpNoDelay;
392         private int connectTimeout;
393         private int sndBufSize;
394         private int rcvBufSize;
395         private int backlogSize;
396 
397         Builder() {
398             this.selectInterval = 1000;
399             this.shutdownGracePeriod = 500;
400             this.interestOpQueued = false;
401             this.ioThreadCount = AVAIL_PROCS;
402             this.soTimeout = 0;
403             this.soReuseAddress = false;
404             this.soLinger = -1;
405             this.soKeepAlive = false;
406             this.tcpNoDelay = true;
407             this.connectTimeout = 0;
408             this.sndBufSize = 0;
409             this.rcvBufSize = 0;
410             this.backlogSize = 0;
411         }
412 
413         public Builder setSelectInterval(final long selectInterval) {
414             this.selectInterval = selectInterval;
415             return this;
416         }
417 
418         public Builder setShutdownGracePeriod(final long shutdownGracePeriod) {
419             this.shutdownGracePeriod = shutdownGracePeriod;
420             return this;
421         }
422 
423         public Builder setInterestOpQueued(final boolean interestOpQueued) {
424             this.interestOpQueued = interestOpQueued;
425             return this;
426         }
427 
428         public Builder setIoThreadCount(final int ioThreadCount) {
429             this.ioThreadCount = ioThreadCount;
430             return this;
431         }
432 
433         public Builder setSoTimeout(final int soTimeout) {
434             this.soTimeout = soTimeout;
435             return this;
436         }
437 
438         public Builder setSoReuseAddress(final boolean soReuseAddress) {
439             this.soReuseAddress = soReuseAddress;
440             return this;
441         }
442 
443         public Builder setSoLinger(final int soLinger) {
444             this.soLinger = soLinger;
445             return this;
446         }
447 
448         public Builder setSoKeepAlive(final boolean soKeepAlive) {
449             this.soKeepAlive = soKeepAlive;
450             return this;
451         }
452 
453         public Builder setTcpNoDelay(final boolean tcpNoDelay) {
454             this.tcpNoDelay = tcpNoDelay;
455             return this;
456         }
457 
458         public Builder setConnectTimeout(final int connectTimeout) {
459             this.connectTimeout = connectTimeout;
460             return this;
461         }
462 
463         public Builder setSndBufSize(final int sndBufSize) {
464             this.sndBufSize = sndBufSize;
465             return this;
466         }
467 
468         public Builder setRcvBufSize(final int rcvBufSize) {
469             this.rcvBufSize = rcvBufSize;
470             return this;
471         }
472 
473         public Builder setBacklogSize(final int backlogSize) {
474             this.backlogSize = backlogSize;
475             return this;
476         }
477 
478         public IOReactorConfig build() {
479             return new IOReactorConfig(
480                     selectInterval, shutdownGracePeriod, interestOpQueued, ioThreadCount,
481                     soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay,
482                     connectTimeout, sndBufSize, rcvBufSize, backlogSize);
483         }
484 
485     }
486 
487     @Override
488     public String toString() {
489         final StringBuilder builder = new StringBuilder();
490         builder.append("[selectInterval=").append(this.selectInterval)
491                 .append(", shutdownGracePeriod=").append(this.shutdownGracePeriod)
492                 .append(", interestOpQueued=").append(this.interestOpQueued)
493                 .append(", ioThreadCount=").append(this.ioThreadCount)
494                 .append(", soTimeout=").append(this.soTimeout)
495                 .append(", soReuseAddress=").append(this.soReuseAddress)
496                 .append(", soLinger=").append(this.soLinger)
497                 .append(", soKeepAlive=").append(this.soKeepAlive)
498                 .append(", tcpNoDelay=").append(this.tcpNoDelay)
499                 .append(", connectTimeout=").append(this.connectTimeout)
500                 .append(", sndBufSize=").append(this.sndBufSize)
501                 .append(", rcvBufSize=").append(this.rcvBufSize)
502                 .append(", backlogSize=").append(this.backlogSize)
503                 .append("]");
504         return builder.toString();
505     }
506 
507 }