|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | +package io.vertx.core.http.impl; |
| 12 | + |
| 13 | +import io.netty.handler.codec.http3.Http3; |
| 14 | +import io.vertx.core.Future; |
| 15 | +import io.vertx.core.Promise; |
| 16 | +import io.vertx.core.http.Http3ClientOptions; |
| 17 | +import io.vertx.core.http.impl.http3.Http3ClientConnection; |
| 18 | +import io.vertx.core.internal.ContextInternal; |
| 19 | +import io.vertx.core.internal.VertxInternal; |
| 20 | +import io.vertx.core.internal.quic.QuicConnectionInternal; |
| 21 | +import io.vertx.core.net.*; |
| 22 | +import io.vertx.core.spi.metrics.ClientMetrics; |
| 23 | + |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.concurrent.locks.Lock; |
| 27 | +import java.util.concurrent.locks.ReentrantLock; |
| 28 | + |
| 29 | +public class Http3ChannelConnector implements HttpChannelConnector { |
| 30 | + |
| 31 | + private final VertxInternal vertx; |
| 32 | + private final Lock lock; |
| 33 | + private Future<QuicClient> clientFuture; |
| 34 | + private final Http3ClientOptions options; |
| 35 | + |
| 36 | + public Http3ChannelConnector(VertxInternal vertxInternal, Http3ClientOptions options) { |
| 37 | + |
| 38 | + options = new Http3ClientOptions(options); |
| 39 | + options.getSslOptions().setApplicationLayerProtocols(Arrays.asList(Http3.supportedApplicationProtocols())); |
| 40 | + options.getTransportOptions().setInitialMaxData(10000000L); |
| 41 | + options.getTransportOptions().setInitialMaxStreamDataBidirectionalLocal(1000000L); |
| 42 | + options.getTransportOptions().setInitialMaxStreamDataBidirectionalRemote(1000000L); |
| 43 | + options.getTransportOptions().setInitialMaxStreamDataUnidirectional(1000000L); |
| 44 | + options.getTransportOptions().setInitialMaxStreamsBidirectional(100L); |
| 45 | + options.getTransportOptions().setInitialMaxStreamsUnidirectional(100L); |
| 46 | + |
| 47 | + this.vertx = vertxInternal; |
| 48 | + this.lock = new ReentrantLock(); |
| 49 | + this.options = options; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Future<HttpClientConnection> httpConnect(ContextInternal context, SocketAddress server, HostAndPort authority, HttpConnectParams params, long maxLifetimeMillis, ClientMetrics<?, ?, ?> metrics) { |
| 54 | + |
| 55 | + lock.lock(); |
| 56 | + Future<QuicClient> fut = clientFuture; |
| 57 | + if (fut == null) { |
| 58 | + QuicClient client = QuicClient.create(vertx, this.options); |
| 59 | + fut = client.bind(SocketAddress.inetSocketAddress(0, "localhost")).map(client); |
| 60 | + clientFuture = fut; |
| 61 | + lock.unlock(); |
| 62 | + } else { |
| 63 | + lock.unlock(); |
| 64 | + } |
| 65 | + Promise<HttpClientConnection> promise = context.promise(); |
| 66 | + |
| 67 | + fut.onComplete((res, err) -> { |
| 68 | + if (err == null) { |
| 69 | + Future<QuicConnection> f = res.connect(server); |
| 70 | + f.onComplete((res2, err2) -> { |
| 71 | + if (err2 == null) { |
| 72 | + Http3ClientConnection c = new Http3ClientConnection((QuicConnectionInternal) res2); |
| 73 | + c.init(); |
| 74 | + promise.complete(c); |
| 75 | + } else { |
| 76 | + promise.fail(err2); |
| 77 | + } |
| 78 | + }); |
| 79 | + } else { |
| 80 | + promise.fail(err); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + return promise.future(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Future<Void> shutdown(Duration timeout) { |
| 89 | + if (clientFuture == null) { |
| 90 | + return vertx.getOrCreateContext().succeededFuture(); |
| 91 | + } else { |
| 92 | + return clientFuture.compose(client -> client.shutdown(timeout)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Future<Void> close() { |
| 98 | + if (clientFuture == null) { |
| 99 | + return vertx.getOrCreateContext().succeededFuture(); |
| 100 | + } else { |
| 101 | + return clientFuture.compose(QuicEndpoint::close); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments