-
Notifications
You must be signed in to change notification settings - Fork 86
squid:S2095 - Resources should be closed #198
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,7 @@ | |
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
@@ -31,11 +29,11 @@ | |
import java.util.TreeSet; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.addthis.basis.util.LessStreams; | ||
import com.addthis.basis.util.LessFiles; | ||
import com.addthis.basis.util.Parameter; | ||
|
||
import com.addthis.hydra.data.query.Query; | ||
import com.addthis.hydra.data.query.QueryException; | ||
import com.addthis.hydra.query.aggregate.BalancedAllocator; | ||
|
@@ -50,7 +48,6 @@ | |
import com.addthis.hydra.query.tracker.TrackerHandler; | ||
import com.addthis.meshy.MeshyServer; | ||
import com.addthis.meshy.service.file.FileReference; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Splitter; | ||
import com.google.common.base.Strings; | ||
|
@@ -246,10 +243,11 @@ protected void writeQuery(ChannelHandlerContext ctx, Query query, ChannelPromise | |
if (Strings.isNullOrEmpty(tasks)) { | ||
return Collections.emptySet(); | ||
} else { | ||
return LessStreams.stream(TASKS_SPLITTER.split(tasks)) | ||
.map(Ints::tryParse) | ||
.filter(i -> i != null) | ||
.collect(Collectors.toSet()); | ||
try(Stream<String> stream = LessStreams.stream(TASKS_SPLITTER.split(tasks))) { | ||
return stream.map(Ints::tryParse) | ||
.filter(i -> i != null) | ||
.collect(Collectors.toSet()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike Personally, I'm not a fan of the arbitrary "does close matter or not -- read the specifics each time and find out!" kind of API, but unless we want to wrap |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,12 @@ | |
|
||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.addthis.basis.kv.KVPairs; | ||
import com.addthis.basis.util.Parameter; | ||
|
||
import com.addthis.hydra.data.query.Query; | ||
import com.addthis.hydra.data.query.source.ErrorHandlingQuerySource; | ||
import com.addthis.hydra.data.query.source.QuerySource; | ||
|
@@ -40,7 +39,6 @@ | |
import io.netty.handler.codec.string.StringEncoder; | ||
import io.netty.util.CharsetUtil; | ||
import io.netty.util.concurrent.EventExecutor; | ||
|
||
import static com.addthis.hydra.query.web.HttpUtils.sendError; | ||
|
||
public final class HttpQueryCallHandler { | ||
|
@@ -65,8 +63,10 @@ public static ChannelFuture handleQuery(ChannelHandler queryToQueryResultsEncode | |
if ((dir != null) && !job.endsWith(dir)) { | ||
String[] jobs = job.split(","); | ||
String[] dirs = dir.split(","); | ||
job = Arrays.stream(jobs).flatMap(subJob -> Arrays.stream(dirs).map(subDir -> subJob + '/' + subDir)) | ||
try (Stream<String> stream = Arrays.stream(jobs)) { | ||
stream.flatMap(subJob -> Arrays.stream(dirs).map(subDir -> subJob + '/' + subDir)) | ||
.collect(Collectors.joining(",")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same deal as before. Strings to strings to strings. There's no reason to fret over "leaking" the stream any more than there is to to be concerned about |
||
} | ||
} | ||
String path = kv.getValue("path", kv.getValue("q", "")); | ||
Query query = new Query(job, new String[]{path}, new String[]{kv.getValue("ops"), kv.getValue("rops")}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,35 +179,32 @@ public void channelRead0( | |
|
||
log.trace("sending {}", file); | ||
|
||
FileChannel fileChannel; | ||
FileChannel fileChannel = null; | ||
try { | ||
fileChannel = FileChannel.open(file, StandardOpenOption.READ); | ||
} catch (IOException fnfe) { | ||
sendError(ctx, NOT_FOUND); | ||
return; | ||
} | ||
long fileLength = fileChannel.size(); | ||
long fileLength = fileChannel.size(); | ||
|
||
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); | ||
setContentLength(response, fileLength); | ||
setContentTypeHeader(response, file); | ||
try { | ||
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); | ||
setContentLength(response, fileLength); | ||
setContentTypeHeader(response, file); | ||
setDateAndCacheHeaders(response, file); | ||
if (isKeepAlive(request)) { | ||
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); | ||
} | ||
|
||
// Write the initial line and the header. | ||
ctx.write(response); | ||
|
||
// Write the content. | ||
ctx.write(new DefaultFileRegion(fileChannel, 0, fileLength)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Netty can be kind of a confusing beast, so I'm not surprised your tools got tripped up here. There's a couple issues, some rather significant:
|
||
} catch (IOException ioex) { | ||
fileChannel.close(); | ||
sendError(ctx, NOT_FOUND); | ||
return; | ||
} finally { | ||
if(fileChannel != null) { | ||
fileChannel.close(); | ||
} | ||
} | ||
if (isKeepAlive(request)) { | ||
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); | ||
} | ||
|
||
// Write the initial line and the header. | ||
ctx.write(response); | ||
|
||
// Write the content. | ||
ctx.write(new DefaultFileRegion(fileChannel, 0, fileLength)); | ||
|
||
// Write the end marker | ||
ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're returning a Stream here -- it is kind of important that we don't close it prematurely. The javadoc also notes that the caller is responsible for closing it, and most importantly, no (non-fatal) exception can occur that leaves the file stream open. The
IOException
can only occur during the initialFiles.list
, and if it does, then it handles that cleanup internally.