Add asserts so users will see errors when try to use methods from outside the EventLoop.

Motivation:

We should guard users from using Unsafe methods from outside the EventLoop if not designed to do so.

Modifications:

Add asserts

Result:

Easier for users to detect miss-use.
This commit is contained in:
Norman Maurer 2016-03-03 14:23:09 +01:00
parent 27c68a10ec
commit a47c03c3d7

View File

@ -374,6 +374,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
/** true if the channel has never been registered, false otherwise */
private boolean neverRegistered = true;
private void assertEventLoop() {
assert !registered || eventLoop.inEventLoop();
}
@Override
public final ChannelOutboundBuffer outboundBuffer() {
return outboundBuffer;
@ -470,6 +474,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
assertEventLoop();
if (!promise.setUncancellable() || !ensureOpen(promise)) {
return;
}
@ -510,6 +516,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void disconnect(final ChannelPromise promise) {
assertEventLoop();
if (!promise.setUncancellable()) {
return;
}
@ -538,6 +546,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void close(final ChannelPromise promise) {
assertEventLoop();
close(promise, CLOSED_CHANNEL_EXCEPTION, false);
}
@ -630,6 +640,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void closeForcibly() {
assertEventLoop();
try {
doClose();
} catch (Exception e) {
@ -639,7 +651,9 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void deregister(final ChannelPromise promise) {
deregister(promise, false);
assertEventLoop();
deregister(promise, false);
}
private void deregister(final ChannelPromise promise, final boolean fireChannelInactive) {
@ -688,6 +702,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void beginRead() {
assertEventLoop();
if (!isActive()) {
return;
}
@ -707,6 +723,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void write(Object msg, ChannelPromise promise) {
assertEventLoop();
ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
if (outboundBuffer == null) {
// If the outboundBuffer is null we know the channel was closed and so
@ -737,6 +755,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void flush() {
assertEventLoop();
ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
if (outboundBuffer == null) {
return;
@ -797,6 +817,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final ChannelPromise voidPromise() {
assertEventLoop();
return unsafeVoidPromise;
}