001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.pool;
028
029import org.apache.http.annotation.ThreadingBehavior;
030import org.apache.http.annotation.Contract;
031
032import java.io.Serializable;
033
034/**
035 * Pool statistics.
036 * <p>
037 * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
038 * </p>
039 *
040 * @since 4.2
041 */
042@Contract(threading = ThreadingBehavior.IMMUTABLE)
043public class PoolStats implements Serializable {
044
045    private static final long serialVersionUID = -2807686144795228544L;
046
047    private final int leased;
048    private final int pending;
049    private final int available;
050    private final int max;
051
052    public PoolStats(final int leased, final int pending, final int free, final int max) {
053        super();
054        this.leased = leased;
055        this.pending = pending;
056        this.available = free;
057        this.max = max;
058    }
059
060    /**
061     * Gets the number of persistent connections tracked by the connection manager currently being used to execute
062     * requests.
063     * <p>
064     * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
065     * </p>
066     *
067     * @return the number of persistent connections.
068     */
069    public int getLeased() {
070        return this.leased;
071    }
072
073    /**
074     * Gets the number of connection requests being blocked awaiting a free connection. This can happen only if there
075     * are more worker threads contending for fewer connections.
076     *
077     * @return the number of connection requests being blocked awaiting a free connection.
078     */
079    public int getPending() {
080        return this.pending;
081    }
082
083    /**
084     * Gets the number idle persistent connections.
085     * <p>
086     * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
087     * </p>
088     *
089     * @return number idle persistent connections.
090     */
091    public int getAvailable() {
092        return this.available;
093    }
094
095    /**
096     * Gets the maximum number of allowed persistent connections.
097     *
098     * @return the maximum number of allowed persistent connections.
099     */
100    public int getMax() {
101        return this.max;
102    }
103
104    @Override
105    public String toString() {
106        final StringBuilder buffer = new StringBuilder();
107        buffer.append("[leased: ");
108        buffer.append(this.leased);
109        buffer.append("; pending: ");
110        buffer.append(this.pending);
111        buffer.append("; available: ");
112        buffer.append(this.available);
113        buffer.append("; max: ");
114        buffer.append(this.max);
115        buffer.append("]");
116        return buffer.toString();
117    }
118
119}