/
githubmirror
/
jdk22
Обзор
Документация
Войти
/
githubmirror
/
jdk22
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/java.net.http/share/classes/jdk/internal/net/http/WindowUpdateSender.java
107 строк
4 KB
Daniel Fuchs
8308310: HttpClient: Avoid logging or locking from within synchronized blocks
24 май 2023, 17:23
24 май 2023, 17:23
736b90d
Код
Авторство
О чём код?
/* * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.internal.net.http; import jdk.internal.net.http.common.FlowTube; import jdk.internal.net.http.common.Logger; import jdk.internal.net.http.frame.SettingsFrame; import jdk.internal.net.http.frame.WindowUpdateFrame; import jdk.internal.net.http.common.Utils; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; abstract class WindowUpdateSender { final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); final int limit; final Http2Connection connection; final AtomicInteger received = new AtomicInteger(); final ReentrantLock sendLock = new ReentrantLock(); WindowUpdateSender(Http2Connection connection) { this(connection, connection.clientSettings.getParameter(SettingsFrame.INITIAL_WINDOW_SIZE)); } WindowUpdateSender(Http2Connection connection, int initWindowSize) { this(connection, connection.getMaxReceiveFrameSize(), initWindowSize); } WindowUpdateSender(Http2Connection connection, int maxFrameSize, int initWindowSize) { this.connection = connection; int v0 = Math.max(0, initWindowSize - maxFrameSize); int v1 = (initWindowSize + (maxFrameSize - 1)) / maxFrameSize; v1 = v1 * maxFrameSize / 2; // send WindowUpdate heuristic: // - we got data near half of window size // or // - remaining window size reached max frame size. limit = Math.min(v0, v1); if (debug.on()) debug.log("maxFrameSize=%d, initWindowSize=%d, limit=%d", maxFrameSize, initWindowSize, limit); } abstract int getStreamId(); void update(int delta) { int rcv = received.addAndGet(delta); if (debug.on()) debug.log("update: %d, received: %d, limit: %d", delta, rcv, limit); if (rcv > limit) { sendLock.lock(); try { int tosend = received.get(); if( tosend > limit) { received.getAndAdd(-tosend); sendWindowUpdate(tosend); } } finally { sendLock.unlock(); } } } void sendWindowUpdate(int delta) { if (debug.on()) debug.log("sending window update: %d", delta); connection.sendUnorderedFrame(new WindowUpdateFrame(getStreamId(), delta)); } volatile String dbgString; String dbgString() { String dbg = dbgString; if (dbg != null) return dbg; FlowTube tube = connection.connection.getConnectionFlow(); if (tube == null) { return "WindowUpdateSender(stream: " + getStreamId() + ")"; } else { int streamId = getStreamId(); dbg = connection.dbgString() + ":WindowUpdateSender(stream: " + streamId + ")"; return streamId == 0 ? dbg : (dbgString = dbg); } } }