Search in sources :

Example 21 with TwoPools

use of org.webpieces.data.api.TwoPools in project webpieces by deanhiller.

the class AsyncSSLEngine3Impl method firePlainPacketToListener.

private void firePlainPacketToListener(ByteBuffer cachedOutBuffer, int totalBytesToAck) {
    cachedOutBuffer.flip();
    metrics.recordPlainBytesToClient(cachedOutBuffer.remaining());
    if (pool instanceof TwoPools) {
        // (we don't want too many large buffers as that eats up memory very fast)
        if (cachedOutBuffer.remaining() <= pool.getSuggestedBufferSize()) {
            ByteBuffer largeSslBuffer = cachedOutBuffer;
            cachedOutBuffer = pool.nextBuffer(largeSslBuffer.remaining());
            cachedOutBuffer.put(largeSslBuffer);
            cachedOutBuffer.flip();
            pool.releaseBuffer(largeSslBuffer);
        }
    }
    listener.packetUnencrypted(cachedOutBuffer).handle((v, t) -> {
        if (t != null)
            log.error("Exception in ssl listener", t);
        // after the client consumes the packet(completes the future), ack the number of bytes corresponding
        // to the packet we sent to the consumer.  NOTE: We do NOT care about how many bytes we sent the
        // consumer BUT only how many encrypted bytes there were to ack those bytes and release our nio library to
        // keep processing.  (in this way, the lower layer shuts off the peer from writing to us if your app gets slow)
        decryptionTracker.ackBytes(totalBytesToAck);
        return null;
    });
}
Also used : TwoPools(org.webpieces.data.api.TwoPools) ByteBuffer(java.nio.ByteBuffer)

Example 22 with TwoPools

use of org.webpieces.data.api.TwoPools in project webpieces by deanhiller.

the class TestSplitBackpressure method setup.

