src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java
author chegar
Fri, 02 Mar 2018 10:15:15 +0000
branchhttp-client-branch
changeset 56224 87781d9c4996
parent 56092 fd85b2bf2b0d
child 56437 f8b3f053cfbb
permissions -rw-r--r--
http-client-branch: improve HTTP/2 caching
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
56035
2f3f5da13c4c http-client-branch: MinimalFuture deep obtrusion
prappo
parents: 56008
diff changeset
     2
 * Copyright (c) 2015, 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
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    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
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    28
import java.lang.System.Logger.Level;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.net.InetSocketAddress;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.util.Base64;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.util.Collections;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    33
import java.util.HashSet;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.util.HashMap;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.util.Map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.Set;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.concurrent.ConcurrentHashMap;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.util.concurrent.CompletableFuture;
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
    39
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    40
import jdk.internal.net.http.common.Log;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    41
import jdk.internal.net.http.common.MinimalFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    42
import jdk.internal.net.http.common.Utils;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    43
import jdk.internal.net.http.frame.SettingsFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    44
import static jdk.internal.net.http.frame.SettingsFrame.INITIAL_WINDOW_SIZE;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    45
import static jdk.internal.net.http.frame.SettingsFrame.ENABLE_PUSH;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import static jdk.internal.net.http.frame.SettingsFrame.HEADER_TABLE_SIZE;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    47
import static jdk.internal.net.http.frame.SettingsFrame.MAX_CONCURRENT_STREAMS;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    48
import static jdk.internal.net.http.frame.SettingsFrame.MAX_FRAME_SIZE;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
 *  Http2 specific aspects of HttpClientImpl
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
class Http2ClientImpl {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    54
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    55
    static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    56
    final static System.Logger debug =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    57
            Utils.getDebugLogger("Http2ClientImpl"::toString, DEBUG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
    private final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
    Http2ClientImpl(HttpClientImpl client) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
        this.client = client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
    /* Map key is "scheme:host:port" */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
    private final Map<String,Http2Connection> connections = new ConcurrentHashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    68
    private final Set<String> failures = Collections.synchronizedSet(new HashSet<>());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
    /**
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    71
     * When HTTP/2 requested only. The following describes the aggregate behavior including the
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    72
     * calling code. In all cases, the HTTP2 connection cache
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    73
     * is checked first for a suitable connection and that is returned if available.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    74
     * If not, a new connection is opened, except in https case when a previous negotiate failed.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    75
     * In that case, we want to continue using http/1.1. When a connection is to be opened and
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    76
     * if multiple requests are sent in parallel then each will open a new connection.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    77
     *
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    78
     * If negotiation/upgrade succeeds then
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    79
     * one connection will be put in the cache and the others will be closed
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    80
     * after the initial request completes (not strictly necessary for h2, only for h2c)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
     *
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    82
     * If negotiate/upgrade fails, then any opened connections remain open (as http/1.1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    83
     * and will be used and cached in the http/1 cache. Note, this method handles the
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    84
     * https failure case only (by completing the CF with an ALPN exception, handled externally)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    85
     * The h2c upgrade is handled externally also.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
     *
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    87
     * Specific CF behavior of this method.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    88
     * 1. completes with ALPN exception: h2 negotiate failed for first time. failure recorded.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    89
     * 2. completes with other exception: failure not recorded. Caller must handle
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    90
     * 3. completes normally with null: no connection in cache for h2c or h2 failed previously
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    91
     * 4. completes normally with connection: h2 or h2c connection in cache. Use it.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
    CompletableFuture<Http2Connection> getConnectionFor(HttpRequestImpl req) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
        URI uri = req.uri();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
        InetSocketAddress proxy = req.proxy();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
        String key = Http2Connection.keyFor(uri, proxy);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    98
        synchronized (this) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    99
            Http2Connection connection = connections.get(key);
56224
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   100
            if (connection != null) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   101
                if (connection.closed) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   102
                    debug.log(Level.DEBUG, "removing found closed connection: %s", connection);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   103
                    connections.remove(key);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   104
                } else {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   105
                    // fast path if connection already exists
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   106
                    debug.log(Level.DEBUG, "found connection in the pool: %s", connection);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   107
                    return MinimalFuture.completedFuture(connection);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   108
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   111
            if (!req.secure() || failures.contains(key)) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   112
                // secure: negotiate failed before. Use http/1.1
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   113
                // !secure: no connection available in cache. Attempt upgrade
56224
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   114
                debug.log(Level.DEBUG, "not found in connection pool");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
                return MinimalFuture.completedFuture(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
        return Http2Connection
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
                .createAsync(req, this)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
                .whenComplete((conn, t) -> {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   121
                    synchronized (Http2ClientImpl.this) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   122
                        if (conn != null) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   123
                            offerConnection(conn);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   124
                        } else {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   125
                            Throwable cause = Utils.getCompletionCause(t);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   126
                            if (cause instanceof Http2Connection.ALPNException)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   127
                                failures.add(key);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
                });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
    /*
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   134
     * Cache the given connection, if no connection to the same
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   135
     * destination exists. If one exists, then we let the initial stream
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   136
     * complete but allow it to close itself upon completion.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   137
     * This situation should not arise with https because the request
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   138
     * has not been sent as part of the initial alpn negotiation
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   139
     */
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   140
    boolean offerConnection(Http2Connection c) {
56224
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   141
        debug.log(Level.DEBUG, "offering to the connection pool: %s", c);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   142
        if (c.closed) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   143
            debug.log(Level.DEBUG, "skipping offered closed connection: %s", c);
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   144
            return false;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   145
        }
56224
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   146
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   147
        String key = c.key();
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   148
        synchronized(this) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   149
            Http2Connection c1 = connections.putIfAbsent(key, c);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   150
            if (c1 != null) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   151
                c.setSingleStream(true);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   152
                debug.log(Level.DEBUG, "existing entry in connection pool for %s", key);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   153
                return false;
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   154
            }
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   155
            debug.log(Level.DEBUG, "put in the connection pool: %s", c);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   156
            return true;
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   157
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
    void deleteConnection(Http2Connection c) {
56224
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   161
        debug.log(Level.DEBUG, "removing from the connection pool: %s", c);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   162
        synchronized (this) {
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   163
            connections.remove(c.key());
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   164
            debug.log(Level.DEBUG, "removed from the connection pool: %s", c);
87781d9c4996 http-client-branch: improve HTTP/2 caching
chegar
parents: 56092
diff changeset
   165
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   166
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   167
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   168
    void stop() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   169
        debug.log(Level.DEBUG, "stopping");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
        connections.values().forEach(this::close);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   171
        connections.clear();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
    private void close(Http2Connection h2c) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
        try { h2c.close(); } catch (Throwable t) {}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
    HttpClientImpl client() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   179
        return client;
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
    /** Returns the client settings as a base64 (url) encoded string */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
    String getSettingsString() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   184
        SettingsFrame sf = getClientSettings();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
        byte[] settings = sf.toByteArray(); // without the header
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
        Base64.Encoder encoder = Base64.getUrlEncoder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
                                       .withoutPadding();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   188
        return encoder.encodeToString(settings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   189
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
    private static final int K = 1024;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   192
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   193
    private static int getParameter(String property, int min, int max, int defaultValue) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   194
        int value =  Utils.getIntegerNetProperty(property, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   195
        // use default value if misconfigured
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   196
        if (value < min || value > max) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   197
            Log.logError("Property value for {0}={1} not in [{2}..{3}]: " +
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   198
                    "using default={4}", property, value, min, max, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   199
            value = defaultValue;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   200
        }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   201
        return value;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   202
    }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   203
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   204
    // used for the connection window, to have a connection window size
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   205
    // bigger than the initial stream window size.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   206
    int getConnectionWindowSize(SettingsFrame clientSettings) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   207
        // Maximum size is 2^31-1. Don't allow window size to be less
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   208
        // than the stream window size. HTTP/2 specify a default of 64 * K -1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   209
        // but we use 2^26 by default for better performance.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   210
        int streamWindow = clientSettings.getParameter(INITIAL_WINDOW_SIZE);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   211
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   212
        // The default is the max between the stream window size
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   213
        // and the connection window size.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   214
        int defaultValue = Math.min(Integer.MAX_VALUE,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   215
                Math.max(streamWindow, K*K*32));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   216
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   217
        return getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   218
                "jdk.httpclient.connectionWindowSize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   219
                streamWindow, Integer.MAX_VALUE, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   220
    }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   221
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   222
    SettingsFrame getClientSettings() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
        SettingsFrame frame = new SettingsFrame();
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   224
        // default defined for HTTP/2 is 4 K, we use 16 K.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   225
        frame.setParameter(HEADER_TABLE_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   226
                "jdk.httpclient.hpack.maxheadertablesize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   227
                0, Integer.MAX_VALUE, 16 * K));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   228
        // O: does not accept push streams. 1: accepts push streams.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   229
        frame.setParameter(ENABLE_PUSH, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   230
                "jdk.httpclient.enablepush",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   231
                0, 1, 1));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   232
        // HTTP/2 recommends to set the number of concurrent streams
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   233
        // no lower than 100. We use 100. 0 means no stream would be
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   234
        // accepted. That would render the client to be non functional,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   235
        // so we won't let 0 be configured for our Http2ClientImpl.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   236
        frame.setParameter(MAX_CONCURRENT_STREAMS, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   237
                "jdk.httpclient.maxstreams",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   238
                1, Integer.MAX_VALUE, 100));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   239
        // Maximum size is 2^31-1. Don't allow window size to be less
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   240
        // than the minimum frame size as this is likely to be a
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   241
        // configuration error. HTTP/2 specify a default of 64 * K -1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   242
        // but we use 16 M  for better performance.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   243
        frame.setParameter(INITIAL_WINDOW_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   244
                "jdk.httpclient.windowsize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   245
                16 * K, Integer.MAX_VALUE, 16*K*K));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   246
        // HTTP/2 specify a minimum size of 16 K, a maximum size of 2^24-1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   247
        // and a default of 16 K. We use 16 K as default.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   248
        frame.setParameter(MAX_FRAME_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   249
                "jdk.httpclient.maxframesize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   250
                16 * K, 16 * K * K -1, 16 * K));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
        return frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
}