Skip to content

Commit

Permalink
Merge pull request #95 from benjchristensen/issue-53
Browse files Browse the repository at this point in the history
RequestLog: Reduce Chance of Memory Leak
  • Loading branch information
benjchristensen committed Feb 1, 2013
2 parents 5c173a8 + 2fbed3b commit 346c6a4
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
public class HystrixRequestLog {
private static final Logger logger = LoggerFactory.getLogger(HystrixRequestLog.class);

/**
* RequestLog: Reduce Chance of Memory Leak
* https://github.com/Netflix/Hystrix/issues/53
*
* Upper limit on RequestLog before ignoring further additions and logging warnings.
*
* Intended to help prevent memory leaks when someone isn't aware of the
* HystrixRequestContext lifecycle or enabling/disabling RequestLog.
*/
private static final int MAX_STORAGE = 1000;

private static final HystrixRequestVariableHolder<HystrixRequestLog> currentRequestLog = new HystrixRequestVariableHolder<HystrixRequestLog>(new HystrixRequestVariableLifecycle<HystrixRequestLog>() {
@Override
public HystrixRequestLog initialValue() {
Expand All @@ -58,7 +69,7 @@ public void shutdown(HystrixRequestLog value) {
/**
* History of {@link HystrixCommand} executed in this request.
*/
private LinkedBlockingQueue<HystrixCommand<?>> executedCommands = new LinkedBlockingQueue<HystrixCommand<?>>();
private LinkedBlockingQueue<HystrixCommand<?>> executedCommands = new LinkedBlockingQueue<HystrixCommand<?>>(MAX_STORAGE);

// prevent public instantiation
private HystrixRequestLog() {
Expand Down Expand Up @@ -101,7 +112,10 @@ public Collection<HystrixCommand<?>> getExecutedCommands() {
* {@code HystrixCommand<?>}
*/
/* package */void addExecutedCommand(HystrixCommand<?> command) {
executedCommands.add(command);
if (!executedCommands.offer(command)) {
// see RequestLog: Reduce Chance of Memory Leak https://github.com/Netflix/Hystrix/issues/53
logger.warn("RequestLog ignoring command after reaching limit of " + MAX_STORAGE + ". See https://github.com/Netflix/Hystrix/issues/53 for more information.");
}
}

/**
Expand Down Expand Up @@ -307,6 +321,24 @@ public void testMultipleCommands() {
}

}

@Test
public void testMaxLimit() {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
for (int i = 0; i < MAX_STORAGE; i++) {
new TestCommand("A", false, true).execute();
}
// then execute again some more
for (int i = 0; i < 10; i++) {
new TestCommand("A", false, true).execute();
}

assertEquals(MAX_STORAGE, HystrixRequestLog.getCurrentRequest().executedCommands.size());
} finally {
context.shutdown();
}
}
}

private static class TestCommand extends HystrixCommand<String> {
Expand Down

0 comments on commit 346c6a4

Please sign in to comment.