-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use virtual threads for I/O bound operations
This commit introduces the use of virtual threads for short-lived I/O bound operations. More in details virtual threads are used in place of the usual thread pool for: - PairingInboundStore: use a virtual thread pool to creare a CompletableFuture to await the completation of a remote command - HttpClientFactory: use a virtual thread for HttpClient operations - TimedInputStream: use a virtual thread pool to read the target input stream within a fixed amount of time. Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
516cca5
commit 70db490
Showing
4 changed files
with
18 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
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 |
---|---|---|
|
@@ -19,14 +19,17 @@ | |
package io.seqera.wave.util | ||
|
||
import java.time.Duration | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Callable | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
import groovy.transform.CompileStatic | ||
import io.seqera.wave.core.RoutePath | ||
import io.seqera.wave.exception.UnexpectedReadException | ||
|
||
/** | ||
* An input stream filter that implements a timeout mechanism on the reading | ||
* operation over the target stream. | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
|
@@ -39,18 +42,21 @@ class TimedInputStream extends FilterInputStream { | |
|
||
private final RoutePath route | ||
|
||
private final ExecutorService executor | ||
|
||
private volatile boolean closed | ||
|
||
TimedInputStream(InputStream inputStream, Duration timeout, RoutePath route) { | ||
super(inputStream) | ||
this.target = inputStream | ||
this.timeoutMillis = (int)timeout.toMillis() | ||
this.route = route | ||
this.executor = Executors.newVirtualThreadPerTaskExecutor() | ||
} | ||
|
||
@Override | ||
int read() throws IOException { | ||
final result = CompletableFuture<Integer>.supplyAsync(() -> target.read()) | ||
final result = executor.submit((Callable<Integer>)(() -> target.read())) | ||
try { | ||
return result.get(timeoutMillis, TimeUnit.MILLISECONDS) | ||
} | ||
|
@@ -62,7 +68,7 @@ class TimedInputStream extends FilterInputStream { | |
|
||
@Override | ||
int read(byte[] b, int off, int len) throws IOException { | ||
final result = CompletableFuture<Integer>.supplyAsync(() -> target.read(b,off,len)) | ||
final result = executor.submit((Callable<Integer>)(() -> target.read(b,off,len))) | ||
try { | ||
return result.get(timeoutMillis, TimeUnit.MILLISECONDS) | ||
} | ||
|