Search in sources :

Example 1 with HttpClientMetrics

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);
}
Also used : HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics)

Example 2 with HttpClientMetrics

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);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpHeaders(io.vertx.core.http.HttpHeaders) WebSocketClientExtensionHandler(io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler) HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics) URI(java.net.URI) WebSocketClientExtensionHandshaker(io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker)

Example 3 with HttpClientMetrics

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);
}
Also used : HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics)

Example 4 with HttpClientMetrics

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());
    }
}
Also used : VertxTracer(io.vertx.core.spi.tracing.VertxTracer) HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics) WriteStream(io.vertx.core.streams.WriteStream)

Example 5 with HttpClientMetrics

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;
}
Also used : HttpClientMetrics(io.vertx.core.spi.metrics.HttpClientMetrics)

Aggregations

HttpClientMetrics (io.vertx.core.spi.metrics.HttpClientMetrics)5 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 WebSocketClientExtensionHandler (io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler)1 WebSocketClientExtensionHandshaker (io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 VertxTracer (io.vertx.core.spi.tracing.VertxTracer)1 WriteStream (io.vertx.core.streams.WriteStream)1 URI (java.net.URI)1