Skip to content

Commit

Permalink
Refactored time-outs for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 6, 2017
1 parent 602375a commit 6aaa1e2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
Expand Up @@ -4,6 +4,7 @@
import org.rapidoid.commons.Err;
import org.rapidoid.concurrent.Future;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;

import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -67,7 +68,7 @@ public T get(long timeoutMs, long sleepingIntervalMs) throws TimeoutException {
long waitingSince = U.time();

while (!isDone()) {
if (U.time() - waitingSince > timeoutMs) {
if (Msc.timedOut(waitingSince, timeoutMs)) {
throw new TimeoutException();
}

Expand Down
2 changes: 1 addition & 1 deletion rapidoid-commons/src/main/java/org/rapidoid/io/Res.java
Expand Up @@ -163,7 +163,7 @@ public byte[] getBytesOrNull() {

protected void loadResource() {
// micro-caching the file content, expires after 500ms
if (U.time() - lastUpdatedOn >= 500) {
if (Msc.timedOut(lastUpdatedOn, 500)) {
boolean hasChanged;

synchronized (this) {
Expand Down
Expand Up @@ -432,7 +432,7 @@ public synchronized ProcessHandle terminate() {
while (isAlive()) {
U.sleep(1);

if (U.time() - t > terminationTimeout) {
if (Msc.timedOut(t, terminationTimeout)) {
destroyForcibly();
break;
}
Expand All @@ -442,7 +442,7 @@ public synchronized ProcessHandle terminate() {
while (isAlive()) {
U.sleep(1);

if (U.time() - t > terminationTimeout) {
if (Msc.timedOut(t, terminationTimeout)) {
throw U.rte("Couldn't terminate the process!");
}
}
Expand Down
3 changes: 3 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/util/Msc.java
Expand Up @@ -1351,4 +1351,7 @@ public static String urlWithProtocol(String url) {
}
}

public static boolean timedOut(long since, long timeout) {
return U.time() - since > timeout;
}
}
Expand Up @@ -28,6 +28,7 @@
import org.rapidoid.http.impl.HttpRoutesImpl;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;

import java.util.Date;

Expand Down Expand Up @@ -85,7 +86,7 @@ public boolean isEmpty() {

public boolean ready() {
long lastChangedAt = lastChangedAt().getTime();
return !isEmpty() && (U.time() - lastChangedAt > ROUTE_SETUP_WAITING_TIME_MS);
return !isEmpty() && Msc.timedOut(lastChangedAt, ROUTE_SETUP_WAITING_TIME_MS);
}

public void reset() {
Expand Down

0 comments on commit 6aaa1e2

Please sign in to comment.