1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package okhttp3;
18
19 import java.util.concurrent.TimeUnit;
20 import okhttp3.internal.connection.RealConnectionPool;
21
22 /**
23 * Manages reuse of HTTP and HTTP/2 connections for reduced network latency. HTTP requests that
24 * share the same {@link Address} may share a {@link Connection}. This class implements the policy
25 * of which connections to keep open for future use.
26 */
27 public final class ConnectionPool {
28 final RealConnectionPool delegate;
29
30 /**
31 * Create a new connection pool with tuning parameters appropriate for a single-user application.
32 * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently
33 * this pool holds up to 5 idle connections which will be evicted after 5 minutes of inactivity.
34 */
35 public ConnectionPool() {
36 this(5, 5, TimeUnit.MINUTES);
37 }
38
39 public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
40 this.delegate = new RealConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
41 }
42
43 /** Returns the number of idle connections in the pool. */
44 public int idleConnectionCount() {
45 return delegate.idleConnectionCount();
46 }
47
48 /** Returns total number of connections in the pool. */
49 public int connectionCount() {
50 return delegate.connectionCount();
51 }
52
53 /** Close and remove all idle connections in the pool. */
54 public void evictAll() {
55 delegate.evictAll();
56 }
57 }
58