Skip to content

Commit

Permalink
Add AUTO_CLOSE option
Browse files Browse the repository at this point in the history
- Fixes netty#1952
- If AUTO_CLOSE is turned on, Netty will close the channel immediately and automatically on write failure.  The default is false.
  • Loading branch information
trustin committed Nov 5, 2013
1 parent c26d437 commit e2de807
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ public RxtxChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public RxtxChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ public static Paritybit valueOf(int value) {
@Override
RxtxChannelConfig setAutoRead(boolean autoRead);

@Override
RxtxChannelConfig setAutoClose(boolean autoClose);

@Override
RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ public SctpChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public SctpChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public SctpServerChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public SctpServerChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public SctpServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public interface SctpChannelConfig extends ChannelConfig {
@Override
SctpChannelConfig setAutoRead(boolean autoRead);

@Override
SctpChannelConfig setAutoClose(boolean autoClose);

@Override
SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public interface SctpServerChannelConfig extends ChannelConfig {
@Override
SctpServerChannelConfig setAutoRead(boolean autoRead);

@Override
SctpServerChannelConfig setAutoClose(boolean autoClose);

@Override
SctpServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ public UdtChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public UdtChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public UdtChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public UdtServerChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public UdtServerChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public UdtServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public interface UdtChannelConfig extends ChannelConfig {
@Override
UdtChannelConfig setAutoRead(boolean autoRead);

@Override
UdtChannelConfig setAutoClose(boolean autoClose);

@Override
UdtChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public interface UdtServerChannelConfig extends UdtChannelConfig {
@Override
UdtServerChannelConfig setAutoRead(boolean autoRead);

@Override
UdtServerChannelConfig setAutoClose(boolean autoClose);

@Override
UdtServerChannelConfig setProtocolReceiveBufferSize(int size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ protected void flush0() {
doWrite(outboundBuffer);
} catch (Throwable t) {
outboundBuffer.failFlushed(t);
if (t instanceof IOException) {
if (t instanceof IOException && config().isAutoClose()) {
close(voidPromise());
}
} finally {
Expand Down
12 changes: 12 additions & 0 deletions transport/src/main/java/io/netty/channel/ChannelConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
6 changes: 6 additions & 0 deletions transport/src/main/java/io/netty/channel/ChannelOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public static <T> ChannelOption<T> valueOf(Class<?> firstNameComponent, String s
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -68,7 +69,7 @@ public Map<ChannelOption<?>, Object> getOptions() {
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);
}

Expand Down Expand Up @@ -125,6 +126,9 @@ public <T> T getOption(ChannelOption<T> option) {
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());
}
Expand Down Expand Up @@ -153,6 +157,8 @@ public <T> boolean setOption(ChannelOption<T> option, T value) {
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) {
Expand Down Expand Up @@ -260,6 +266,17 @@ public ChannelConfig setAutoRead(boolean autoRead) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Thro
}
}
pipeline.fireChannelReadComplete();
pipeline.fireExceptionCaught(cause);
if (close || cause instanceof IOException) {
closeOnRead(pipeline);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public interface DatagramChannelConfig extends ChannelConfig {
@Override
DatagramChannelConfig setAutoRead(boolean autoRead);

@Override
DatagramChannelConfig setAutoClose(boolean autoClose);

@Override
DatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ public DatagramChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public DatagramChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public DatagramChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ public SocketChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public SocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public interface SocketChannelConfig extends ChannelConfig {
@Override
SocketChannelConfig setAutoRead(boolean autoRead);

@Override
SocketChannelConfig setAutoClose(boolean autoClose);

@Override
SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ public OioServerSocketChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public OioServerSocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public OioServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -174,6 +173,12 @@ public OioSocketChannelConfig setAutoRead(boolean autoRead) {
return this;
}

@Override
public OioSocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}

@Override
public OioSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public interface OioServerSocketChannelConfig extends ServerSocketChannelConfig
@Override
OioServerSocketChannelConfig setAutoRead(boolean autoRead);

@Override
OioServerSocketChannelConfig setAutoClose(boolean autoClose);

@Override
OioServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public interface OioSocketChannelConfig extends SocketChannelConfig {
@Override
OioSocketChannelConfig setAutoRead(boolean autoRead);

@Override
OioSocketChannelConfig setAutoClose(boolean autoClose);

@Override
OioSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

Expand Down

0 comments on commit e2de807

Please sign in to comment.