Only release FileRegion after transfer was done if its configured todo

so. The default is false to be sure we are compatible with earlier 3.2
releases. This should be changed to default true in the master branch.
See NETTY-440
This commit is contained in:
Norman Maurer 2011-10-22 14:56:53 +02:00
parent 4b9b421af2
commit 2a4b91b59b
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 {