src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/WindowUpdateSender.java
branchhttp-client-branch
changeset 56079 d23b02f37fce
parent 56078 6c11b48a0695
child 56080 64846522c0d5
equal deleted inserted replaced
56078:6c11b48a0695 56079:d23b02f37fce
     1 /*
       
     2  * Copyright (c) 2015, 2017, 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 package jdk.incubator.http;
       
    26 
       
    27 import java.lang.System.Logger.Level;
       
    28 import jdk.incubator.http.internal.frame.SettingsFrame;
       
    29 import jdk.incubator.http.internal.frame.WindowUpdateFrame;
       
    30 import jdk.incubator.http.internal.common.Utils;
       
    31 
       
    32 import java.util.concurrent.atomic.AtomicInteger;
       
    33 
       
    34 abstract class WindowUpdateSender {
       
    35 
       
    36     final static boolean DEBUG = Utils.DEBUG;
       
    37     final System.Logger debug =
       
    38             Utils.getDebugLogger(this::dbgString, DEBUG);
       
    39 
       
    40     final int limit;
       
    41     final Http2Connection connection;
       
    42     final AtomicInteger received = new AtomicInteger(0);
       
    43 
       
    44     WindowUpdateSender(Http2Connection connection) {
       
    45         this(connection, connection.clientSettings.getParameter(SettingsFrame.INITIAL_WINDOW_SIZE));
       
    46     }
       
    47 
       
    48     WindowUpdateSender(Http2Connection connection, int initWindowSize) {
       
    49         this(connection, connection.getMaxReceiveFrameSize(), initWindowSize);
       
    50     }
       
    51 
       
    52     WindowUpdateSender(Http2Connection connection, int maxFrameSize, int initWindowSize) {
       
    53         this.connection = connection;
       
    54         int v0 = Math.max(0, initWindowSize - maxFrameSize);
       
    55         int v1 = (initWindowSize + (maxFrameSize - 1)) / maxFrameSize;
       
    56         v1 = v1 * maxFrameSize / 2;
       
    57         // send WindowUpdate heuristic:
       
    58         // - we got data near half of window size
       
    59         //   or
       
    60         // - remaining window size reached max frame size.
       
    61         limit = Math.min(v0, v1);
       
    62         debug.log(Level.DEBUG, "maxFrameSize=%d, initWindowSize=%d, limit=%d",
       
    63                 maxFrameSize, initWindowSize, limit);
       
    64     }
       
    65 
       
    66     abstract int getStreamId();
       
    67 
       
    68     void update(int delta) {
       
    69         debug.log(Level.DEBUG, "update: %d", delta);
       
    70         if (received.addAndGet(delta) > limit) {
       
    71             synchronized (this) {
       
    72                 int tosend = received.get();
       
    73                 if( tosend > limit) {
       
    74                     received.getAndAdd(-tosend);
       
    75                     sendWindowUpdate(tosend);
       
    76                 }
       
    77             }
       
    78         }
       
    79     }
       
    80 
       
    81     void sendWindowUpdate(int delta) {
       
    82         debug.log(Level.DEBUG, "sending window update: %d", delta);
       
    83         connection.sendUnorderedFrame(new WindowUpdateFrame(getStreamId(), delta));
       
    84     }
       
    85 
       
    86     String dbgString() {
       
    87         return "WindowUpdateSender(stream: " + getStreamId() + ")";
       
    88     }
       
    89 
       
    90 }