1 /*
2 * Copyright 2015-2020 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 package com.amazonaws.http.conn;
16
17 import org.apache.http.HttpResponse;
18 import org.apache.http.conn.ConnectionKeepAliveStrategy;
19 import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
20 import org.apache.http.protocol.HttpContext;
21
22 /**
23 * The AWS SDK for Java's implementation of the
24 * {@code ConnectionKeepAliveStrategy} interface. Allows a user-configurable
25 * maximum idle time for connections.
26 */
27 public class SdkConnectionKeepAliveStrategy
28 implements ConnectionKeepAliveStrategy {
29
30 private final long maxIdleTime;
31
32 /**
33 * @param maxIdleTime the maximum time a connection may be idle
34 */
35 public SdkConnectionKeepAliveStrategy(long maxIdleTime) {
36 this.maxIdleTime = maxIdleTime;
37 }
38
39 @Override
40 public long getKeepAliveDuration(
41 HttpResponse response,
42 HttpContext context) {
43
44 // If there's a Keep-Alive timeout directive in the response and it's
45 // shorter than our configured max, honor that. Otherwise go with the
46 // configured maximum.
47
48 long duration = DefaultConnectionKeepAliveStrategy.INSTANCE
49 .getKeepAliveDuration(response, context);
50
51 if (0 < duration && duration < maxIdleTime) {
52 return duration;
53 }
54
55 return maxIdleTime;
56 }
57 }
58