Path: blob/master/src/java.base/share/classes/sun/net/www/http/KeepAliveStreamCleaner.java
41161 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.www.http;2627import java.io.IOException;28import java.util.LinkedList;29import sun.net.NetProperties;30import java.security.AccessController;31import java.security.PrivilegedAction;32import java.util.concurrent.TimeUnit;33import java.util.concurrent.locks.Condition;34import java.util.concurrent.locks.ReentrantLock;3536/**37* This class is used to cleanup any remaining data that may be on a KeepAliveStream38* so that the connection can be cached in the KeepAliveCache.39* Instances of this class can be used as a FIFO queue for KeepAliveCleanerEntry objects.40* Executing this Runnable removes each KeepAliveCleanerEntry from the Queue, reads41* the reamining bytes on its KeepAliveStream, and if successful puts the connection in42* the KeepAliveCache.43*44* @author Chris Hegarty45*/4647@SuppressWarnings("serial") // never serialized48class KeepAliveStreamCleaner49extends LinkedList<KeepAliveCleanerEntry>50implements Runnable51{52// maximum amount of remaining data that we will try to cleanup53protected static int MAX_DATA_REMAINING = 512;5455// maximum amount of KeepAliveStreams to be queued56protected static int MAX_CAPACITY = 10;5758// timeout for both socket and poll on the queue59protected static final int TIMEOUT = 5000;6061// max retries for skipping data62private static final int MAX_RETRIES = 5;6364static {65final String maxDataKey = "http.KeepAlive.remainingData";66@SuppressWarnings("removal")67int maxData = AccessController.doPrivileged(68new PrivilegedAction<Integer>() {69public Integer run() {70return NetProperties.getInteger(maxDataKey, MAX_DATA_REMAINING);71}}).intValue() * 1024;72MAX_DATA_REMAINING = maxData;7374final String maxCapacityKey = "http.KeepAlive.queuedConnections";75@SuppressWarnings("removal")76int maxCapacity = AccessController.doPrivileged(77new PrivilegedAction<Integer>() {78public Integer run() {79return NetProperties.getInteger(maxCapacityKey, MAX_CAPACITY);80}}).intValue();81MAX_CAPACITY = maxCapacity;8283}8485private final ReentrantLock queueLock = new ReentrantLock();86private final Condition waiter = queueLock.newCondition();8788final void signalAll() {89waiter.signalAll();90}9192final void lock() {93queueLock.lock();94}9596final void unlock() {97queueLock.unlock();98}99100@Override101public boolean offer(KeepAliveCleanerEntry e) {102if (size() >= MAX_CAPACITY)103return false;104105return super.offer(e);106}107108@Override109public void run()110{111KeepAliveCleanerEntry kace = null;112113do {114try {115lock();116try {117long before = System.currentTimeMillis();118long timeout = TIMEOUT;119while ((kace = poll()) == null) {120waiter.await(timeout, TimeUnit.MILLISECONDS);121122long after = System.currentTimeMillis();123long elapsed = after - before;124if (elapsed > timeout) {125/* one last try */126kace = poll();127break;128}129before = after;130timeout -= elapsed;131}132} finally {133unlock();134}135136if(kace == null)137break;138139KeepAliveStream kas = kace.getKeepAliveStream();140141if (kas != null) {142kas.lock();143try {144HttpClient hc = kace.getHttpClient();145try {146if (hc != null && !hc.isInKeepAliveCache()) {147int oldTimeout = hc.getReadTimeout();148hc.setReadTimeout(TIMEOUT);149long remainingToRead = kas.remainingToRead();150if (remainingToRead > 0) {151long n = 0;152int retries = 0;153while (n < remainingToRead && retries < MAX_RETRIES) {154remainingToRead = remainingToRead - n;155n = kas.skip(remainingToRead);156if (n == 0)157retries++;158}159remainingToRead = remainingToRead - n;160}161if (remainingToRead == 0) {162hc.setReadTimeout(oldTimeout);163hc.finished();164} else165hc.closeServer();166}167} catch (IOException ioe) {168hc.closeServer();169} finally {170kas.setClosed();171}172} finally {173kas.unlock();174}175}176} catch (InterruptedException ie) { }177} while (kace != null);178}179}180181182