Add AUTO_CLOSE option

- Fixes #1952
- If AUTO_CLOSE is turned on, Netty will close the channel immediately and automatically on write failure.  The default is false.
This commit is contained in:
Trustin Lee 2013-11-05 17:23:22 +09:00
parent 77b4ec7e1b
commit c7b66545b4
23 changed files with 119 additions and 3 deletions

View File

@ -234,6 +234,12 @@ final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements Rxt
return this;
}
@Override
public RxtxChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -288,6 +288,9 @@ public interface RxtxChannelConfig extends ChannelConfig {
@Override
RxtxChannelConfig setAutoRead(boolean autoRead);
@Override
RxtxChannelConfig setAutoClose(boolean autoClose);
@Override
RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

View File

@ -208,6 +208,12 @@ public class DefaultSctpChannelConfig extends DefaultChannelConfig implements Sc
return this;
}
@Override
public SctpChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -192,6 +192,12 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
return this;
}
@Override
public SctpServerChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public SctpServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);

View File

@ -115,6 +115,9 @@ public interface SctpChannelConfig extends ChannelConfig {
@Override
SctpChannelConfig setAutoRead(boolean autoRead);
@Override
SctpChannelConfig setAutoClose(boolean autoClose);
@Override
SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

View File

@ -110,6 +110,9 @@ public interface SctpServerChannelConfig extends ChannelConfig {
@Override
SctpServerChannelConfig setAutoRead(boolean autoRead);
@Override
SctpServerChannelConfig setAutoClose(boolean autoClose);
@Override
SctpServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

View File

@ -269,6 +269,12 @@ public class DefaultUdtChannelConfig extends DefaultChannelConfig implements
return this;
}
@Override
public UdtChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public UdtChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);

View File

@ -172,6 +172,12 @@ public class DefaultUdtServerChannelConfig extends DefaultUdtChannelConfig
return this;
}
@Override
public UdtServerChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public UdtServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);

View File

