Have FlowState.cancel take a Throwable and code cleanup.

Motivation:

- In FlowState.write(...) we are currently swalloing an exception.
- In my previous commit I introduced a compiler warning by not making
  a local variabe final.

Modifications:

- Have FlowState.cancel() take a Throwable.
- Make the variable final.

Result:

No more swallowed exceptions and warnings.
This commit is contained in:
Jakob Buchgraber 2015-03-10 13:57:13 -07:00 committed by Scott Mitchell
parent 9bad408de5
commit 8e04d706de
3 changed files with 17 additions and 6 deletions

View File

@ -174,7 +174,7 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
} }
// Save the context. We'll use this later when we write pending bytes. // Save the context. We'll use this later when we write pending bytes.
this.ctx = ctx; this.ctx = ctx;
FlowState state; final FlowState state;
try { try {
state = state(stream); state = state(stream);
state.enqueueFrame(frame); state.enqueueFrame(frame);
@ -449,6 +449,15 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
* Clears the pending queue and writes errors for each remaining frame. * Clears the pending queue and writes errors for each remaining frame.
*/ */
void cancel() { void cancel() {
cancel(null);
}
/**
* Clears the pending queue and writes errors for each remaining frame.
*
* @param cause the {@link Throwable} that caused this method to be invoked.
*/
void cancel(Throwable cause) {
cancelled = true; cancelled = true;
// Ensure that the queue can't be modified while // Ensure that the queue can't be modified while
// we are writing. // we are writing.
@ -460,7 +469,7 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
if (frame == null) { if (frame == null) {
break; break;
} }
writeError(frame, streamError(stream.id(), INTERNAL_ERROR, writeError(frame, streamError(stream.id(), INTERNAL_ERROR, cause,
"Stream closed before write could take place")); "Stream closed before write could take place"));
} }
} }
@ -492,6 +501,9 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
int write(FlowControlled frame, int allowedBytes) { int write(FlowControlled frame, int allowedBytes) {
int before = frame.size(); int before = frame.size();
int writtenBytes = 0; int writtenBytes = 0;
// In case an exception is thrown we want to
// remember it and pass it to cancel(Throwable).
Throwable cause = null;
try { try {
assert !writing; assert !writing;
@ -506,10 +518,11 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
pendingWriteQueue.remove(); pendingWriteQueue.remove();
frame.writeComplete(); frame.writeComplete();
} }
} catch (Throwable e) { } catch (Throwable t) {
// Mark the state as cancelled, we'll clear the pending queue // Mark the state as cancelled, we'll clear the pending queue
// via cancel() below. // via cancel() below.
cancelled = true; cancelled = true;
cause = t;
} finally { } finally {
writing = false; writing = false;
// Make sure we always decrement the flow control windows // Make sure we always decrement the flow control windows
@ -520,7 +533,7 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
// If a cancellation occurred while writing, call cancel again to // If a cancellation occurred while writing, call cancel again to
// clear and error all of the pending writes. // clear and error all of the pending writes.
if (cancelled) { if (cancelled) {
cancel(); cancel(cause);
} }
} }
return writtenBytes; return writtenBytes;

View File

@ -82,7 +82,6 @@ public interface Http2RemoteFlowController extends Http2FlowController {
* </p> * </p>
* *
* @param allowedBytes an upper bound on the number of bytes the payload can write at this time. * @param allowedBytes an upper bound on the number of bytes the payload can write at this time.
* @throws Exception if an error occurs. The method must not call {@link #error(Throwable)} by itself.
* @return {@code true} if a flush is required, {@code false} otherwise. * @return {@code true} if a flush is required, {@code false} otherwise.
*/ */
boolean write(int allowedBytes); boolean write(int allowedBytes);

View File

@ -35,7 +35,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http2.Http2FrameWriter.Configuration; import io.netty.handler.codec.http2.Http2FrameWriter.Configuration;
import io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled;
import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap; import io.netty.util.collection.IntObjectMap;