Skip to content

Commit

Permalink
Add re-entrant lock protection around adding message
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlb committed Feb 16, 2024
1 parent bfa3a76 commit 102fa3b
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,16 @@ private void handleNonFatal(Throwable ex) {
public void addMessage(LogMessage message) {
try {
messagesReceivedCounter.increment();
if (indexWriter.isPresent()) {
indexWriter.get().addDocument(documentBuilder.fromMessage(message));
} else {
LOG.error("IndexWriter should never be null when adding a message");
throw new IllegalStateException("IndexWriter should never be null when adding a message");
indexWriterLock.lock();
try {
if (indexWriter.isPresent()) {
indexWriter.get().addDocument(documentBuilder.fromMessage(message));
} else {
LOG.error("IndexWriter should never be null when adding a message");
throw new IllegalStateException("IndexWriter should never be null when adding a message");
}
} finally {
indexWriterLock.unlock();
}
} catch (FieldDefMismatchException | IllegalArgumentException e) {
LOG.error(String.format("Indexing message %s failed with error:", message), e);
Expand Down

0 comments on commit 102fa3b

Please sign in to comment.