1 /* |
|
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.incubator.http.internal; |
|
27 |
|
28 import java.lang.System.Logger.Level; |
|
29 import jdk.incubator.http.internal.frame.SettingsFrame; |
|
30 import jdk.incubator.http.internal.frame.WindowUpdateFrame; |
|
31 import jdk.incubator.http.internal.common.Utils; |
|
32 |
|
33 import java.util.concurrent.atomic.AtomicInteger; |
|
34 |
|
35 abstract class WindowUpdateSender { |
|
36 |
|
37 final static boolean DEBUG = Utils.DEBUG; |
|
38 final System.Logger debug = |
|
39 Utils.getDebugLogger(this::dbgString, DEBUG); |
|
40 |
|
41 final int limit; |
|
42 final Http2Connection connection; |
|
43 final AtomicInteger received = new AtomicInteger(0); |
|
44 |
|
45 WindowUpdateSender(Http2Connection connection) { |
|
46 this(connection, connection.clientSettings.getParameter(SettingsFrame.INITIAL_WINDOW_SIZE)); |
|
47 } |
|
48 |
|
49 WindowUpdateSender(Http2Connection connection, int initWindowSize) { |
|
50 this(connection, connection.getMaxReceiveFrameSize(), initWindowSize); |
|
51 } |
|
52 |
|
53 WindowUpdateSender(Http2Connection connection, int maxFrameSize, int initWindowSize) { |
|
54 this.connection = connection; |
|
55 int v0 = Math.max(0, initWindowSize - maxFrameSize); |
|
56 int v1 = (initWindowSize + (maxFrameSize - 1)) / maxFrameSize; |
|
57 v1 = v1 * maxFrameSize / 2; |
|
58 // send WindowUpdate heuristic: |
|
59 // - we got data near half of window size |
|
60 // or |
|
61 // - remaining window size reached max frame size. |
|
62 limit = Math.min(v0, v1); |
|
63 debug.log(Level.DEBUG, "maxFrameSize=%d, initWindowSize=%d, limit=%d", |
|
64 maxFrameSize, initWindowSize, limit); |
|
65 } |
|
66 |
|
67 abstract int getStreamId(); |
|
68 |
|
69 void update(int delta) { |
|
70 debug.log(Level.DEBUG, "update: %d", delta); |
|
71 if (received.addAndGet(delta) > limit) { |
|
72 synchronized (this) { |
|
73 int tosend = received.get(); |
|
74 if( tosend > limit) { |
|
75 received.getAndAdd(-tosend); |
|
76 sendWindowUpdate(tosend); |
|
77 } |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 void sendWindowUpdate(int delta) { |
|
83 debug.log(Level.DEBUG, "sending window update: %d", delta); |
|
84 connection.sendUnorderedFrame(new WindowUpdateFrame(getStreamId(), delta)); |
|
85 } |
|
86 |
|
87 String dbgString() { |
|
88 return "WindowUpdateSender(stream: " + getStreamId() + ")"; |
|
89 } |
|
90 |
|
91 } |
|