@Before
public void setup() throws GeneralSecurityException, IOException, InterruptedException, ExecutionException, TimeoutException {
    System.setProperty("jdk.tls.server.protocols", "TLSv1.2");
    System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
    SSLMetrics metrics = new SSLMetrics("", new SimpleMeterRegistry());
    MockSSLEngineFactory sslEngineFactory = new MockSSLEngineFactory();
    BufferPool pool = new TwoPools("p1", new SimpleMeterRegistry());
    SSLEngine client = sslEngineFactory.createEngineForSocket();
    SSLEngine svr = sslEngineFactory.createEngineForServerSocket();
    clientEngine = AsyncSSLFactory.create("client", client, pool, clientListener, metrics);
    svrEngine = AsyncSSLFactory.create("svr", svr, pool, svrListener, metrics);
    Assert.assertEquals(ConnectionState.NOT_STARTED, clientEngine.getConnectionState());
    Assert.assertEquals(ConnectionState.NOT_STARTED, svrEngine.getConnectionState());
    XFuture<Void> clientFuture = clientEngine.beginHandshake();
    Assert.assertEquals(ConnectionState.CONNECTING, clientEngine.getConnectionState());
    BufferedFuture toSend = clientListener.getSingleHandshake();
    assertFutureJustResolved(clientFuture, toSend);
    List<ByteBuffer> split = split(toSend.engineToSocketData);
    // TODO: feed in this packet as two and resolving 'both' resolves the future returned
    XFuture<Void> svrFuture = svrEngine.feedEncryptedPacket(split.get(0));
    Assert.assertEquals(ConnectionState.CONNECTING, svrEngine.getConnectionState());
    XFuture<Void> svrFuture2 = svrEngine.feedEncryptedPacket(split.get(1));
    Assert.assertEquals(ConnectionState.CONNECTING, svrEngine.getConnectionState());
    BufferedFuture toSend2 = svrListener.getSingleHandshake();
    Assert.assertFalse(svrFuture.isDone());
    Assert.assertFalse(svrFuture2.isDone());
    toSend2.future.complete(null);
    svrFuture.get(2, TimeUnit.SECONDS);
    svrFuture2.get(2, TimeUnit.SECONDS);
    // TODO: feed 2 halves of this packet in and test resolution of future
    List<ByteBuffer> split2 = split(toSend2.engineToSocketData);
    XFuture<Void> clientFuture1 = clientEngine.feedEncryptedPacket(split2.get(0));
    Assert.assertEquals(ConnectionState.CONNECTING, clientEngine.getConnectionState());
    XFuture<Void> clientFuture2 = clientEngine.feedEncryptedPacket(split2.get(1));
    List<BufferedFuture> buffers = clientListener.getHandshake();
    buffers.get(0).future.complete(null);
    buffers.get(1).future.complete(null);
    Assert.assertFalse(clientFuture1.isDone());
    Assert.assertFalse(clientFuture2.isDone());
    buffers.get(2).future.complete(null);
    clientFuture1.get(2, TimeUnit.SECONDS);
    clientFuture2.get(2, TimeUnit.SECONDS);
    XFuture<Void> fut1 = svrEngine.feedEncryptedPacket(buffers.get(0).engineToSocketData);
    fut1.get(2, TimeUnit.SECONDS);
    XFuture<Void> fut2 = svrEngine.feedEncryptedPacket(buffers.get(1).engineToSocketData);
    Assert.assertEquals(ConnectionState.CONNECTING, clientEngine.getConnectionState());
    fut2.get(2, TimeUnit.SECONDS);
    XFuture<Void> fut3 = svrEngine.feedEncryptedPacket(buffers.get(2).engineToSocketData);
    Assert.assertEquals(ConnectionState.CONNECTED, svrEngine.getConnectionState());
    Assert.assertTrue(svrListener.connected);
    List<BufferedFuture> toClientBuffers = svrListener.getHandshake();
    toClientBuffers.get(0).future.complete(null);
    assertFutureJustResolved(fut3, toClientBuffers.get(1));
    XFuture<Void> cliFut = clientEngine.feedEncryptedPacket(toClientBuffers.get(0).engineToSocketData);
    Assert.assertEquals(ConnectionState.CONNECTING, clientEngine.getConnectionState());
    cliFut.get(2, TimeUnit.SECONDS);
    XFuture<Void> cliFut2 = clientEngine.feedEncryptedPacket(toClientBuffers.get(1).engineToSocketData);
    Assert.assertEquals(ConnectionState.CONNECTED, clientEngine.getConnectionState());
    Assert.assertTrue(clientListener.connected);
    cliFut2.get(2, TimeUnit.SECONDS);
}
Also used : BufferPool(org.webpieces.data.api.BufferPool) TwoPools(org.webpieces.data.api.TwoPools) SSLEngine(javax.net.ssl.SSLEngine) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) BufferedFuture(org.webpieces.ssl.api.MockSslListener.BufferedFuture) ByteBuffer(java.nio.ByteBuffer) Before(org.junit.Before)

Example 23 with TwoPools

use of org.webpieces.data.api.TwoPools in project webpieces by deanhiller.

the class IntegSingleRequest method createHttpClient.

public static Http2Socket createHttpClient(String id, boolean isHttp, InetSocketAddress addr) {
    BufferPool pool2 = new TwoPools("pl", new SimpleMeterRegistry());
    HpackParser hpackParser = HpackParserFactory.createParser(pool2, false);
    Executor executor2 = Executors.newFixedThreadPool(10, new NamedThreadFactory("clientThread"));
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory(Metrics.globalRegistry);
    ChannelManager mgr = factory.createMultiThreadedChanMgr("client", pool2, new BackpressureConfig(), executor2);
    InjectionConfig injConfig = new InjectionConfig(hpackParser);
    String host = addr.getHostName();
    int port = addr.getPort();
    ForTestSslClientEngineFactory ssl = new ForTestSslClientEngineFactory();
    SSLEngine engine = ssl.createSslEngine(host, port);
    Http2Client client = Http2ClientFactory.createHttpClient(id, mgr, injConfig);
    Http2Socket socket;
    if (isHttp) {
        socket = client.createHttpSocket(new SocketListener());
    } else {
        socket = client.createHttpsSocket(engine, new SocketListener());
    }
    return socket;
}
Also used : HpackParser(com.webpieces.hpack.api.HpackParser) BackpressureConfig(org.webpieces.nio.api.BackpressureConfig) ChannelManager(org.webpieces.nio.api.ChannelManager) TwoPools(org.webpieces.data.api.TwoPools) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) SSLEngine(javax.net.ssl.SSLEngine) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) InjectionConfig(com.webpieces.http2engine.api.client.InjectionConfig) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) BufferPool(org.webpieces.data.api.BufferPool) Executor(java.util.concurrent.Executor) SocketListener(org.webpieces.http2client.SocketListener) Http2Socket(org.webpieces.http2client.api.Http2Socket) Http2Client(org.webpieces.http2client.api.Http2Client)

