public void reserve(long elements, long bytes) throws FlowControlException { Preconditions.checkArgument(elements >= 0); Preconditions.checkArgument(bytes >= 0); if (outstandingElementCount != null) { if (!outstandingElementCount.acquire(elements)) { throw new MaxOutstandingElementCountReachedException(maxOutstandingElementCount); } } // Will always allow to send a request even if it is larger than the flow control limit, // if it doesn't then it will deadlock the thread. if (outstandingByteCount != null) { long permitsToDraw = Math.min(bytes, maxOutstandingRequestBytes); if (!outstandingByteCount.acquire(permitsToDraw)) { if (outstandingElementCount != null) { outstandingElementCount.release(elements); } throw new MaxOutstandingRequestBytesReachedException(maxOutstandingRequestBytes); } } }
@Override public void run() { semaphore.acquire(1); } };
public void release(long elements, long bytes) { Preconditions.checkArgument(elements >= 0); Preconditions.checkArgument(bytes >= 0); if (outstandingElementCount != null) { outstandingElementCount.release(elements); } if (outstandingByteCount != null) { // Need to return at most as much bytes as it can be drawn. long permitsToReturn = Math.min(bytes, maxOutstandingRequestBytes); outstandingByteCount.release(permitsToReturn); } } }
@Test(expected = IllegalArgumentException.class) public void testNegative() { Semaphore64 semaphore = new BlockingSemaphore(1); semaphore.acquire(-1); }
public void release(long elements, long bytes) { Preconditions.checkArgument(elements >= 0); Preconditions.checkArgument(bytes >= 0); if (outstandingElementCount != null) { outstandingElementCount.release(elements); } if (outstandingByteCount != null) { // Need to return at most as much bytes as it can be drawn. long permitsToReturn = Math.min(bytes, maxOutstandingRequestBytes); outstandingByteCount.release(permitsToReturn); } } }
public void reserve(long elements, long bytes) throws FlowControlException { Preconditions.checkArgument(elements >= 0); Preconditions.checkArgument(bytes >= 0); if (outstandingElementCount != null) { if (!outstandingElementCount.acquire(elements)) { throw new MaxOutstandingElementCountReachedException(maxOutstandingElementCount); } } // Will always allow to send a request even if it is larger than the flow control limit, // if it doesn't then it will deadlock the thread. if (outstandingByteCount != null) { long permitsToDraw = Math.min(bytes, maxOutstandingRequestBytes); if (!outstandingByteCount.acquire(permitsToDraw)) { if (outstandingElementCount != null) { outstandingElementCount.release(elements); } throw new MaxOutstandingRequestBytesReachedException(maxOutstandingRequestBytes); } } }