Skip to content

Commit

Permalink
add MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <[email protected]>
  • Loading branch information
anthony-swirldslabs committed Feb 28, 2024
1 parent b690687 commit a835854
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ private synchronized void flipQueues() {
*/
private static final long AWAIT_FOR_REPORT_TIMEOUT_MILLIS = 100;

/**
* The maximum total time for the `while (!hasReported) { wait() }` loop.
* This is used to protect the teacher from a dying learner.
*/
private static final long MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS = 5000;

/**
* A queue of the nodes (by path) that we expect responses for.
*/
Expand Down Expand Up @@ -186,7 +192,10 @@ public Long getNextNodeToHandle() {
if (lastNodeAwaitingReporting != null) {
try {
synchronized (lastNodeAwaitingReporting) {
while (!hasLearnerReportedFor(lastNodeAwaitingReporting)) {
final long waitStartMillis = System.currentTimeMillis();
while (!hasLearnerReportedFor(lastNodeAwaitingReporting)
&& System.currentTimeMillis() - waitStartMillis
< MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS) {
lastNodeAwaitingReporting.wait(AWAIT_FOR_REPORT_TIMEOUT_MILLIS);
}
}
Expand Down

0 comments on commit a835854

Please sign in to comment.