new FlowController( builder .flowControlSettings
flowController.reserve( 1, outstandingMessage.receivedMessage().getMessage().getSerializedSize()); } catch (FlowController.MaxOutstandingElementCountReachedException
/** Stop extending deadlines for this message and free flow control. */ private void forget() { if (pendingMessages.remove(ackId) == null) { /* * We're forgetting the message for the second time. Probably because we ran out of total * expiration, forget the message, then the user finishes working on the message, and forget * again. Turn the second forget into a no-op so we don't free twice. */ return; } flowController.release(1, outstandingBytes); messagesWaiter.incrementPendingMessages(-1); processOutstandingBatches(); }
@Test public void testReserveRelease_noLimits_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }
@Test public void testRestoreAfterFail() throws FlowController.FlowControlException { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(2L) .setMaxOutstandingRequestBytes(1L) .setLimitExceededBehavior(LimitExceededBehavior.ThrowException) .build()); flowController.reserve(1, 1); try { flowController.reserve(1, 1); throw new IllegalStateException("flowController should not have any bytes left"); } catch (FlowController.MaxOutstandingRequestBytesReachedException e) { // Ignore. } flowController.reserve(1, 0); }
private void testRejectedReserveRelease( FlowController flowController, int maxElementCount, int maxNumBytes, Class<? extends FlowController.FlowControlException> expectedException) throws FlowController.FlowControlException { flowController.reserve(1, 1); try { flowController.reserve(maxElementCount, maxNumBytes); fail("Should thrown a FlowController.FlowControlException"); } catch (FlowController.FlowControlException e) { assertTrue(expectedException.isInstance(e)); } flowController.release(1, 1); flowController.reserve(maxElementCount, maxNumBytes); } }
@Test public void testReserveRelease_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(10L) .setMaxOutstandingRequestBytes(10L) .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }
@Test public void testInvalidArguments() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(10L) .setMaxOutstandingRequestBytes(10L) .setLimitExceededBehavior(LimitExceededBehavior.Block) .build()); flowController.reserve(0, 0); try { flowController.reserve(-1, 1); fail("Must have thrown an illegal argument error"); } catch (IllegalArgumentException expected) { // Expected } try { flowController.reserve(1, -1); fail("Must have thrown an illegal argument error"); } catch (IllegalArgumentException expected) { // Expected } }
private static void testBlockingReserveRelease( final FlowController flowController, final int maxElementCount, final int maxNumBytes) throws Exception { flowController.reserve(1, 1); final SettableFuture<?> permitsReserved = SettableFuture.create(); Future<?> finished = Executors.newCachedThreadPool() .submit( new Runnable() { @Override public void run() { try { permitsReserved.set(null); flowController.reserve(maxElementCount, maxNumBytes); } catch (FlowController.FlowControlException e) { throw new AssertionError(e); } } }); permitsReserved.get(); flowController.release(1, 1); finished.get(); }
@Test public void testReserveRelease_ignore_ok() throws Exception { FlowController flowController = new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(1L) .setMaxOutstandingRequestBytes(1L) .setLimitExceededBehavior(LimitExceededBehavior.Ignore) .build()); flowController.reserve(1, 1); flowController.release(1, 1); }
new FlowController( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(1L)
@Test public void testExtension_GiveUp() throws Exception { dispatcher.processReceivedMessages(Collections.singletonList(TEST_MESSAGE), NOOP_RUNNABLE); dispatcher.extendDeadlines(); assertThat(sentModAcks) .contains(ModAckItem.of(TEST_MESSAGE.getAckId(), Subscriber.MIN_ACK_DEADLINE_SECONDS)); sentModAcks.clear(); // If we run extendDeadlines after totalExpiration, we shouldn't send anything. // In particular, don't send negative modacks. clock.advance(1, TimeUnit.DAYS); dispatcher.extendDeadlines(); assertThat(sentModAcks).isEmpty(); // We should be able to reserve another item in the flow controller and not block shutdown flowController.reserve(1, 0); dispatcher.stop(); }
@Override public void release(long elements, long bytes) { super.release(elements, bytes); this.elementsReleased += elements; this.bytesReleased += bytes; this.callsToRelease += 1; }
private BatchingCallSettings(Builder<RequestT, ResponseT> builder) { super(builder); this.batchingDescriptor = builder.batchingDescriptor; this.batchingSettings = Preconditions.checkNotNull(builder.batchingSettings); FlowController flowControllerToUse = builder.flowController; if (flowControllerToUse == null) { flowControllerToUse = new FlowController(batchingSettings.getFlowControlSettings()); } this.flowController = flowControllerToUse; }
@Override public void reserve(long elements, long bytes) throws FlowControlException { super.reserve(elements, bytes); this.elementsReserved += elements; this.bytesReserved += bytes; this.callsToReserve += 1; }
public void release(T batch) { Preconditions.checkNotNull(batch); int elements = Ints.checkedCast(elementCounter.count(batch)); int bytes = Ints.checkedCast(byteCounter.count(batch)); flowController.release(elements, bytes); } }
private BatchingCallSettings(Builder<RequestT, ResponseT> builder) { super(builder); this.batchingDescriptor = builder.batchingDescriptor; this.batchingSettings = Preconditions.checkNotNull(builder.batchingSettings); FlowController flowControllerToUse = builder.flowController; if (flowControllerToUse == null) { flowControllerToUse = new FlowController(batchingSettings.getFlowControlSettings()); } this.flowController = flowControllerToUse; }
public void reserve(T batch) throws FlowControlException { Preconditions.checkNotNull(batch); int elements = Ints.checkedCast(elementCounter.count(batch)); int bytes = Ints.checkedCast(byteCounter.count(batch)); flowController.reserve(elements, bytes); }
public void release(T batch) { Preconditions.checkNotNull(batch); int elements = Ints.checkedCast(elementCounter.count(batch)); int bytes = Ints.checkedCast(byteCounter.count(batch)); flowController.release(elements, bytes); } }
private static FlowController getDisabledFlowController() { return new FlowController( FlowControlSettings.newBuilder() .setLimitExceededBehavior(LimitExceededBehavior.Ignore) .build()); }