Skip to content

Commit

Permalink
Merge pull request #98 from bugsnag/breadcrumbs-concurrent-exception
Browse files Browse the repository at this point in the history
Switch breadcrumb store to ConcurrentLinkedQueue
  • Loading branch information
kattrali committed Jan 25, 2016
2 parents 7f0a626 + 5150291 commit 7680a00
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/bugsnag/android/Breadcrumbs.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

class Breadcrumbs implements JsonStream.Streamable {
private static class Breadcrumb {
Expand All @@ -20,7 +20,7 @@ private static class Breadcrumb {
}

private static final int DEFAULT_MAX_SIZE = 20;
private final List<Breadcrumb> store = new LinkedList<Breadcrumb>();
private final Queue<Breadcrumb> store = new ConcurrentLinkedQueue<>();
private int maxSize = DEFAULT_MAX_SIZE;

public void toStream(@NonNull JsonStream writer) throws IOException {
Expand All @@ -38,9 +38,9 @@ public void toStream(@NonNull JsonStream writer) throws IOException {

void add(@NonNull String message) {
if (store.size() >= maxSize) {
store.remove(0);
// Remove oldest breadcrumb
store.poll();
}

store.add(new Breadcrumb(message));
}

Expand All @@ -52,7 +52,10 @@ void setSize(int size) {
if (size > store.size()) {
this.maxSize = size;
} else {
store.subList(0, store.size() - size).clear();
// Remove oldest breadcrumbs until reaching the required size
while (store.size() > size) {
store.poll();
}
}
}
}

0 comments on commit 7680a00

Please sign in to comment.