Search in sources :

Example 1 with PermitQueue

use of org.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);
    XFuture<Long> future1 = new XFuture<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 : FuturePermitQueue(org.webpieces.util.locking.FuturePermitQueue) PermitQueue(org.webpieces.util.locking.PermitQueue) XFuture(org.webpieces.util.futures.XFuture) Test(org.junit.Test)

Example 2 with PermitQueue

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

the class Layer2Http11Handler method processInitialPieceOfRequest.

private XFuture<Void> processInitialPieceOfRequest(FrontendSocketImpl socket, HttpRequest http1Req, Http2Request headers) {
    int id = counter.getAndAdd(2);
    PermitQueue permitQueue = socket.getPermitQueue();
    return permitQueue.runRequest(() -> {
        Http11StreamImpl currentStream = new Http11StreamImpl(id, socket, httpParser, permitQueue, http1Req, headers);
        HttpStream streamHandle = httpListener.openStream(socket);
        currentStream.setStreamHandle(streamHandle);
        socket.setCurrentStream(currentStream);
        if (!headers.isEndOfStream()) {
            // in this case, we are NOT at the end of the request so we must let the next piece of
            // data run right after the request
            // TODO(dhiller): Replace this section with futureUtil.trySuccessFinally
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> {
                // must release the permit so the next data piece(which may be cached) can come in
                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);
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> null);
        }
    });
}
Also used : PermitQueue(org.webpieces.util.locking.PermitQueue) StreamRef(com.webpieces.http2.api.streaming.StreamRef) HttpStream(org.webpieces.frontend2.api.HttpStream)

Example 3 with PermitQueue

use of org.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);
    XFuture<Long> future1 = new XFuture<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 : FuturePermitQueue(org.webpieces.util.locking.FuturePermitQueue) PermitQueue(org.webpieces.util.locking.PermitQueue) XFuture(org.webpieces.util.futures.XFuture) Test(org.junit.Test)

Aggregations

PermitQueue (org.webpieces.util.locking.PermitQueue)3 Test (org.junit.Test)2 XFuture (org.webpieces.util.futures.XFuture)2 FuturePermitQueue (org.webpieces.util.locking.FuturePermitQueue)2 StreamRef (com.webpieces.http2.api.streaming.StreamRef)1 HttpStream (org.webpieces.frontend2.api.HttpStream)1