Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server.update(int timeout) returning after incorrect timeout. #164

Open
ent-moot opened this issue Jun 7, 2021 · 0 comments
Open

server.update(int timeout) returning after incorrect timeout. #164

ent-moot opened this issue Jun 7, 2021 · 0 comments

Comments

@ent-moot
Copy link

ent-moot commented Jun 7, 2021

There's a "bug" whereby the server.update() method might take longer to return than specified.

E.g. a call of server.update(10) may take 25ms to return.

This issue is caused because of the way in which a workaround has been implemented for an issue with NIO.

The fix is very straight-forward:

Replace this code, in Server.java:

// NIO freaks and returns immediately with 0 sometimes, so try to keep from hogging the CPU.
long elapsedTime = System.currentTimeMillis() - startTime;
try {
if (elapsedTime < 25) Thread.sleep(25 - elapsedTime);
} catch (InterruptedException ex) {
}

with :

// NIO freaks and returns immediately with 0 sometimes, so try to keep from hogging the CPU.
long elapsedTime = System.currentTimeMillis() - startTime;
try {
int targetDuration = Math.min(timeout,25);
if (elapsedTime < targetDuration) Thread.sleep(targetDuration - elapsedTime);
} catch (InterruptedException ex) {
}

This ensures that, even if NIO "freaks out", we:

  1. Still avoid hogging the CPU
  2. BUT also avoid sleeping longer than the specified timeout

I have built and tested this locally, and it works, but not sure how to go about submitting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant