io.undertow.connector.ByteBufferPool Java Examples

The following examples show how to use io.undertow.connector.ByteBufferPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GetUserHandler.java    From rpc-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleAsyncRequest(HttpServerExchange exchange, PooledByteBufferInputStream content)
		throws Exception {

	Map<String, Deque<String>> params = exchange.getQueryParameters();
	String idStr = params.get("id").getFirst();
	long id = Integer.parseInt(idStr);

	User user = userService.getUser(id);

	ByteBufferPool pool = exchange.getConnection().getByteBufferPool();
	PooledByteBufferOutputStream output = new PooledByteBufferOutputStream(pool);
	objectMapper.writeValue(output, user);

	send(exchange, StatusCodes.OK, output);
}
 
Example #2
Source File: Http2Client.java    From light-4j with Apache License 2.0 6 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
    if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
    ClientProvider provider = getClientProvider(uri);
    final FutureResult<ClientConnection> result = new FutureResult<>();
        provider.connect(new ClientCallback<ClientConnection>() {
            @Override
            public void completed(ClientConnection r) {
                result.setResult(r);
            }

            @Override
            public void failed(IOException e) {
                result.setException(e);
            }
        }, bindAddress, uri, worker, ssl, bufferPool, options);
    return result.getIoFuture();
}
 
Example #3
Source File: AjpClientProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    ChannelListener<StreamConnection> openListener = new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, ssl, bufferPool, options);
        }
    };
    IoFuture.Notifier<StreamConnection, Object> notifier = new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
    if(bindAddress == null) {
        worker.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, options).addNotifier(notifier, null);
    } else {
        worker.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, null, options).addNotifier(notifier, null);
    }
}
 
Example #4
Source File: AjpClientProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress,final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    ChannelListener<StreamConnection> openListener = new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, ssl, bufferPool, options);
        }
    };
    IoFuture.Notifier<StreamConnection, Object> notifier = new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
    if(bindAddress == null) {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, options).addNotifier(notifier, null);
    } else {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, null, options).addNotifier(notifier, null);
    }
}
 
Example #5
Source File: WebSocket13ClientHandshake.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public WebSocketChannel createChannel(final StreamConnection channel, final String wsUri, final ByteBufferPool bufferPool, OptionMap options) {
    if (negotiation != null && negotiation.getSelectedExtensions() != null && !negotiation.getSelectedExtensions().isEmpty()) {

        List<WebSocketExtension> selected = negotiation.getSelectedExtensions();
        List<ExtensionFunction> negotiated = new ArrayList<>();
        if (selected != null && !selected.isEmpty()) {
            for (WebSocketExtension ext : selected) {
                for (ExtensionHandshake extHandshake : extensions) {
                    if (ext.getName().equals(extHandshake.getName())) {
                        negotiated.add(extHandshake.create());
                    }
                }
            }
        }
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation.getSelectedSubProtocol(), true, !negotiated.isEmpty(), CompositeExtensionFunction.compose(negotiated), new HashSet<WebSocketChannel>(), options);
    } else {
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation != null ? negotiation.getSelectedSubProtocol() : "", true, false, NoopExtensionFunction.INSTANCE, new HashSet<WebSocketChannel>(), options);
    }
}
 
Example #6
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
    ClientProvider provider = getClientProvider(uri);
    final FutureResult<ClientConnection> result = new FutureResult<>();
    provider.connect(new ClientCallback<ClientConnection>() {
        @Override
        public void completed(ClientConnection r) {
            result.setResult(r);
        }

        @Override
        public void failed(IOException e) {
            result.setException(e);
        }
    }, bindAddress, uri, worker, ssl, bufferPool, options);
    return result.getIoFuture();
}
 
Example #7
Source File: AbstractServerConnection.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public AbstractServerConnection(StreamConnection channel, final ByteBufferPool bufferPool, final HttpHandler rootHandler, final OptionMap undertowOptions, final int bufferSize) {
    this.channel = channel;
    this.bufferPool = bufferPool;
    this.rootHandler = rootHandler;
    this.undertowOptions = undertowOptions;
    this.bufferSize = bufferSize;
    closeSetter = new CloseSetter();
    if (channel != null) {
        this.originalSinkConduit = channel.getSinkChannel().getConduit();
        this.originalSourceConduit = channel.getSourceChannel().getConduit();
        channel.setCloseListener(closeSetter);
    } else {
        this.originalSinkConduit = null;
        this.originalSourceConduit = null;
    }
}
 
Example #8
Source File: Light4jHttp2ClientProvider.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated will be change to protected in future TODO: not sure if this should be public
 * @param listener {@link ClientCallback}
 * @param uri URI
 * @param bufferPool ByteBufferPool
 * @param options OptionMap
 * @return ALPNClientSelector.ALPNProtocol
 */
