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