Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/http/KeepAliveStreamCleaner.java
38923 views
/*1* Copyright (c) 2005, 2008, 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;3233/**34* This class is used to cleanup any remaining data that may be on a KeepAliveStream35* so that the connection can be cached in the KeepAliveCache.36* Instances of this class can be used as a FIFO queue for KeepAliveCleanerEntry objects.37* Executing this Runnable removes each KeepAliveCleanerEntry from the Queue, reads38* the reamining bytes on its KeepAliveStream, and if successful puts the connection in39* the KeepAliveCache.40*41* @author Chris Hegarty42*/4344@SuppressWarnings("serial") // never serialized45class KeepAliveStreamCleaner46extends LinkedList<KeepAliveCleanerEntry>47implements Runnable48{49// maximum amount of remaining data that we will try to cleanup50protected static int MAX_DATA_REMAINING = 512;5152// maximum amount of KeepAliveStreams to be queued53protected static int MAX_CAPACITY = 10;5455// timeout for both socket and poll on the queue56protected static final int TIMEOUT = 5000;5758// max retries for skipping data59private static final int MAX_RETRIES = 5;6061static {62final String maxDataKey = "http.KeepAlive.remainingData";63int maxData = AccessController.doPrivileged(64new PrivilegedAction<Integer>() {65public Integer run() {66return NetProperties.getInteger(maxDataKey, MAX_DATA_REMAINING);67}}).intValue() * 1024;68MAX_DATA_REMAINING = maxData;6970final String maxCapacityKey = "http.KeepAlive.queuedConnections";71int maxCapacity = AccessController.doPrivileged(72new PrivilegedAction<Integer>() {73public Integer run() {74return NetProperties.getInteger(maxCapacityKey, MAX_CAPACITY);75}}).intValue();76MAX_CAPACITY = maxCapacity;7778}798081@Override82public boolean offer(KeepAliveCleanerEntry e) {83if (size() >= MAX_CAPACITY)84return false;8586return super.offer(e);87}8889@Override90public void run()91{92KeepAliveCleanerEntry kace = null;9394do {95try {96synchronized(this) {97long before = System.currentTimeMillis();98long timeout = TIMEOUT;99while ((kace = poll()) == null) {100this.wait(timeout);101102long after = System.currentTimeMillis();103long elapsed = after - before;104if (elapsed > timeout) {105/* one last try */106kace = poll();107break;108}109before = after;110timeout -= elapsed;111}112}113114if(kace == null)115break;116117KeepAliveStream kas = kace.getKeepAliveStream();118119if (kas != null) {120synchronized(kas) {121HttpClient hc = kace.getHttpClient();122try {123if (hc != null && !hc.isInKeepAliveCache()) {124int oldTimeout = hc.getReadTimeout();125hc.setReadTimeout(TIMEOUT);126long remainingToRead = kas.remainingToRead();127if (remainingToRead > 0) {128long n = 0;129int retries = 0;130while (n < remainingToRead && retries < MAX_RETRIES) {131remainingToRead = remainingToRead - n;132n = kas.skip(remainingToRead);133if (n == 0)134retries++;135}136remainingToRead = remainingToRead - n;137}138if (remainingToRead == 0) {139hc.setReadTimeout(oldTimeout);140hc.finished();141} else142hc.closeServer();143}144} catch (IOException ioe) {145hc.closeServer();146} finally {147kas.setClosed();148}149}150}151} catch (InterruptedException ie) { }152} while (kace != null);153}154}155156157