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.hc.core5.http.impl.io;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.http.ClassicHttpRequest;
33  import org.apache.hc.core5.http.io.HttpClientConnection;
34  import org.apache.hc.core5.http.io.ResponseOutOfOrderStrategy;
35  import org.apache.hc.core5.util.Args;
36  import org.apache.hc.core5.util.Timeout;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  
41  /**
42   * A {@link ResponseOutOfOrderStrategy} implementation which checks for premature responses every {@link #chunkSize}
43   * bytes. An 8 KiB chunk size is used by default based on testing using values between 4 KiB and 128 KiB. This is
44   * optimized for correctness and results in a maximum upload speed of 8 MiB/s until {@link #maxChunksToCheck} is
45   * reached.
46   *
47   * @since 5.1
48   */
49  @Contract(threading = ThreadingBehavior.IMMUTABLE)
50  public final class MonitoringResponseOutOfOrderStrategy implements ResponseOutOfOrderStrategy {
51  
52      private static final int DEFAULT_CHUNK_SIZE = 8 * 1024;
53  
54      public static final MonitoringResponseOutOfOrderStrategyOutOfOrderStrategy.html#MonitoringResponseOutOfOrderStrategy">MonitoringResponseOutOfOrderStrategy INSTANCE = new MonitoringResponseOutOfOrderStrategy();
55  
56      private final long chunkSize;
57      private final long maxChunksToCheck;
58  
59      /**
60       * Instantiates a default {@link MonitoringResponseOutOfOrderStrategy}. {@link #INSTANCE} may be used instead.
61       */
62      public MonitoringResponseOutOfOrderStrategy() {
63          this(DEFAULT_CHUNK_SIZE);
64      }
65  
66      /**
67       * Instantiates a {@link MonitoringResponseOutOfOrderStrategy} with unlimited {@link #maxChunksToCheck}.
68       *
69       * @param chunkSize The chunk size after which a response check is executed.
70       */
71      public MonitoringResponseOutOfOrderStrategy(final long chunkSize) {
72          this(chunkSize, Long.MAX_VALUE);
73      }
74  
75      /**
76       * Instantiates a {@link MonitoringResponseOutOfOrderStrategy}.
77       *
78       * @param chunkSize The chunk size after which a response check is executed.
79       * @param maxChunksToCheck The maximum number of chunks to check, allowing expensive checks to be avoided
80       *                         after a sufficient portion of the request entity has been transferred.
81       */
82      public MonitoringResponseOutOfOrderStrategy(final long chunkSize, final long maxChunksToCheck) {
83          this.chunkSize = Args.positive(chunkSize, "chunkSize");
84          this.maxChunksToCheck = Args.positive(maxChunksToCheck, "maxChunksToCheck");
85      }
86  
87      @Override
88      public boolean isEarlyResponseDetected(
89              final ClassicHttpRequest request,
90              final HttpClientConnection connection,
91              final InputStream inputStream,
92              final long totalBytesSent,
93              final long nextWriteSize) throws IOException {
94          if (nextWriteStartsNewChunk(totalBytesSent, nextWriteSize)) {
95              final boolean ssl = connection.getSSLSession() != null;
96              return ssl ? connection.isDataAvailable(Timeout.ONE_MILLISECOND) : (inputStream.available() > 0);
97          }
98          return false;
99      }
100 
101     private boolean nextWriteStartsNewChunk(final long totalBytesSent, final long nextWriteSize) {
102         final long currentChunkIndex = Math.min(totalBytesSent / chunkSize, maxChunksToCheck);
103         final long newChunkIndex = Math.min((totalBytesSent + nextWriteSize) / chunkSize, maxChunksToCheck);
104         return currentChunkIndex < newChunkIndex;
105     }
106 
107     @Override
108     public String toString() {
109         return "DefaultResponseOutOfOrderStrategy{chunkSize=" + chunkSize + ", maxChunksToCheck=" + maxChunksToCheck + '}';
110     }
111 }