Search in sources :

Example 1 with ProxyConnection

use of io.undertow.server.handlers.proxy.ProxyConnection in project undertow by undertow-io.

the class ModClusterProxyClient method getConnection.

public void getConnection(final ProxyTarget target, final HttpServerExchange exchange, final ProxyCallback<ProxyConnection> callback, final long timeout, final TimeUnit timeUnit) {
    final ExclusiveConnectionHolder holder = exchange.getConnection().getAttachment(exclusiveConnectionKey);
    if (holder != null && holder.connection.getConnection().isOpen()) {
        // Something has already caused an exclusive connection to be
        // allocated so keep using it.
        callback.completed(exchange, holder.connection);
        return;
    }
    if (!(target instanceof ModClusterProxyTarget)) {
        callback.couldNotResolveBackend(exchange);
        return;
    }
    // Resolve the node
    final ModClusterProxyTarget proxyTarget = (ModClusterProxyTarget) target;
    final Context context = proxyTarget.resolveContext(exchange);
    if (context == null) {
        callback.couldNotResolveBackend(exchange);
    } else {
        if (holder != null || (exclusivityChecker != null && exclusivityChecker.isExclusivityRequired(exchange))) {
            // If we have a holder, even if the connection was closed we now
            // exclusivity was already requested so our client
            // may be assuming it still exists.
            final ProxyCallback<ProxyConnection> wrappedCallback = new ProxyCallback<ProxyConnection>() {

                @Override
                public void completed(HttpServerExchange exchange, ProxyConnection result) {
                    if (holder != null) {
                        holder.connection = result;
                    } else {
                        final ExclusiveConnectionHolder newHolder = new ExclusiveConnectionHolder();
                        newHolder.connection = result;
                        ServerConnection connection = exchange.getConnection();
                        connection.putAttachment(exclusiveConnectionKey, newHolder);
                        connection.addCloseListener(new ServerConnection.CloseListener() {

                            @Override
                            public void closed(ServerConnection connection) {
                                ClientConnection clientConnection = newHolder.connection.getConnection();
                                if (clientConnection.isOpen()) {
                                    safeClose(clientConnection);
                                }
                            }
                        });
                    }
                    callback.completed(exchange, result);
                }

                @Override
                public void queuedRequestFailed(HttpServerExchange exchange) {
                    callback.queuedRequestFailed(exchange);
                }

                @Override
                public void failed(HttpServerExchange exchange) {
                    callback.failed(exchange);
                }

                @Override
                public void couldNotResolveBackend(HttpServerExchange exchange) {
                    callback.couldNotResolveBackend(exchange);
                }
            };
            context.handleRequest(proxyTarget, exchange, wrappedCallback, timeout, timeUnit, true);
        } else {
            context.handleRequest(proxyTarget, exchange, callback, timeout, timeUnit, false);
        }
    }
}
Also used : ProxyCallback(io.undertow.server.handlers.proxy.ProxyCallback) HttpServerExchange(io.undertow.server.HttpServerExchange) ProxyConnection(io.undertow.server.handlers.proxy.ProxyConnection) ServerConnection(io.undertow.server.ServerConnection) ClientConnection(io.undertow.client.ClientConnection)

Aggregations

ClientConnection (io.undertow.client.ClientConnection)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ServerConnection (io.undertow.server.ServerConnection)1 ProxyCallback (io.undertow.server.handlers.proxy.ProxyCallback)1 ProxyConnection (io.undertow.server.handlers.proxy.ProxyConnection)1