@ -130,6 +130,9 @@ public interface UdtChannelConfig extends ChannelConfig {
@Override
UdtChannelConfig setAutoRead(boolean autoRead);
@Override
UdtChannelConfig setAutoClose(boolean autoClose);
@Override
UdtChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

View File

@ -64,6 +64,9 @@ public interface UdtServerChannelConfig extends UdtChannelConfig {
@Override
UdtServerChannelConfig setAutoRead(boolean autoRead);
@Override
UdtServerChannelConfig setAutoClose(boolean autoClose);
@Override
UdtServerChannelConfig setProtocolReceiveBufferSize(int size);

View File

@ -682,7 +682,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
doWrite(outboundBuffer);
} catch (Throwable t) {
outboundBuffer.failFlushed(t);
if (t instanceof IOException) {
if (t instanceof IOException && config().isAutoClose()) {
close(voidPromise());
}
} finally {

View File

@ -188,6 +188,18 @@ public interface ChannelConfig {
*/
ChannelConfig setAutoRead(boolean autoRead);
/**
* Returns {@code true} if and only if the {@link Channel} will be closed automatically and immediately on
* write failure. The default is {@code false}.
*/
boolean isAutoClose();
/**
* Sets whether the {@link Channel} should be closed automatically and immediately on write faillure.
* The default is {@code false}.
*/
ChannelConfig setAutoClose(boolean autoClose);
/**
* Returns the high water mark of the write buffer. If the number of bytes
* queued in the write buffer exceeds this value, {@link Channel#isWritable()}

View File

@ -49,6 +49,12 @@ public class ChannelOption<T> extends UniqueName {
public static final ChannelOption<Boolean> ALLOW_HALF_CLOSURE = valueOf("ALLOW_HALF_CLOSURE");
public static final ChannelOption<Boolean> AUTO_READ = valueOf("AUTO_READ");
/**
* {@code true} if and only if the {@link Channel} is closed automatically and immediately on write failure.
* The default is {@code false}.
*/
public static final ChannelOption<Boolean> AUTO_CLOSE = valueOf("AUTO_CLOSE");
public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");

View File

@ -46,6 +46,7 @@ public class DefaultChannelConfig implements ChannelConfig {
private volatile int maxMessagesPerRead;
private volatile int writeSpinCount = 16;
private volatile boolean autoRead = true;
private volatile boolean autoClose;
private volatile int writeBufferHighWaterMark = 64 * 1024;
private volatile int writeBufferLowWaterMark = 32 * 1024;
@ -68,7 +69,7 @@ public class DefaultChannelConfig implements ChannelConfig {
return getOptions(
null,
CONNECT_TIMEOUT_MILLIS, MAX_MESSAGES_PER_READ, WRITE_SPIN_COUNT,
ALLOCATOR, AUTO_READ, RCVBUF_ALLOCATOR, WRITE_BUFFER_HIGH_WATER_MARK,
ALLOCATOR, AUTO_READ, AUTO_CLOSE, RCVBUF_ALLOCATOR, WRITE_BUFFER_HIGH_WATER_MARK,
WRITE_BUFFER_LOW_WATER_MARK, MESSAGE_SIZE_ESTIMATOR);
}
@ -125,6 +126,9 @@ public class DefaultChannelConfig implements ChannelConfig {
if (option == AUTO_READ) {
return (T) Boolean.valueOf(isAutoRead());
}
if (option == AUTO_CLOSE) {
return (T) Boolean.valueOf(isAutoClose());
}
if (option == WRITE_BUFFER_HIGH_WATER_MARK) {
return (T) Integer.valueOf(getWriteBufferHighWaterMark());
}
@ -153,6 +157,8 @@ public class DefaultChannelConfig implements ChannelConfig {
setRecvByteBufAllocator((RecvByteBufAllocator) value);
} else if (option == AUTO_READ) {
setAutoRead((Boolean) value);
} else if (option == AUTO_CLOSE) {
setAutoClose((Boolean) value);
} else if (option == WRITE_BUFFER_HIGH_WATER_MARK) {
setWriteBufferHighWaterMark((Integer) value);
} else if (option == WRITE_BUFFER_LOW_WATER_MARK) {
@ -260,6 +266,17 @@ public class DefaultChannelConfig implements ChannelConfig {
return this;
}
@Override
public boolean isAutoClose() {
return autoClose;
}
@Override
public ChannelConfig setAutoClose(boolean autoClose) {
this.autoClose = autoClose;
return this;
}
@Override
public int getWriteBufferHighWaterMark() {
return writeBufferHighWaterMark;

View File

@ -86,6 +86,7 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
}
}
pipeline.fireChannelReadComplete();
pipeline.fireExceptionCaught(cause);
if (close || cause instanceof IOException) {
closeOnRead(pipeline);
}

View File

@ -174,6 +174,9 @@ public interface DatagramChannelConfig extends ChannelConfig {
@Override
DatagramChannelConfig setAutoRead(boolean autoRead);
@Override
DatagramChannelConfig setAutoClose(boolean autoClose);
@Override
DatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
}

View File

@ -395,6 +395,12 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
return this;
}
@Override
public DatagramChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public DatagramChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -315,6 +315,12 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
return this;
}
@Override
public SocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -175,6 +175,9 @@ public interface SocketChannelConfig extends ChannelConfig {
@Override
SocketChannelConfig setAutoRead(boolean autoRead);
@Override
SocketChannelConfig setAutoClose(boolean autoClose);
@Override
SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
}

View File

@ -145,6 +145,12 @@ public class DefaultOioServerSocketChannelConfig extends DefaultServerSocketChan
return this;
}
@Override
public OioServerSocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public OioServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -22,7 +22,6 @@ import io.netty.channel.MessageSizeEstimator;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.socket.DefaultSocketChannelConfig;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.SocketChannelConfig;
import java.io.IOException;
import java.net.Socket;
@ -174,6 +173,12 @@ public class DefaultOioSocketChannelConfig extends DefaultSocketChannelConfig im
return this;
}
@Override
public OioSocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}
@Override
public OioSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);

View File

@ -81,6 +81,9 @@ public interface OioServerSocketChannelConfig extends ServerSocketChannelConfig
@Override
OioServerSocketChannelConfig setAutoRead(boolean autoRead);
@Override
OioServerSocketChannelConfig setAutoClose(boolean autoClose);
@Override
OioServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

View File

@ -96,6 +96,9 @@ public interface OioSocketChannelConfig extends SocketChannelConfig {
@Override
OioSocketChannelConfig setAutoRead(boolean autoRead);
@Override
OioSocketChannelConfig setAutoClose(boolean autoClose);
@Override
OioSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);