use of io.vertx.core.spi.metrics.HttpClientMetrics in project vert.x by eclipse.
the class HttpChannelConnector method http1xConnected.
private void http1xConnected(HttpVersion version, SocketAddress server, boolean ssl, ContextInternal context, Object socketMetric, Channel ch, Promise<HttpClientConnection> future) {
boolean upgrade = version == HttpVersion.HTTP_2 && options.isHttp2ClearTextUpgrade();
VertxHandler<Http1xClientConnection> clientHandler = VertxHandler.create(chctx -> {
HttpClientMetrics met = client.metrics();
Http1xClientConnection conn = new Http1xClientConnection(upgrade ? HttpVersion.HTTP_1_1 : version, client, chctx, ssl, server, context, this.metrics);
if (met != null) {
conn.metric(socketMetric);
met.endpointConnected(metrics);
}
return conn;
});
clientHandler.addHandler(conn -> {
if (upgrade) {
future.complete(new Http2UpgradedClientConnection(client, conn));
} else {
future.complete(conn);
}
});
ch.pipeline().addLast("handler", clientHandler);
}
use of io.vertx.core.spi.metrics.HttpClientMetrics in project vert.x by eclipse.
the class Http1xClientConnection method toWebSocket.
synchronized void toWebSocket(ContextInternal context, String requestURI, MultiMap headers, boolean allowOriginHeader, WebsocketVersion vers, List<String> subProtocols, int maxWebSocketFrameSize, Promise<WebSocket> promise) {
try {
URI wsuri = new URI(requestURI);
if (!wsuri.isAbsolute()) {
// Netty requires an absolute url
wsuri = new URI((ssl ? "https:" : "http:") + "//" + server.host() + ":" + server.port() + requestURI);
}
WebSocketVersion version = WebSocketVersion.valueOf((vers == null ? WebSocketVersion.V13 : vers).toString());
HttpHeaders nettyHeaders;
if (headers != null) {
nettyHeaders = new DefaultHttpHeaders();
for (Map.Entry<String, String> entry : headers) {
nettyHeaders.add(entry.getKey(), entry.getValue());
}
} else {
nettyHeaders = null;
}
ChannelPipeline p = chctx.channel().pipeline();
ArrayList<WebSocketClientExtensionHandshaker> extensionHandshakers = initializeWebSocketExtensionHandshakers(client.getOptions());
if (!extensionHandshakers.isEmpty()) {
p.addBefore("handler", "webSocketsExtensionsHandler", new WebSocketClientExtensionHandler(extensionHandshakers.toArray(new WebSocketClientExtensionHandshaker[0])));
}
String subp = null;
if (subProtocols != null) {
subp = String.join(",", subProtocols);
}
WebSocketClientHandshaker handshaker = newHandshaker(wsuri, version, subp, !extensionHandshakers.isEmpty(), allowOriginHeader, nettyHeaders, maxWebSocketFrameSize, !options.isSendUnmaskedFrames());
WebSocketHandshakeInboundHandler handshakeInboundHandler = new WebSocketHandshakeInboundHandler(handshaker, ar -> {
AsyncResult<WebSocket> wsRes = ar.map(v -> {
WebSocketImpl w = new WebSocketImpl(context, Http1xClientConnection.this, version != WebSocketVersion.V00, options.getWebSocketClosingTimeout(), options.getMaxWebSocketFrameSize(), options.getMaxWebSocketMessageSize());
w.subProtocol(handshaker.actualSubprotocol());
return w;
});
if (ar.failed()) {
close();
} else {
webSocket = (WebSocketImpl) wsRes.result();
webSocket.registerHandler(vertx.eventBus());
log.debug("WebSocket handshake complete");
HttpClientMetrics metrics = client.metrics();
if (metrics != null) {
webSocket.setMetric(metrics.connected(webSocket));
}
}
getContext().emit(wsRes, res -> {
if (res.succeeded()) {
webSocket.headers(ar.result());
}
promise.handle(res);
if (res.succeeded()) {
webSocket.headers(null);
}
});
});
p.addBefore("handler", "handshakeCompleter", handshakeInboundHandler);
handshaker.handshake(chctx.channel());
} catch (Exception e) {
handleException(e);
}
}
use of io.vertx.core.spi.metrics.HttpClientMetrics in project vert.x by eclipse.
the class WebSocketImpl method handleClose.
@Override
protected void handleClose(boolean graceful) {
HttpClientMetrics metrics = conn.metrics();
if (METRICS_ENABLED && metrics != null) {
metrics.disconnected(getMetric());
setMetric(null);
}
super.handleClose(graceful);
}
use of io.vertx.core.spi.metrics.HttpClientMetrics in project vert.x by eclipse.
the class Http1xClientConnection method handleClosed.
protected void handleClosed() {
super.handleClosed();
long timerID = shutdownTimerID;
if (timerID != -1) {
shutdownTimerID = -1L;
vertx.cancelTimer(timerID);
}
closed = true;
if (metrics != null) {
HttpClientMetrics met = client.metrics();
met.endpointDisconnected(metrics);
}
if (!evicted) {
evicted = true;
if (evictionHandler != null) {
evictionHandler.handle(null);
}
}
WebSocketImpl ws;
VertxTracer tracer = context.tracer();
List<Stream> allocatedStreams;
List<Stream> sentStreams;
synchronized (this) {
ws = webSocket;
sentStreams = new ArrayList<>(responses);
allocatedStreams = new ArrayList<>(requests);
allocatedStreams.removeAll(responses);
}
if (ws != null) {
ws.handleConnectionClosed();
}
for (Stream stream : allocatedStreams) {
stream.context.execute(null, v -> stream.handleClosed());
}
for (Stream stream : sentStreams) {
if (metrics != null) {
metrics.requestReset(stream.metric);
}
Object trace = stream.trace;
if (tracer != null && trace != null) {
tracer.receiveResponse(stream.context, null, trace, ConnectionBase.CLOSED_EXCEPTION, TagExtractor.empty());
}
stream.context.execute(null, v -> stream.handleClosed());
}
}
use of io.vertx.core.spi.metrics.HttpClientMetrics in project vert.x by eclipse.
the class Http2ClientConnection method createHttp2ConnectionHandler.
public static VertxHttp2ConnectionHandler<Http2ClientConnection> createHttp2ConnectionHandler(HttpClientImpl client, ClientMetrics metrics, EventLoopContext context, boolean upgrade, Object socketMetric, Handler<Http2ClientConnection> c) {
HttpClientOptions options = client.getOptions();
HttpClientMetrics met = client.metrics();
VertxHttp2ConnectionHandler<Http2ClientConnection> handler = new VertxHttp2ConnectionHandlerBuilder<Http2ClientConnection>().server(false).useCompression(client.getOptions().isTryUseCompression()).gracefulShutdownTimeoutMillis(// So client close tests don't hang 30 seconds - make this configurable later but requires HTTP/1 impl
0).initialSettings(client.getOptions().getInitialSettings()).connectionFactory(connHandler -> {
Http2ClientConnection conn = new Http2ClientConnection(client, context, connHandler, metrics);
if (metrics != null) {
Object m = socketMetric;
conn.metric(m);
}
return conn;
}).logEnabled(options.getLogActivity()).build();
handler.addHandler(conn -> {
if (options.getHttp2ConnectionWindowSize() > 0) {
conn.setWindowSize(options.getHttp2ConnectionWindowSize());
}
if (metrics != null) {
if (!upgrade) {
met.endpointConnected(metrics);
}
}
c.handle(conn);
});
handler.removeHandler(conn -> {
if (metrics != null) {
met.endpointDisconnected(metrics);
}
conn.tryEvict();
});
return handler;
}
Aggregations