Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio servlet: don't change map while used by interation #801

Merged
merged 1 commit into from
May 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -143,16 +146,20 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}

private synchronized void removeTimedOutStreams() {
for (String streamId : multiTimeStreams.keySet()) {
if (streamTimeouts.get(streamId) < System.nanoTime()) {
// the stream has expired, we need to remove it!
FixedLengthAudioStream stream = multiTimeStreams.remove(streamId);
streamTimeouts.remove(streamId);
IOUtils.closeQuietly(stream);
stream = null;
logger.debug("Removed timed out stream {}", streamId);
// Build list of expired streams.
final List<String> toRemove = new LinkedList<>();
for (Entry<String, Long> entry : streamTimeouts.entrySet()) {
if (entry.getValue() < System.nanoTime()) {
toRemove.add(entry.getKey());
}
}
toRemove.forEach(streamId -> {
// the stream has expired, we need to remove it!
final FixedLengthAudioStream stream = multiTimeStreams.remove(streamId);
streamTimeouts.remove(streamId);
IOUtils.closeQuietly(stream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maggu2810 I think in this line you can find a piece of the puzzle to answer #532.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. While reading this implementation there are much more things that confused me. I answered to #532

logger.debug("Removed timed out stream {}", streamId);
});
}

@Override
Expand Down