@Deprecated
public static ALPNClientSelector.ALPNProtocol alpnProtocol(final ClientCallback<ClientConnection> listener, URI uri, ByteBufferPool bufferPool, OptionMap options) {
    return new ALPNClientSelector.ALPNProtocol(new ChannelListener<SslConnection>() {
        @Override
        public void handleEvent(SslConnection connection) {
            listener.completed(createHttp2Channel(connection, bufferPool, options, uri.getHost()));
        }
    }, HTTP2);
}
 
Example #9
Source File: AjpServerResponseConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
AjpServerResponseConduit(final StreamSinkConduit next, final ByteBufferPool pool, final HttpServerExchange exchange, ConduitListener<? super AjpServerResponseConduit> finishListener, boolean headRequest) {
    super(next);
    this.pool = pool;
    this.exchange = exchange;
    this.finishListener = finishListener;
    this.headRequest = headRequest;
    state = FLAG_START;
}
 
Example #10
Source File: PerMessageDeflateFunction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private PooledByteBuffer toArrayBacked(ByteBuffer buffer, ByteBufferPool pool) {
    if(pool.getBufferSize() < buffer.remaining()) {
        return new ImmediatePooledByteBuffer(ByteBuffer.wrap(Buffers.take(buffer)));
    }
    PooledByteBuffer newBuf = pool.getArrayBackedPool().allocate();
    newBuf.getBuffer().put(buffer);
    newBuf.getBuffer().flip();
    return newBuf;
}
 
Example #11
Source File: AlpnOpenListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AlpnOpenListener(ByteBufferPool bufferPool, OptionMap undertowOptions, String fallbackProtocol, DelegateOpenListener fallbackListener) {
    this.bufferPool = bufferPool;
    this.undertowOptions = undertowOptions;
    this.fallbackProtocol = fallbackProtocol;
    statisticsEnabled = undertowOptions.get(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false);
    if (fallbackProtocol != null && fallbackListener != null) {
        addProtocol(fallbackProtocol, fallbackListener, 0);
    }
}
 
Example #12
Source File: TokenAuthenticator.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private ByteBufferPool createByteBufferPool() {
    long maxMemory = Runtime.getRuntime().maxMemory();
    boolean useDirectBuffers;
    int bufferSize, buffersPerRegion;
    if (maxMemory < 64 * 1024 * 1024) {
        //smaller than 64mb of ram we use 512b buffers
        useDirectBuffers = false;
        bufferSize = 512;
        buffersPerRegion = 10;
    } else if (maxMemory < 128 * 1024 * 1024) {
        //use 1k buffers
        useDirectBuffers = true;
        bufferSize = 1024;
        buffersPerRegion = 10;
    } else {
        //use 16k buffers for best performance
        //as 16k is generally the max amount of data that can be sent in a single write() call
        useDirectBuffers = true;
        bufferSize = 1024 * 16;
        buffersPerRegion = 20;
    }
    BufferAllocator<ByteBuffer> allocator;
    if (useDirectBuffers) {
        allocator = BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR;
    } else {
        allocator = BufferAllocator.BYTE_BUFFER_ALLOCATOR;
    }
    int maxRegionSize = buffersPerRegion * bufferSize;
    ByteBufferSlicePool pool = new ByteBufferSlicePool(allocator, bufferSize, maxRegionSize);
    return new XnioByteBufferPool(pool);
}
 
Example #13
Source File: Light4jHttpClientProvider.java    From light-4j with Apache License 2.0 5 votes vote down vote up
private ClientConnection createHttpClientConnection(final StreamConnection connection, final OptionMap options, final ByteBufferPool bufferPool) {
	try {
		Class<?> cls = Class.forName("io.undertow.client.http.HttpClientConnection");
		
		Constructor<?> o = cls.getDeclaredConstructor(StreamConnection.class, OptionMap.class, ByteBufferPool.class);
		
		o.setAccessible(true);
		
		return (ClientConnection) o.newInstance(connection, options, bufferPool);			
	}catch(Exception e) {
		logger.error(e.getMessage(), e);
	}

	return null;
}
 
Example #14
Source File: MultipartParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static ParseState beginParse(final ByteBufferPool bufferPool, final PartHandler handler, final byte[] boundary, final String requestCharset) {

        // We prepend CR/LF to the boundary to chop trailing CR/LF from
        // body-data tokens.
        byte[] boundaryToken = new byte[boundary.length + BOUNDARY_PREFIX.length];
        System.arraycopy(BOUNDARY_PREFIX, 0, boundaryToken, 0, BOUNDARY_PREFIX.length);
        System.arraycopy(boundary, 0, boundaryToken, BOUNDARY_PREFIX.length, boundary.length);
        return new ParseState(bufferPool, handler, requestCharset, boundaryToken);
    }
 
