-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ain/java/rocks/inspectit/ocelot/core/config/propertysources/http/TaskTimeoutExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package rocks.inspectit.ocelot.core.config.propertysources.http; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Cancels internal tasks after a timeout has been exceeded. | ||
*/ | ||
@Slf4j | ||
public class TaskTimeoutExecutor { | ||
|
||
/** | ||
* The future to cancel the task by timeout. | ||
*/ | ||
private Future<?> timeoutExecutor; | ||
|
||
/** | ||
* Cancel the task after a specific timeout. This should prevent, that the thread stays deadlocked. | ||
* The task will be restarted after successful cancel. | ||
* | ||
* @param task the task to cancel by timeout | ||
* @param taskName the name of the task | ||
* @param restartTask the runnable to restart the task | ||
* @param timeout the time after which the task should be cancelled | ||
*/ | ||
public void scheduleCancelling(Future<?> task, String taskName, Runnable restartTask, Duration timeout) { | ||
ThreadFactory factory = new ThreadFactoryBuilder() | ||
.setNameFormat("timeout-" + taskName) | ||
.build(); | ||
ScheduledExecutorService cancelExecutor = Executors.newSingleThreadScheduledExecutor(factory); | ||
|
||
// Execute when timeout is reached | ||
Runnable cancelRunnable = () -> { | ||
task.cancel(true); | ||
boolean isCancelled = task.isCancelled(); | ||
log.warn("Cancelled {}: {}", taskName, isCancelled); | ||
if (isCancelled) { | ||
log.info("Restarting {}...", taskName); | ||
restartTask.run(); | ||
} | ||
}; | ||
|
||
// Schedule the cancelling just once | ||
log.debug("Scheduling {} timeout with: {}", taskName, timeout); | ||
timeoutExecutor = cancelExecutor.schedule(cancelRunnable, timeout.toMillis(), TimeUnit.MILLISECONDS); | ||
} | ||
|
||
/** | ||
* Cancel the timeout executor | ||
*/ | ||
public void cancelTimeout() { | ||
if(timeoutExecutor != null) timeoutExecutor.cancel(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.