1 /*
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
125 }
126
127 synchronized void start() {
128 assert !stopped : "Already stopped";
129 }
130
131 static CacheKey cacheKey(InetSocketAddress destination,
132 InetSocketAddress proxy)
133 {
134 return new CacheKey(destination, proxy);
135 }
136
137 synchronized HttpConnection getConnection(boolean secure,
138 InetSocketAddress addr,
139 InetSocketAddress proxy) {
140 if (stopped) return null;
141 CacheKey key = new CacheKey(addr, proxy);
142 HttpConnection c = secure ? findConnection(key, sslPool)
143 : findConnection(key, plainPool);
144 //System.out.println ("getConnection returning: " + c);
145 return c;
146 }
147
148 /**
149 * Returns the connection to the pool.
150 */
151 void returnToPool(HttpConnection conn) {
152 returnToPool(conn, Instant.now(), KEEP_ALIVE);
153 }
154
155 // Called also by whitebox tests
156 void returnToPool(HttpConnection conn, Instant now, long keepAlive) {
157
158 // Don't call registerCleanupTrigger while holding a lock,
159 // but register it before the connection is added to the pool,
160 // since we don't want to trigger the cleanup if the connection
161 // is not in the pool.
162 CleanupTrigger cleanup = registerCleanupTrigger(conn);
163
164 // it's possible that cleanup may have been called.
165 HttpConnection toClose = null;
166 synchronized(this) {
167 if (cleanup.isDone()) {
168 return;
169 } else if (stopped) {
170 conn.close();
171 return;
172 }
173 if (MAX_POOL_SIZE > 0 && expiryList.size() >= MAX_POOL_SIZE) {
174 toClose = expiryList.removeOldest();
175 if (toClose != null) removeFromPool(toClose);
176 }
177 if (conn instanceof PlainHttpConnection) {
433 java.util.stream.Stream<ExpiryEntry> stream() {
434 return list.stream();
435 }
436
437 // should only be called while holding a synchronization
438 // lock on the ConnectionPool
439 void clear() {
440 list.clear();
441 mayContainEntries = false;
442 }
443 }
444
445 // Remove a connection from the pool.
446 // should only be called while holding a synchronization
447 // lock on the ConnectionPool
448 private void removeFromPool(HttpConnection c) {
449 assert Thread.holdsLock(this);
450 if (c instanceof PlainHttpConnection) {
451 removeFromPool(c, plainPool);
452 } else {
453 assert c.isSecure();
454 removeFromPool(c, sslPool);
455 }
456 }
457
458 // Used by tests
459 synchronized boolean contains(HttpConnection c) {
460 final CacheKey key = c.cacheKey();
461 List<HttpConnection> list;
462 if ((list = plainPool.get(key)) != null) {
463 if (list.contains(c)) return true;
464 }
465 if ((list = sslPool.get(key)) != null) {
466 if (list.contains(c)) return true;
467 }
468 return false;
469 }
470
471 void cleanup(HttpConnection c, Throwable error) {
472 if (debug.on())
473 debug.log("%s : ConnectionPool.cleanup(%s)",
|
1 /*
2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
125 }
126
127 synchronized void start() {
128 assert !stopped : "Already stopped";
129 }
130
131 static CacheKey cacheKey(InetSocketAddress destination,
132 InetSocketAddress proxy)
133 {
134 return new CacheKey(destination, proxy);
135 }
136
137 synchronized HttpConnection getConnection(boolean secure,
138 InetSocketAddress addr,
139 InetSocketAddress proxy) {
140 if (stopped) return null;
141 CacheKey key = new CacheKey(addr, proxy);
142 HttpConnection c = secure ? findConnection(key, sslPool)
143 : findConnection(key, plainPool);
144 //System.out.println ("getConnection returning: " + c);
145 assert c == null || c.isSecure() == secure;
146 return c;
147 }
148
149 /**
150 * Returns the connection to the pool.
151 */
152 void returnToPool(HttpConnection conn) {
153 returnToPool(conn, Instant.now(), KEEP_ALIVE);
154 }
155
156 // Called also by whitebox tests
157 void returnToPool(HttpConnection conn, Instant now, long keepAlive) {
158
159 assert (conn instanceof PlainHttpConnection) || conn.isSecure()
160 : "Attempting to return unsecure connection to SSL pool: "
161 + conn.getClass();
162
163 // Don't call registerCleanupTrigger while holding a lock,
164 // but register it before the connection is added to the pool,
165 // since we don't want to trigger the cleanup if the connection
166 // is not in the pool.
167 CleanupTrigger cleanup = registerCleanupTrigger(conn);
168
169 // it's possible that cleanup may have been called.
170 HttpConnection toClose = null;
171 synchronized(this) {
172 if (cleanup.isDone()) {
173 return;
174 } else if (stopped) {
175 conn.close();
176 return;
177 }
178 if (MAX_POOL_SIZE > 0 && expiryList.size() >= MAX_POOL_SIZE) {
179 toClose = expiryList.removeOldest();
180 if (toClose != null) removeFromPool(toClose);
181 }
182 if (conn instanceof PlainHttpConnection) {
438 java.util.stream.Stream<ExpiryEntry> stream() {
439 return list.stream();
440 }
441
442 // should only be called while holding a synchronization
443 // lock on the ConnectionPool
444 void clear() {
445 list.clear();
446 mayContainEntries = false;
447 }
448 }
449
450 // Remove a connection from the pool.
451 // should only be called while holding a synchronization
452 // lock on the ConnectionPool
453 private void removeFromPool(HttpConnection c) {
454 assert Thread.holdsLock(this);
455 if (c instanceof PlainHttpConnection) {
456 removeFromPool(c, plainPool);
457 } else {
458 assert c.isSecure() : "connection " + c + " is not secure!";
459 removeFromPool(c, sslPool);
460 }
461 }
462
463 // Used by tests
464 synchronized boolean contains(HttpConnection c) {
465 final CacheKey key = c.cacheKey();
466 List<HttpConnection> list;
467 if ((list = plainPool.get(key)) != null) {
468 if (list.contains(c)) return true;
469 }
470 if ((list = sslPool.get(key)) != null) {
471 if (list.contains(c)) return true;
472 }
473 return false;
474 }
475
476 void cleanup(HttpConnection c, Throwable error) {
477 if (debug.on())
478 debug.log("%s : ConnectionPool.cleanup(%s)",
|