Skip to content

Commit

Permalink
don't block when there are no buffers left
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin committed Jun 30, 2020
1 parent 2fb3f4c commit 5b6a772
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.InsufficientCapacityException;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import datadog.common.exec.DaemonThreadFactory;
Expand Down Expand Up @@ -46,7 +47,25 @@ public void close() {
}

long beginTransaction() {
return disruptor.getRingBuffer().next();
long backoffMillis = 1;
long nextLogTime = 0;
while (true) {
try {
return disruptor.getRingBuffer().tryNext();
} catch (InsufficientCapacityException insufficientCapacity) {
long now = System.currentTimeMillis();
backoffMillis = Math.min(backoffMillis * 2, 1000);
if (now > nextLogTime) { // log every 20 seconds
log.debug("no buffer available, sleeping for {}ms", backoffMillis);
nextLogTime = now + 20_000;
}
try {
Thread.sleep(backoffMillis);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
}
}
}

TraceBuffer getTraceBuffer(long sequence) {
Expand Down

0 comments on commit 5b6a772

Please sign in to comment.