author | mbaesken |
Thu, 28 Nov 2019 13:02:39 +0100 | |
changeset 59323 | ae2eb76c486d |
parent 51232 | ad1fa1db73d9 |
child 56842 | 43c3b82728eb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
48083 | 28 |
import java.lang.System.Logger.Level; |
29 |
import java.util.ArrayList; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.util.Map; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.util.HashMap; |
48083 | 32 |
import java.util.Iterator; |
33 |
import java.util.LinkedHashMap; |
|
34 |
import java.util.List; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.concurrent.locks.ReentrantLock; |
49765 | 36 |
|
37 |
import jdk.internal.net.http.common.Logger; |
|
38 |
import jdk.internal.net.http.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
/** |
48083 | 41 |
* A Send Window Flow-Controller that is used to control outgoing Connection |
42 |
* and Stream flows, per HTTP/2 connection. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
* A Http2Connection has its own unique single instance of a WindowController |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
* that it shares with its Streams. Each stream must acquire the appropriate |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
* amount of Send Window from the controller before sending data. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
* WINDOW_UPDATE frames, both connection and stream specific, must notify the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
* controller of their increments. SETTINGS frame's INITIAL_WINDOW_SIZE must |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
* notify the controller so that it can adjust the active stream's window size. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
final class WindowController { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
49765 | 54 |
static final Logger debug = |
55 |
Utils.getDebugLogger("WindowController"::toString, Utils.DEBUG); |
|
48083 | 56 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
* Default initial connection Flow-Control Send Window size, as per HTTP/2. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
private static final int DEFAULT_INITIAL_WINDOW_SIZE = 64 * 1024 - 1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
/** The connection Send Window size. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
private int connectionWindowSize; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
/** A Map of the active streams, where the key is the stream id, and the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
* value is the stream's Send Window size, which may be negative. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
private final Map<Integer,Integer> streams = new HashMap<>(); |
48083 | 67 |
/** A Map of streams awaiting Send Window. The key is the stream id. The |
68 |
* value is a pair of the Stream ( representing the key's stream id ) and |
|
69 |
* the requested amount of send Window. */ |
|
70 |
private final Map<Integer, Map.Entry<Stream<?>, Integer>> pending |
|
71 |
= new LinkedHashMap<>(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
private final ReentrantLock controllerLock = new ReentrantLock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
/** A Controller with the default initial window size. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
WindowController() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
connectionWindowSize = DEFAULT_INITIAL_WINDOW_SIZE; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
|
48083 | 80 |
// /** A Controller with the given initial window size. */ |
81 |
// WindowController(int initialConnectionWindowSize) { |
|
82 |
// connectionWindowSize = initialConnectionWindowSize; |
|
83 |
// } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
/** Registers the given stream with this controller. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
void registerStream(int streamid, int initialStreamWindowSize) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
Integer old = streams.put(streamid, initialStreamWindowSize); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
if (old != null) |
48083 | 91 |
throw new InternalError("Unexpected entry [" |
92 |
+ old + "] for streamid: " + streamid); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
/** Removes/De-registers the given stream with this controller. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
void removeStream(int streamid) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
Integer old = streams.remove(streamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
// Odd stream numbers (client streams) should have been registered. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
// Even stream numbers (server streams - aka Push Streams) should |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
// not be registered |
49765 | 106 |
final boolean isClientStream = (streamid & 0x1) == 1; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
if (old == null && isClientStream) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
throw new InternalError("Expected entry for streamid: " + streamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
} else if (old != null && !isClientStream) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
throw new InternalError("Unexpected entry for streamid: " + streamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
* Attempts to acquire the requested amount of Send Window for the given |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
* stream. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
* The actual amount of Send Window available may differ from the requested |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
* amount. The actual amount, returned by this method, is the minimum of, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
* 1) the requested amount, 2) the stream's Send Window, and 3) the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
* connection's Send Window. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
* |
48083 | 126 |
* A negative or zero value is returned if there's no window available. |
127 |
* When the result is negative or zero, this method arranges for the |
|
128 |
* given stream's {@link Stream#signalWindowUpdate()} method to be invoke at |
|
129 |
* a later time when the connection and/or stream window's have been |
|
130 |
* increased. The {@code tryAcquire} method should then be invoked again to |
|
131 |
* attempt to acquire the available window. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
*/ |
48083 | 133 |
int tryAcquire(int requestAmount, int streamid, Stream<?> stream) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
try { |
48083 | 136 |
Integer streamSize = streams.get(streamid); |
137 |
if (streamSize == null) |
|
138 |
throw new InternalError("Expected entry for streamid: " |
|
139 |
+ streamid); |
|
140 |
int x = Math.min(requestAmount, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
Math.min(streamSize, connectionWindowSize)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
|
48083 | 143 |
if (x <= 0) { // stream window size may be negative |
49765 | 144 |
if (debug.on()) |
145 |
debug.log("Stream %d requesting %d but only %d available (stream: %d, connection: %d)", |
|
146 |
streamid, requestAmount, Math.min(streamSize, connectionWindowSize), |
|
147 |
streamSize, connectionWindowSize); |
|
48083 | 148 |
// If there's not enough window size available, put the |
149 |
// caller in a pending list. |
|
150 |
pending.put(streamid, Map.entry(stream, requestAmount)); |
|
151 |
return x; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
|
48083 | 154 |
// Remove the caller from the pending list ( if was waiting ). |
155 |
pending.remove(streamid); |
|
156 |
||
157 |
// Update window sizes and return the allocated amount to the caller. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
streamSize -= x; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
streams.put(streamid, streamSize); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
connectionWindowSize -= x; |
49765 | 161 |
if (debug.on()) |
162 |
debug.log("Stream %d amount allocated %d, now %d available (stream: %d, connection: %d)", |
|
163 |
streamid, x, Math.min(streamSize, connectionWindowSize), |
|
164 |
streamSize, connectionWindowSize); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
return x; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
* Increases the Send Window size for the connection. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
* |
48083 | 174 |
* A number of awaiting requesters, from unfulfilled tryAcquire requests, |
175 |
* may have their stream's {@link Stream#signalWindowUpdate()} method |
|
176 |
* scheduled to run ( i.e. awake awaiters ). |
|
177 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
* @return false if, and only if, the addition of the given amount would |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
* cause the Send Window to exceed 2^31-1 (overflow), otherwise true |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
boolean increaseConnectionWindow(int amount) { |
48083 | 182 |
List<Stream<?>> candidates = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
int size = connectionWindowSize; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
size += amount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
if (size < 0) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
connectionWindowSize = size; |
49765 | 190 |
if (debug.on()) |
191 |
debug.log("Connection window size is now %d (amount added %d)", |
|
192 |
size, amount); |
|
48083 | 193 |
|
194 |
// Notify waiting streams, until the new increased window size is |
|
195 |
// effectively exhausted. |
|
196 |
Iterator<Map.Entry<Integer,Map.Entry<Stream<?>,Integer>>> iter = |
|
197 |
pending.entrySet().iterator(); |
|
198 |
||
199 |
while (iter.hasNext() && size > 0) { |
|
200 |
Map.Entry<Integer,Map.Entry<Stream<?>,Integer>> item = iter.next(); |
|
201 |
Integer streamSize = streams.get(item.getKey()); |
|
202 |
if (streamSize == null) { |
|
203 |
iter.remove(); |
|
204 |
} else { |
|
205 |
Map.Entry<Stream<?>,Integer> e = item.getValue(); |
|
206 |
int requestedAmount = e.getValue(); |
|
207 |
// only wakes up the pending streams for which there is |
|
208 |
// at least 1 byte of space in both windows |
|
209 |
int minAmount = 1; |
|
210 |
if (size >= minAmount && streamSize >= minAmount) { |
|
211 |
size -= Math.min(streamSize, requestedAmount); |
|
212 |
iter.remove(); |
|
213 |
if (candidates == null) |
|
214 |
candidates = new ArrayList<>(); |
|
215 |
candidates.add(e.getKey()); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
} |
48083 | 222 |
if (candidates != null) { |
223 |
candidates.forEach(Stream::signalWindowUpdate); |
|
224 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
* Increases the Send Window size for the given stream. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
* |
48083 | 231 |
* If the given stream is awaiting window size, from an unfulfilled |
232 |
* tryAcquire request, it will have its stream's {@link |
|
233 |
* Stream#signalWindowUpdate()} method scheduled to run ( i.e. awoken ). |
|
234 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
* @return false if, and only if, the addition of the given amount would |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
* cause the Send Window to exceed 2^31-1 (overflow), otherwise true |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
boolean increaseStreamWindow(int amount, int streamid) { |
48083 | 239 |
Stream<?> s = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
Integer size = streams.get(streamid); |
49765 | 243 |
if (size == null) { |
244 |
// The stream may have been cancelled. |
|
245 |
if (debug.on()) |
|
246 |
debug.log("WARNING: No entry found for streamid: %s. May be cancelled?", |
|
247 |
streamid); |
|
248 |
} else { |
|
51232
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
249 |
int prev = size; |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
250 |
size = prev + amount; |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
251 |
if (size < prev) |
49765 | 252 |
return false; |
253 |
streams.put(streamid, size); |
|
254 |
if (debug.on()) |
|
255 |
debug.log("Stream %s window size is now %s (amount added %d)", |
|
256 |
streamid, size, amount); |
|
48083 | 257 |
|
49765 | 258 |
Map.Entry<Stream<?>, Integer> p = pending.get(streamid); |
259 |
if (p != null) { |
|
260 |
int minAmount = 1; |
|
261 |
// only wakes up the pending stream if there is at least |
|
262 |
// 1 byte of space in both windows |
|
263 |
if (size >= minAmount |
|
264 |
&& connectionWindowSize >= minAmount) { |
|
265 |
pending.remove(streamid); |
|
266 |
s = p.getKey(); |
|
267 |
} |
|
48083 | 268 |
} |
269 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
} |
48083 | 273 |
|
274 |
if (s != null) |
|
275 |
s.signalWindowUpdate(); |
|
276 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
278 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
* Adjusts, either increases or decreases, the active streams registered |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
* with this controller. May result in a stream's Send Window size becoming |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
* negative. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
void adjustActiveStreams(int adjustAmount) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
assert adjustAmount != 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
for (Map.Entry<Integer,Integer> entry : streams.entrySet()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
int streamid = entry.getKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
// the API only supports sending on Streams initialed by |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
// the client, i.e. odd stream numbers |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
if (streamid != 0 && (streamid % 2) != 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
Integer size = entry.getValue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
size += adjustAmount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
297 |
streams.put(streamid, size); |
49765 | 298 |
if (debug.on()) |
299 |
debug.log("Stream %s window size is now %s (adjusting amount %d)", |
|
300 |
streamid, size, adjustAmount); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
/** Returns the Send Window size for the connection. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
int connectionWindowSize() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
controllerLock.lock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
return connectionWindowSize; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
controllerLock.unlock(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
|
51232
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
318 |
/** Returns the Send Window size for the given stream. */ |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
319 |
int streamWindowSize(int streamid) { |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
320 |
controllerLock.lock(); |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
321 |
try { |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
322 |
Integer size = streams.get(streamid); |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
323 |
if (size == null) |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
324 |
throw new InternalError("Expected entry for streamid: " + streamid); |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
325 |
return size; |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
326 |
} finally { |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
327 |
controllerLock.unlock(); |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
328 |
} |
ad1fa1db73d9
8207960: Non-negative WINDOW_UPDATE increments may leave the stream window size negative
chegar
parents:
49765
diff
changeset
|
329 |
} |
48083 | 330 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
} |