Example 24 with TwoPools

use of org.webpieces.data.api.TwoPools in project webpieces by deanhiller.

the class TestHttp1Backpressure method create3BuffersWithTwoMessags.

private List<ByteBuffer> create3BuffersWithTwoMessags(Http2Response response1, HttpData response2) {
    HttpStatefulParser parser = HttpParserFactory.createStatefulParser("a", new SimpleMeterRegistry(), new TwoPools("pl", new SimpleMeterRegistry()));
    HttpResponse resp1 = Http2ToHttp11.translateResponse(response1);
    ByteBuffer buf1 = parser.marshalToByteBuffer(resp1);
    ByteBuffer buf2 = parser.marshalToByteBuffer(response2);
    byte[] part1 = new byte[10];
    byte[] part2 = new byte[buf1.remaining()];
    buf1.get(part1);
    int toWrite = buf1.remaining();
    buf1.get(part2, 0, toWrite);
    buf2.get(part2, toWrite, part2.length - toWrite);
    byte[] part3 = new byte[buf2.remaining()];
    buf2.get(part3);
    List<ByteBuffer> buffers = new ArrayList<>();
    buffers.add(ByteBuffer.wrap(part1));
    buffers.add(ByteBuffer.wrap(part2));
    buffers.add(ByteBuffer.wrap(part3));
    return buffers;
}
Also used : HttpStatefulParser(org.webpieces.httpparser.api.HttpStatefulParser) TwoPools(org.webpieces.data.api.TwoPools) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) ByteBuffer(java.nio.ByteBuffer)

Example 25 with TwoPools

use of org.webpieces.data.api.TwoPools in project webpieces by deanhiller.

the class TestHttp1Backpressure method setup.

@Before
public void setup() throws InterruptedException, ExecutionException, TimeoutException {
    BufferPool pool = new TwoPools("pl", new SimpleMeterRegistry());
    httpClient = Http2to11ClientFactory.createHttpClient("myClient3", mockChannelMgr, new SimpleMeterRegistry(), pool);
    mockChannelMgr.addTCPChannelToReturn(mockChannel);
    socket = httpClient.createHttpSocket(new Http2CloseListener());
    mockChannel.setConnectFuture(XFuture.completedFuture(null));
    XFuture<Void> future = socket.connect(new InetSocketAddress(8080));
    future.get(2, TimeUnit.SECONDS);
}
Also used : Http2CloseListener(org.webpieces.httpclient.Http2CloseListener) BufferPool(org.webpieces.data.api.BufferPool) TwoPools(org.webpieces.data.api.TwoPools) InetSocketAddress(java.net.InetSocketAddress) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Before(org.junit.Before)

Aggregations

TwoPools (org.webpieces.data.api.TwoPools)43 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)39 Before (org.junit.Before)15 BufferPool (org.webpieces.data.api.BufferPool)15 ChannelManager (org.webpieces.nio.api.ChannelManager)12 InetSocketAddress (java.net.InetSocketAddress)11 ChannelManagerFactory (org.webpieces.nio.api.ChannelManagerFactory)11 NamedThreadFactory (org.webpieces.util.threading.NamedThreadFactory)10 Executor (java.util.concurrent.Executor)8 SSLEngine (javax.net.ssl.SSLEngine)8 AsyncServerManager (org.webpieces.asyncserver.api.AsyncServerManager)8 DirectExecutor (org.webpieces.util.threading.DirectExecutor)8 ByteBuffer (java.nio.ByteBuffer)7 AsyncConfig (org.webpieces.asyncserver.api.AsyncConfig)7 BackpressureConfig (org.webpieces.nio.api.BackpressureConfig)7 AsyncServer (org.webpieces.asyncserver.api.AsyncServer)6 HttpParser (org.webpieces.httpparser.api.HttpParser)6 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)5 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)5 ArrayList (java.util.ArrayList)4