jdk/src/share/classes/sun/net/www/http/KeepAliveStreamCleaner.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3865 cc2ca3d07bbb
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
     2
 * Copyright (c) 2005, 2008, 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;
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    29
import java.util.LinkedList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.net.NetProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class is used to cleanup any remaining data that may be on a KeepAliveStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * so that the connection can be cached in the KeepAliveCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Instances of this class can be used as a FIFO queue for KeepAliveCleanerEntry objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Executing this Runnable removes each KeepAliveCleanerEntry from the Queue, reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the reamining bytes on its KeepAliveStream, and if successful puts the connection in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the KeepAliveCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
1334
21b652819b97 6746836: java.net exception classes don't specify serialVersionUID
chegar
parents: 715
diff changeset
    45
@SuppressWarnings("serial")  // never serialized
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    46
class KeepAliveStreamCleaner
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    47
    extends LinkedList<KeepAliveCleanerEntry>
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    48
    implements Runnable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // maximum amount of remaining data that we will try to cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected static int MAX_DATA_REMAINING = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // maximum amount of KeepAliveStreams to be queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected static int MAX_CAPACITY = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // timeout for both socket and poll on the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected static final int TIMEOUT = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // max retries for skipping data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int MAX_RETRIES = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        final String maxDataKey = "http.KeepAlive.remainingData";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    64
        int maxData = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    65
            new PrivilegedAction<Integer>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    66
                public Integer run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    67
                    return NetProperties.getInteger(maxDataKey, MAX_DATA_REMAINING);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    68
                }}).intValue() * 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        MAX_DATA_REMAINING = maxData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        final String maxCapacityKey = "http.KeepAlive.queuedConnections";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    72
        int maxCapacity = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    73
            new PrivilegedAction<Integer>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    74
                public Integer run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    75
                    return NetProperties.getInteger(maxCapacityKey, MAX_CAPACITY);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    76
                }}).intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        MAX_CAPACITY = maxCapacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    82
    @Override
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    83
    public boolean offer(KeepAliveCleanerEntry e) {
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    84
        if (size() >= MAX_CAPACITY)
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    85
            return false;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    86
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    87
        return super.offer(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    90
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        KeepAliveCleanerEntry kace = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    97
                synchronized(this) {
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    98
                    long before = System.currentTimeMillis();
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
    99
                    long timeout = TIMEOUT;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   100
                    while ((kace = poll()) == null) {
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   101
                        this.wait(timeout);
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   102
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   103
                        long after = System.currentTimeMillis();
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   104
                        long elapsed = after - before;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   105
                        if (elapsed > timeout) {
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   106
                            /* one last try */
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   107
                            kace = poll();
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   108
                            break;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   109
                        }
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   110
                        before = after;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   111
                        timeout -= elapsed;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   112
                    }
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   113
                }
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 2928
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if(kace == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                KeepAliveStream kas = kace.getKeepAliveStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (kas != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    synchronized(kas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        HttpClient hc = kace.getHttpClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            if (hc != null && !hc.isInKeepAliveCache()) {
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1334
diff changeset
   125
                                int oldTimeout = hc.getReadTimeout();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1334
diff changeset
   126
                                hc.setReadTimeout(TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                long remainingToRead = kas.remainingToRead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                if (remainingToRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                    long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    int retries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    while (n < remainingToRead && retries < MAX_RETRIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        remainingToRead = remainingToRead - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                        n = kas.skip(remainingToRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                            retries++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                    remainingToRead = remainingToRead - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                if (remainingToRead == 0) {
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1334
diff changeset
   140
                                    hc.setReadTimeout(oldTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                    hc.finished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                    hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            kas.setClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } catch (InterruptedException ie) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } while (kace != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}