Search in sources :

Example 1 with PermitQueue

use of com.webpieces.util.locking.PermitQueue in project webpieces by deanhiller.

the class TestPermitQueue method testReducePermits.

@Test
public void testReducePermits() throws InterruptedException {
    PermitQueue queue = new PermitQueue(2);
    CompletableFuture<Long> future1 = new CompletableFuture<Long>();
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    queue.runRequest(() -> svc.runFunction(1));
    queue.runRequest(() -> svc.runFunction(2));
    queue.runRequest(() -> svc.runFunction(3));
    queue.runRequest(() -> svc.runFunction(4));
    List<Integer> results = svc.getAndClear();
    Assert.assertEquals(2, results.size());
    Assert.assertEquals(1, results.get(0).intValue());
    Assert.assertEquals(2, results.get(1).intValue());
    queue.modifyPermitPoolSize(-1);
    //release two
    queue.releasePermit();
    queue.releasePermit();
    //only one will run
    List<Integer> results2 = svc.getAndClear();
    Assert.assertEquals(1, results2.size());
    Assert.assertEquals(3, results2.get(0).intValue());
    queue.releasePermit();
    List<Integer> results3 = svc.getAndClear();
    Assert.assertEquals(1, results3.size());
    Assert.assertEquals(4, results3.get(0).intValue());
}
Also used : PermitQueue(com.webpieces.util.locking.PermitQueue) FuturePermitQueue(com.webpieces.util.locking.FuturePermitQueue) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 2 with PermitQueue

use of com.webpieces.util.locking.PermitQueue in project webpieces by deanhiller.

the class Layer2Http1_1Handler method processData.

private CompletableFuture<Void> processData(FrontendSocketImpl socket, DataFrame msg) {
    PermitQueue permitQueue = socket.getPermitQueue();
    return permitQueue.runRequest(() -> {
        Http1_1StreamImpl stream = socket.getCurrentStream();
        StreamWriter requestWriter = stream.getRequestWriter();
        if (msg.isEndOfStream())
            stream.setSentFullRequest(true);
        return requestWriter.processPiece(msg).thenApply(w -> {
            stream.setRequestWriter(w);
            if (!msg.isEndOfStream())
                permitQueue.releasePermit();
            return null;
        });
    });
}
Also used : PermitQueue(com.webpieces.util.locking.PermitQueue) StreamWriter(com.webpieces.http2engine.api.StreamWriter)

Example 3 with PermitQueue

use of com.webpieces.util.locking.PermitQueue in project webpieces by deanhiller.

the class TestPermitQueue method testAddPermits.

@Test
public void testAddPermits() throws InterruptedException {
    PermitQueue queue = new PermitQueue(1);
    CompletableFuture<Long> future1 = new CompletableFuture<Long>();
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    queue.runRequest(() -> svc.runFunction(1));
    queue.runRequest(() -> svc.runFunction(2));
    queue.runRequest(() -> svc.runFunction(3));
    queue.runRequest(() -> svc.runFunction(4));
    List<Integer> results = svc.getAndClear();
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(1, results.get(0).intValue());
    queue.modifyPermitPoolSize(2);
    List<Integer> results2 = svc.getAndClear();
    Assert.assertEquals(2, results2.size());
    Assert.assertEquals(2, results2.get(0).intValue());
    Assert.assertEquals(3, results2.get(1).intValue());
    queue.releasePermit();
    List<Integer> results3 = svc.getAndClear();
    Assert.assertEquals(1, results3.size());
    Assert.assertEquals(4, results3.get(0).intValue());
}
Also used : PermitQueue(com.webpieces.util.locking.PermitQueue) FuturePermitQueue(com.webpieces.util.locking.FuturePermitQueue) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 4 with PermitQueue

use of com.webpieces.util.locking.PermitQueue in project webpieces by deanhiller.

the class Layer2Http1_1Handler method processInitialPieceOfRequest.

private CompletableFuture<Void> processInitialPieceOfRequest(FrontendSocketImpl socket, HttpRequest http1Req, Http2Request headers) {
    int id = counter.getAndAdd(2);
    PermitQueue permitQueue = socket.getPermitQueue();
    return permitQueue.runRequest(() -> {
        Http1_1StreamImpl currentStream = new Http1_1StreamImpl(id, socket, httpParser, permitQueue);
        HttpStream streamHandle = httpListener.openStream();
        currentStream.setStreamHandle(streamHandle);
        socket.setCurrentStream(currentStream);
        if (!headers.isEndOfStream()) {
            //data run right after the request
            return streamHandle.incomingRequest(headers, currentStream).thenApply(w -> {
                currentStream.setRequestWriter(w);
                permitQueue.releasePermit();
                return null;
            });
        } else {
            //in this case, since this is the END of the request, we cannot release the permit in the
            //permit queue as we do not want to let the next request to start until the full response is
            //sent back to the client
            currentStream.setSentFullRequest(true);
            return streamHandle.incomingRequest(headers, currentStream).thenApply(w -> {
                currentStream.setRequestWriter(w);
                return null;
            });
        }
    });
}
Also used : PermitQueue(com.webpieces.util.locking.PermitQueue) HttpStream(org.webpieces.frontend2.api.HttpStream)

Aggregations

PermitQueue (com.webpieces.util.locking.PermitQueue)4 FuturePermitQueue (com.webpieces.util.locking.FuturePermitQueue)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Test (org.junit.Test)2 StreamWriter (com.webpieces.http2engine.api.StreamWriter)1 HttpStream (org.webpieces.frontend2.api.HttpStream)1