Make most outbound operations are cancellable

- Inspired by #2214
- It actually reduces the chance of trying to marking a cancelled promise as success again, which raises an IllegalStateException.
This commit is contained in:
Trustin Lee 2014-02-10 14:04:47 -08:00
parent c7087c6842
commit a34d5baad2
3 changed files with 12 additions and 9 deletions

View File

@ -134,7 +134,7 @@ public class RxtxChannel extends OioByteStreamChannel {
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress localAddress, final ChannelPromise promise) {
if (!ensureOpen(promise)) { if (!promise.setUncancellable() || !ensureOpen(promise)) {
return; return;
} }

View File

@ -418,7 +418,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
try { try {
// check if the channel is still open as it could be closed in the mean time when the register // check if the channel is still open as it could be closed in the mean time when the register
// call was outside of the eventLoop // call was outside of the eventLoop
if (!ensureOpen(promise)) { if (!promise.setUncancellable() || !ensureOpen(promise)) {
return; return;
} }
doRegister(); doRegister();
@ -442,7 +442,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) { public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
if (!ensureOpen(promise)) { if (!promise.setUncancellable() || !ensureOpen(promise)) {
return; return;
} }
@ -480,6 +480,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void disconnect(final ChannelPromise promise) { public final void disconnect(final ChannelPromise promise) {
if (!promise.setUncancellable()) {
return;
}
boolean wasActive = isActive(); boolean wasActive = isActive();
try { try {
doDisconnect(); doDisconnect();
@ -502,6 +506,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void close(final ChannelPromise promise) { public final void close(final ChannelPromise promise) {
if (!promise.setUncancellable()) {
return;
}
if (inFlush0) { if (inFlush0) {
invokeLater(new Runnable() { invokeLater(new Runnable() {
@Override @Override

View File

@ -55,12 +55,7 @@ public abstract class AbstractOioChannel extends AbstractChannel {
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress localAddress, final ChannelPromise promise) {
if (!ensureOpen(promise)) { if (!promise.setUncancellable() || !ensureOpen(promise)) {
return;
}
if (!promise.setUncancellable()) {
close(voidPromise());
return; return;
} }