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 package org.apache.hc.client5.http.impl;
28
29 import org.apache.hc.client5.http.HttpRoute;
30 import org.apache.hc.core5.annotation.Internal;
31 import org.apache.hc.core5.pool.ConnPoolControl;
32 import org.apache.hc.core5.pool.PoolStats;
33 import org.apache.hc.core5.util.Identifiable;
34
35 /**
36 * Connection pooling support methods.
37 *
38 * @since 5.0
39 */
40 @Internal
41 public final class ConnPoolSupport {
42
43 public static String getId(final Object object) {
44 if (object == null) {
45 return null;
46 }
47 return object instanceof Identifiable
48 ? ((Identifiable) object).getId()
49 : object.getClass().getSimpleName() + "-"
50 + Integer.toHexString(System.identityHashCode(object));
51 }
52
53 public static String formatStats(
54 final HttpRoute route,
55 final Object state,
56 final ConnPoolControl<HttpRoute> connPool) {
57 final StringBuilder buf = new StringBuilder();
58 buf.append("[route: ").append(route).append("]");
59 if (state != null) {
60 buf.append("[state: ").append(state).append("]");
61 }
62 final PoolStats totals = connPool.getTotalStats();
63 final PoolStats stats = connPool.getStats(route);
64 buf.append("[total available: ").append(totals.getAvailable()).append("; ");
65 buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable());
66 buf.append(" of ").append(stats.getMax()).append("; ");
67 buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
68 buf.append(" of ").append(totals.getMax()).append("]");
69 return buf.toString();
70 }
71
72 }