Example #15
Source File: UndertowXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public Undertow13BufferSupport() {
	this.undertowBufferPool = new DefaultByteBufferPool(false, 1024, -1, 2);
	this.httpClientConnectCallbackMethod = ReflectionUtils.findMethod(UndertowClient.class, "connect",
			ClientCallback.class, URI.class, XnioWorker.class, ByteBufferPool.class, OptionMap.class);
	this.httpClientConnectMethod = ReflectionUtils.findMethod(UndertowClient.class, "connect",
			URI.class, XnioWorker.class, ByteBufferPool.class, OptionMap.class);
}
 
Example #16
Source File: AjpOpenListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AjpOpenListener(final ByteBufferPool pool, final OptionMap undertowOptions) {
    this.undertowOptions = undertowOptions;
    this.bufferPool = pool;
    PooledByteBuffer buf = pool.allocate();
    this.bufferSize = buf.getBuffer().remaining();
    buf.close();
    parser = new AjpRequestParser(undertowOptions.get(URL_CHARSET, StandardCharsets.UTF_8.name()), undertowOptions.get(DECODE_URL, true), undertowOptions.get(UndertowOptions.MAX_PARAMETERS, UndertowOptions.DEFAULT_MAX_PARAMETERS), undertowOptions.get(UndertowOptions.MAX_HEADERS, UndertowOptions.DEFAULT_MAX_HEADERS), undertowOptions.get(UndertowOptions.ALLOW_ENCODED_SLASH, false), undertowOptions.get(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, false));
    connectorStatistics = new ConnectorStatisticsImpl();
    statisticsEnabled = undertowOptions.get(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false);
}
 
Example #17
Source File: Light4jHttp2ClientProvider.java    From light-4j with Apache License 2.0 5 votes vote down vote up
protected ChannelListener<StreamConnection> createOpenListener(final ClientCallback<ClientConnection> listener, final URI uri, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    return new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, bufferPool, options);
        }
    };
}
 
Example #18
Source File: Http2ClearClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Map<String, String> createHeaders(OptionMap options, ByteBufferPool bufferPool, URI uri) {
    Map<String, String> headers = new HashMap<>();
    headers.put("HTTP2-Settings", createSettingsFrame(options, bufferPool));
    headers.put(Headers.UPGRADE_STRING, Http2Channel.CLEARTEXT_UPGRADE_STRING);
    headers.put(Headers.CONNECTION_STRING, "Upgrade, HTTP2-Settings");
    headers.put(Headers.HOST_STRING, uri.getHost());
    headers.put("X-HTTP2-connect-only", "connect"); //undertow specific header that tells the remote server that this request should be ignored
    return headers;
}
 
Example #19
Source File: UndertowWebSocketClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Alternate constructor providing additional control over the
 * {@link ConnectionBuilder} for each WebSocket connection.
 * @param worker the Xnio worker to use to create {@code ConnectionBuilder}'s
 * @param byteBufferPool the ByteBufferPool to use to create {@code ConnectionBuilder}'s
 * @param builderConsumer a consumer to configure {@code ConnectionBuilder}'s
 * @since 5.0.8
 */
public UndertowWebSocketClient(XnioWorker worker, ByteBufferPool byteBufferPool,
		Consumer<ConnectionBuilder> builderConsumer) {

	Assert.notNull(worker, "XnioWorker must not be null");
	Assert.notNull(byteBufferPool, "ByteBufferPool must not be null");
	this.worker = worker;
	this.byteBufferPool = byteBufferPool;
	this.builderConsumer = builderConsumer;
}
 
Example #20
Source File: Http2PriorKnowledgeClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {

    if (bindAddress == null) {
        worker.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()), createOpenListener(listener, bufferPool, options, uri.getHost()), options).addNotifier(createNotifier(listener), null);
    } else {
        worker.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()), createOpenListener(listener, bufferPool, options, uri.getHost()), null, options).addNotifier(createNotifier(listener), null);
    }}
 
Example #21
Source File: Node.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Node(NodeConfig nodeConfig, Balancer balancerConfig, XnioIoThread ioThread, ByteBufferPool bufferPool, ModClusterContainer container) {
    this.id = idGen.incrementAndGet();
    this.jvmRoute = nodeConfig.getJvmRoute();
    this.nodeConfig = nodeConfig;
    this.ioThread = ioThread;
    this.bufferPool = bufferPool;
    this.balancerConfig = balancerConfig;
    this.container = container;
    this.connectionPoolManager = new NodeConnectionPoolManager();
    this.connectionPool = new ProxyConnectionPool(connectionPoolManager, nodeConfig.getConnectionURI(), container.getXnioSsl(), container.getClient(), container.getClientOptions());
}
 
