src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
JDK-8229867: first prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.NotSerializableException;
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    30
import java.io.ObjectInputStream;
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    31
import java.io.ObjectOutputStream;
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    32
import java.net.URL;
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    33
import java.security.AccessController;
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    34
import java.security.PrivilegedAction;
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    35
import java.util.ArrayDeque;
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    36
import java.util.ArrayList;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    37
import java.util.HashMap;
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    38
import java.util.List;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    39
import java.util.concurrent.locks.Lock;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    40
import java.util.concurrent.locks.ReentrantLock;
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    41
35786
9d26db88aa14 8149653: Move sun.misc.InnocuousThread to jdk.internal.misc
chegar
parents: 29986
diff changeset
    42
import jdk.internal.misc.InnocuousThread;
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    43
import sun.security.action.GetIntegerAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A class that implements a cache of idle Http connections for keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Stephen R. Pietrowicz (NCSA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Dave Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    51
public class KeepAliveCache
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    52
    extends HashMap<KeepAliveKey, ClientVector>
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    53
    implements Runnable {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
    54
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long serialVersionUID = -2937172892064557949L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* maximum # keep-alive connections to maintain at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * This should be 2 by the HTTP spec, but because we don't support pipe-lining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * a larger value is more appropriate. So we now set a default of 5, and the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * refers to the number of idle connections per destination (in the cache) only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * It can be reset by setting system property "http.maxConnections".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final int MAX_CONNECTIONS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static int result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static int getMaxConnections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (result == -1) {
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    67
            result = AccessController.doPrivileged(
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    68
                new GetIntegerAction("http.maxConnections", MAX_CONNECTIONS))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                .intValue();
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    70
            if (result <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                result = MAX_CONNECTIONS;
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    72
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
    74
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final int LIFETIME = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    79
    private final ReentrantLock cacheLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Thread keepAliveTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public KeepAliveCache() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Register this URL and HttpClient (that supports keep-alive) with the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param url  The URL contains info about the host and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param http The HttpClient to be cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    92
    public void put(final URL url, Object obj, HttpClient http) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    93
        cacheLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    94
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    95
            boolean startThread = (keepAliveTimer == null);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    96
            if (!startThread) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    97
                if (!keepAliveTimer.isAlive()) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    98
                    startThread = true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    99
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   101
            if (startThread) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   102
                clear();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   103
                /* Unfortunately, we can't always believe the keep-alive timeout we got
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   104
                 * back from the server.  If I'm connected through a Netscape proxy
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   105
                 * to a server that sent me a keep-alive
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   106
                 * time of 15 sec, the proxy unilaterally terminates my connection
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   107
                 * The robustness to get around this is in HttpClient.parseHTTP()
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   108
                 */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   109
                final KeepAliveCache cache = this;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   110
                AccessController.doPrivileged(new PrivilegedAction<>() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   111
                    public Void run() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   112
                        keepAliveTimer = InnocuousThread.newSystemThread("Keep-Alive-Timer", cache);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   113
                        keepAliveTimer.setDaemon(true);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   114
                        keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   115
                        keepAliveTimer.start();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   116
                        return null;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   117
                    }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   118
                });
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   119
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   121
            KeepAliveKey key = new KeepAliveKey(url, obj);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   122
            ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   124
            if (v == null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   125
                int keepAliveTimeout = http.getKeepAliveTimeout();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   126
                v = new ClientVector(keepAliveTimeout > 0 ?
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   127
                        keepAliveTimeout * 1000 : LIFETIME);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   128
                v.put(http);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   129
                super.put(key, v);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   130
            } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   131
                v.put(http);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   132
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   133
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   134
            cacheLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   138
    /* remove an obsolete HttpClient from its VectorCache */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   139
    public void remove(HttpClient h, Object obj) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   140
        cacheLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   141
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   142
            KeepAliveKey key = new KeepAliveKey(h.url, obj);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   143
            ClientVector v = super.get(key);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   144
            if (v != null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   145
                v.remove(h);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   146
                if (v.isEmpty()) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   147
                    super.remove(key);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   148
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   150
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   151
            cacheLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Check to see if this URL has a cached HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   158
    public HttpClient get(URL url, Object obj) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   159
        cacheLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   160
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   161
            KeepAliveKey key = new KeepAliveKey(url, obj);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   162
            ClientVector v = super.get(key);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   163
            if (v == null) { // nothing in cache yet
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   164
                return null;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   165
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   166
            return v.get();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   167
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   168
            cacheLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /* Sleeps for an alloted timeout, then checks for timed out connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Errs on the side of caution (leave connections idle for a relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * short time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   176
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                Thread.sleep(LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } catch (InterruptedException e) {}
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   182
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   183
            // Remove all outdated HttpClients.
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   184
            cacheLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   185
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                long currentTime = System.currentTimeMillis();
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   187
                List<KeepAliveKey> keysToRemove = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   189
                for (KeepAliveKey key : keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   190
                    ClientVector v = get(key);
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   191
                    final Lock lock = v.lock;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   192
                    lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   193
                    try {
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   194
                        KeepAliveEntry e = v.peek();
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   195
                        while (e != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            if ((currentTime - e.idleStartTime) > v.nap) {
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   197
                                v.poll();
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   198
                                e.hc.closeServer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            }
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   202
                            e = v.peek();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   205
                        if (v.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            keysToRemove.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   208
                    } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   209
                        lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   212
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   213
                for (KeepAliveKey key : keysToRemove) {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   214
                    super.remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   216
            } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   217
                cacheLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   219
        } while (!isEmpty());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
   225
    @java.io.Serial
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   226
    private void writeObject(ObjectOutputStream stream) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
   230
    @java.io.Serial
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   231
    private void readObject(ObjectInputStream stream)
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   232
        throws IOException, ClassNotFoundException
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   233
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
/* FILO order for recycling HttpClients, should run in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * to time them out.  If > maxConns are in use, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 */
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   241
class ClientVector extends ArrayDeque<KeepAliveEntry> {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
   242
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private static final long serialVersionUID = -8680532108106489459L;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   244
    final ReentrantLock lock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // sleep time in milliseconds, before cache clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    int nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   249
    ClientVector(int nap) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this.nap = nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   253
    HttpClient get() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   254
        lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   255
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   256
            if (isEmpty()) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   257
                return null;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   258
            }
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   259
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   260
            // Loop until we find a connection that has not timed out
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   261
            HttpClient hc = null;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   262
            long currentTime = System.currentTimeMillis();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   263
            do {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   264
                KeepAliveEntry e = pop();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   265
                if ((currentTime - e.idleStartTime) > nap) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   266
                    e.hc.closeServer();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   267
                } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   268
                    hc = e.hc;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   269
                }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   270
            } while ((hc == null) && (!isEmpty()));
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   271
            return hc;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   272
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   273
            lock.unlock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   274
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /* return a still valid, unused HttpClient */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   278
    void put(HttpClient h) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   279
        lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   280
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   281
            if (size() >= KeepAliveCache.getMaxConnections()) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   282
                h.closeServer(); // otherwise the connection remains in limbo
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   283
            } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   284
                push(new KeepAliveEntry(h, System.currentTimeMillis()));
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   285
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   286
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   287
            lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   291
    /* remove an HttpClient */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   292
    boolean remove(HttpClient h) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   293
        lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   294
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   295
            for (KeepAliveEntry curr : this) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   296
                if (curr.hc == h) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   297
                    return super.remove(curr);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   298
                }
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   299
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   300
            return false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   301
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   302
            lock.unlock();
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   303
        }
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   304
    }
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   305
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
   309
    @java.io.Serial
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   310
    private void writeObject(ObjectOutputStream stream) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47361
diff changeset
   314
    @java.io.Serial
47361
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   315
    private void readObject(ObjectInputStream stream)
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   316
        throws IOException, ClassNotFoundException
74e1913a98c0 8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents: 47216
diff changeset
   317
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
class KeepAliveKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private String      protocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private String      host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private int         port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private Object      obj = null; // additional key, such as socketfactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param url the URL containing the protocol, host and port information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public KeepAliveKey(URL url, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        this.protocol = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        this.port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Determine whether or not two objects of this type are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   343
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if ((obj instanceof KeepAliveKey) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        KeepAliveKey kae = (KeepAliveKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return host.equals(kae.host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            && (port == kae.port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            && protocol.equals(kae.protocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            && this.obj == kae.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The hashCode() for this object is the string hashCode() of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * concatenation of the protocol, host name and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   358
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        String str = protocol+host+port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return this.obj == null? str.hashCode() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            str.hashCode() + this.obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
class KeepAliveEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    long idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    KeepAliveEntry(HttpClient hc, long idleStartTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        this.idleStartTime = idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
}