Skip to content

Commit

Permalink
Merge pull request #83 from mhart/fix-gelf-race-condition
Browse files Browse the repository at this point in the history
Fix nasty race condition when handling chunked GELF messages
  • Loading branch information
Lennart Koopmann committed Jun 18, 2012
2 parents 5aaf2b9 + c796649 commit ed502ae
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static synchronized ChunkedGELFClientManager getInstance() {
}

/**
* Add a chunk to it's message
* Add a chunk to its message
*
* @param chunk
* @return NULL if message is not yet complete, the complete ChunkedGELFMessage if this was the last missing chunk
Expand All @@ -60,23 +60,26 @@ public static synchronized ChunkedGELFClientManager getInstance() {
*/
public ChunkedGELFMessage insertChunk(GELFClientChunk chunk) throws ForeignGELFChunkException, InvalidGELFChunkException {
ChunkedGELFMessage fullMessage = messageMap.get(chunk.getHash());

if (fullMessage == null) {
// This is the first chunk of this message. Create a new message.
fullMessage = new ChunkedGELFMessage();
final ChunkedGELFMessage newMessage = new ChunkedGELFMessage();
fullMessage = messageMap.putIfAbsent(chunk.getHash(), newMessage);
if (fullMessage == null) {
fullMessage = newMessage;
}
}

// Add this chunk to the message.
fullMessage.insertChunk(chunk);

// Save the message with the new chunk.
messageMap.put(chunk.getHash(), fullMessage);
synchronized (fullMessage) {
// Add this chunk to the message.
fullMessage.insertChunk(chunk);

if (fullMessage.isComplete()) {
return fullMessage;
}
if (fullMessage.isComplete()) {
return fullMessage;
}

return null;
return null;
}
}

/**
Expand Down

0 comments on commit ed502ae

Please sign in to comment.