Example #22
Source File: Http2PriorKnowledgeClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, final InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {

    if (bindAddress == null) {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()), createOpenListener(listener, bufferPool, options, uri.getHost()), options).addNotifier(createNotifier(listener), null);
    } else {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()), createOpenListener(listener, bufferPool, options, uri.getHost()), null, options).addNotifier(createNotifier(listener), null);
    }
}
 
Example #23
Source File: ProxyProtocolReadListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
ProxyProtocolReadListener(StreamConnection streamConnection, OpenListener openListener, UndertowXnioSsl ssl, ByteBufferPool bufferPool, OptionMap sslOptionMap) {
    this.streamConnection = streamConnection;
    this.openListener = openListener;
    this.ssl = ssl;
    this.bufferPool = bufferPool;
    this.sslOptionMap = sslOptionMap;
    if (bufferPool.getBufferSize() < MAX_HEADER_LENGTH) {
        throw UndertowMessages.MESSAGES.bufferPoolTooSmall(MAX_HEADER_LENGTH);
    }
}
 
Example #24
Source File: ChunkedStreamSinkConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new instance.
 *
 * @param next            the channel to wrap
 * @param configurable    {@code true} to allow configuration of the next channel, {@code false} otherwise
 * @param passClose       {@code true} to close the underlying channel when this channel is closed, {@code false} otherwise
 * @param responseHeaders The response headers
 * @param finishListener  The finish listener
 * @param attachable      The attachable
 */
public ChunkedStreamSinkConduit(final StreamSinkConduit next, final ByteBufferPool bufferPool, final boolean configurable, final boolean passClose, HeaderMap responseHeaders, final ConduitListener<? super ChunkedStreamSinkConduit> finishListener, final Attachable attachable) {
    super(next);
    this.bufferPool = bufferPool;
    this.responseHeaders = responseHeaders;
    this.finishListener = finishListener;
    this.attachable = attachable;
    config = (configurable ? CONF_FLAG_CONFIGURABLE : 0) | (passClose ? CONF_FLAG_PASS_CLOSE : 0);
    chunkingSepBuffer = ByteBuffer.allocate(2);
    chunkingSepBuffer.flip();
}
 
Example #25
Source File: Http2ClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static ALPNClientSelector.ALPNProtocol alpnProtocol(final ClientCallback<ClientConnection> listener, URI uri, ByteBufferPool bufferPool, OptionMap options) {
    return new ALPNClientSelector.ALPNProtocol(new ChannelListener<SslConnection>() {
        @Override
        public void handleEvent(SslConnection connection) {
            listener.completed(createHttp2Channel(connection, bufferPool, options, uri.getHost()));
        }
    }, HTTP2);
}
 
Example #26
Source File: NodePingUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
HttpClientPingTask(URI connection, RequestExchangeListener exchangeListener, XnioIoThread thread, UndertowClient client, XnioSsl xnioSsl, ByteBufferPool bufferPool, OptionMap options) {
    this.connection = connection;
    this.thread = thread;
    this.client = client;
    this.xnioSsl = xnioSsl;
    this.bufferPool = bufferPool;
    this.options = options;
    this.exchangeListener = exchangeListener;
}
 
Example #27
Source File: HttpOpenListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public HttpOpenListener(final ByteBufferPool pool, final OptionMap undertowOptions) {
    this.undertowOptions = undertowOptions;
    this.bufferPool = pool;
    PooledByteBuffer buf = pool.allocate();
    this.bufferSize = buf.getBuffer().remaining();
    buf.close();
    parser = HttpRequestParser.instance(undertowOptions);
    connectorStatistics = new ConnectorStatisticsImpl();
    statisticsEnabled = undertowOptions.get(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false);
}
 
Example #28
Source File: AjpServerConnection.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public AjpServerConnection(StreamConnection channel, ByteBufferPool bufferPool, HttpHandler rootHandler, OptionMap undertowOptions, int bufferSize) {
    super(channel, bufferPool, rootHandler, undertowOptions, bufferSize);
    this.writeReadyHandler = new WriteReadyHandler.ChannelListenerHandler<>(channel.getSinkChannel());
}
 
Example #29
Source File: Http2Client.java    From light-4j with Apache License 2.0 4 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
    return connect(bindAddress, uri, worker, null, bufferPool, options);
}
 
Example #30
Source File: Http2ClientProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioWorker worker, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    connect(listener, null, uri, worker, ssl, bufferPool, options);
}