Skip to content

Commit

Permalink
Fix #9169 refine idle timeout and failure (#10418)
Browse files Browse the repository at this point in the history
Only fail request callback if a failure has not been otherwise notified.
Slight optimisation for failing idle timeouts by avoiding double lock.
Always create a failure if failing the callback.
  • Loading branch information
gregw authored Aug 29, 2023
1 parent 5808a62 commit d6a0226
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ public Invocable.InvocationType getInvocationType()
@Override
public Runnable onIdleTimeout(TimeoutException t)
{
Predicate<TimeoutException> onIdleTimeout;
try (AutoLock ignored = _lock.lock())
{
if (LOG.isDebugEnabled())
Expand Down Expand Up @@ -380,29 +379,29 @@ public Runnable onIdleTimeout(TimeoutException t)
if (invokeOnContentAvailable != null || invokeWriteFailure != null)
return _serializedInvoker.offer(invokeOnContentAvailable, invokeWriteFailure);

// Otherwise We ask any idle timeout listeners if we should call onFailure or not
onIdleTimeout = _onIdleTimeout;
}
else
{
onIdleTimeout = null;
}
}
// otherwise, if there is an idle timeout listener, we ask if if we should call onFailure or not
Predicate<TimeoutException> onIdleTimeout = _onIdleTimeout;
if (onIdleTimeout != null)
{
return _serializedInvoker.offer(() ->
{
if (onIdleTimeout.test(t))
{
// If the idle timeout listener(s) return true, then we call onFailure and run any task it returns.
Runnable task = onFailure(t);
if (task != null)
task.run();
}
});
}

// Ask any listener what to do
if (onIdleTimeout != null)
{
Runnable onIdle = () ->
{
if (onIdleTimeout.test(t))
// otherwise, if there is no failure listener, then we can fail the callback directly without a double lock
if (_onFailure == null && _request != null)
{
// If the idle timeout listener(s) return true, then we call onFailure and any task it returns.
Runnable task = onFailure(t);
if (task != null)
task.run();
_failure = Content.Chunk.from(t, true);
return () -> _request._callback.failed(t);
}
};
return _serializedInvoker.offer(onIdle);
}
}

// otherwise treat as a failure
Expand Down Expand Up @@ -476,7 +475,7 @@ else if (ExceptionUtil.areNotAssociated(_failure.getFailure(), x) && _failure.ge
}

// If the application has not been otherwise informed of the failure
if (invokeOnContentAvailable == null && invokeWriteFailure == null)
if (invokeOnContentAvailable == null && invokeWriteFailure == null && onFailure == null)
{
if (LOG.isDebugEnabled())
LOG.debug("failing callback in {}", this, x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ public boolean handle(Request request, Response response, Callback callback)
request.addFailureListener(t -> error.set(null));
request.addFailureListener(t -> error.compareAndSet(null, t));
request.addFailureListener(t -> error.compareAndSet(null, new Throwable("WRONG")));
request.addFailureListener(callback::failed);
return true;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public boolean handle(Request request, Response response, Callback callback)
request.addFailureListener(t ->
{
assertThat(ContextHandler.getCurrentContext(), sameInstance(_context.getContext()));
callback.failed(t);
latch.countDown();
});
return true;
Expand Down

0 comments on commit d6a0226

Please sign in to comment.