Merge pull request #34 from normanmaurer/3.2

Only release FileRegion if configured to do so. See NETTY-440
This commit is contained in:
Norman Maurer 2011-10-22 11:31:27 -07:00
commit dbccc9e52b
3 changed files with 32 additions and 4 deletions

View File

@ -14,21 +14,29 @@ public class DefaultFileRegion implements FileRegion {
private final FileChannel file;
private final long position;
private final long count;
private boolean releaseAfterTransfer;
public DefaultFileRegion(FileChannel file, long position, long count) {
public DefaultFileRegion(FileChannel file, long position, long count, boolean releaseAfterTransfer) {
this.file = file;
this.position = position;
this.count = count;
this.releaseAfterTransfer = releaseAfterTransfer;
}
public DefaultFileRegion(FileChannel file, long position, long count) {
this(file, position, count, false);
}
public long getPosition() {
return position;
}
public long getCount() {
return count;
}
public long transferTo(WritableByteChannel target, long position) throws IOException {
long count = this.count - position;
if (count < 0 || position < 0) {
@ -43,6 +51,7 @@ public class DefaultFileRegion implements FileRegion {
return file.transferTo(this.position + position, count, target);
}
public void releaseExternalResources() {
try {
file.close();
@ -50,4 +59,13 @@ public class DefaultFileRegion implements FileRegion {
logger.warn("Failed to close a file.", e);
}
}
public boolean releaseAfterTransfer() {
return releaseAfterTransfer;
}
public void setReleaseAfterTransfer(boolean releaseAfterTransfer) {
this.releaseAfterTransfer = releaseAfterTransfer;
}
}

View File

@ -23,6 +23,7 @@ import java.nio.channels.DatagramChannel;
import java.nio.channels.WritableByteChannel;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.DefaultFileRegion;
import org.jboss.netty.channel.FileRegion;
/**
@ -288,8 +289,12 @@ final class SocketSendBufferPool {
}
public void release() {
// Make sure the FileRegion resource are released otherwise it may cause a FD leak or something similar
file.releaseExternalResources();
if (file instanceof DefaultFileRegion) {
if (((DefaultFileRegion)file).releaseAfterTransfer()) {
// Make sure the FileRegion resource are released otherwise it may cause a FD leak or something similar
file.releaseExternalResources();
}
}
}
}

View File

@ -28,6 +28,7 @@ import java.util.regex.Pattern;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.DefaultFileRegion;
import org.jboss.netty.channel.FileRegion;
/**
@ -141,7 +142,11 @@ class OioWorker implements Runnable {
}
}
} finally {
fr.releaseExternalResources();
if (fr instanceof DefaultFileRegion) {
if (((DefaultFileRegion)fr).releaseAfterTransfer()) {
fr.releaseExternalResources();
}
}
}
} else {