public Task cleanup() { // The benchmarks showed that it's faster to iterate over all pending messages // than to keep a priority queue ordered by timeout. // The priority queue forces the application to pay the price of keeping the queue ordered // plus the synchronization cost of adding and removing elements. pendingResponseMap.values() .forEach(top -> { if (top.timeoutAt > runtime.clock().millis()) { // return the message, if there was a concurrent reception the message will be removed on the next cycle. return; } if (!top.isDone()) { final StringBuilder errorMsg = new StringBuilder(); errorMsg.append("Response timeout. "); errorMsg.append(top.message.toString()); top.internalCompleteExceptionally(new TimeoutException(errorMsg.toString())); } pendingResponseMap.remove(top.message.getMessageId()); }); return Task.done(); }
runtime.clock().millis() + getResponseTimeoutMillis(message) );