[#1606] Reduce overhead during writes because of ChannelPromise validation

This commit is contained in:
Norman Maurer 2013-07-22 11:28:02 +02:00
parent 81612f8e9b
commit 25f96b1644
2 changed files with 29 additions and 4 deletions

View File

@ -812,17 +812,28 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return new FailedChannelFuture(channel(), executor(), cause); return new FailedChannelFuture(channel(), executor(), cause);
} }
private void validatePromise(ChannelFuture promise, boolean allowUnsafe) { private void validatePromise(ChannelPromise promise, boolean allowUnsafe) {
if (promise == null) { if (promise == null) {
throw new NullPointerException("promise"); throw new NullPointerException("promise");
} }
if (promise.isDone()) {
throw new IllegalArgumentException("promise already done: " + promise);
}
// check if the promise is of type DefaultChannelPromise and if so check if its validated already.
DefaultChannelPromise p = null;
if (promise instanceof DefaultChannelPromise) {
p = (DefaultChannelPromise) promise;
if (p.isValidated()) {
return;
}
}
if (promise.channel() != channel()) { if (promise.channel() != channel()) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
"promise.channel does not match: %s (expected: %s)", promise.channel(), channel())); "promise.channel does not match: %s (expected: %s)", promise.channel(), channel()));
} }
if (promise.isDone()) {
throw new IllegalArgumentException("promise already done: " + promise);
}
if (!allowUnsafe && promise instanceof VoidChannelPromise) { if (!allowUnsafe && promise instanceof VoidChannelPromise) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
StringUtil.simpleClassName(VoidChannelPromise.class) + " not allowed for this operation"); StringUtil.simpleClassName(VoidChannelPromise.class) + " not allowed for this operation");
@ -831,6 +842,11 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
throw new IllegalArgumentException( throw new IllegalArgumentException(
StringUtil.simpleClassName(AbstractChannel.CloseFuture.class) + " not allowed in a pipeline"); StringUtil.simpleClassName(AbstractChannel.CloseFuture.class) + " not allowed in a pipeline");
} }
if (p != null) {
// mark as validated
p.validated();
}
} }
private DefaultChannelHandlerContext findContextInbound() { private DefaultChannelHandlerContext findContextInbound() {

View File

@ -29,6 +29,7 @@ public class DefaultChannelPromise extends DefaultPromise<Void> implements Chann
private final Channel channel; private final Channel channel;
private long checkpoint; private long checkpoint;
private boolean validated;
/** /**
* Creates a new instance. * Creates a new instance.
@ -157,4 +158,12 @@ public class DefaultChannelPromise extends DefaultPromise<Void> implements Chann
super.checkDeadLock(); super.checkDeadLock();
} }
} }
final boolean isValidated() {
return validated;
}
final void validated() {
validated = true;
}
} }