1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package software.amazon.awssdk.http.apache.internal.conn;
17
18 import org.apache.http.HttpResponse;
19 import org.apache.http.conn.ConnectionKeepAliveStrategy;
20 import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
21 import org.apache.http.protocol.HttpContext;
22 import software.amazon.awssdk.annotations.SdkInternalApi;
23
24 /**
25 * The AWS SDK for Java's implementation of the
26 * {@code ConnectionKeepAliveStrategy} interface. Allows a user-configurable
27 * maximum idle time for connections.
28 */
29 @SdkInternalApi
30 public class SdkConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy {
31
32 private final long maxIdleTime;
33
34 /**
35 * @param maxIdleTime the maximum time a connection may be idle
36 */
37 public SdkConnectionKeepAliveStrategy(long maxIdleTime) {
38 this.maxIdleTime = maxIdleTime;
39 }
40
41 @Override
42 public long getKeepAliveDuration(
43 HttpResponse response,
44 HttpContext context) {
45
46 // If there's a Keep-Alive timeout directive in the response and it's
47 // shorter than our configured max, honor that. Otherwise go with the
48 // configured maximum.
49
50 long duration = DefaultConnectionKeepAliveStrategy.INSTANCE
51 .getKeepAliveDuration(response, context);
52
53 if (0 < duration && duration < maxIdleTime) {
54 return duration;
55 }
56
57 return maxIdleTime;
58 }
59 }
60