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

Consider adding a timeout when submiting a task #129

Open
aalmiray opened this issue Oct 23, 2017 · 2 comments
Open

Consider adding a timeout when submiting a task #129

aalmiray opened this issue Oct 23, 2017 · 2 comments

Comments

@aalmiray
Copy link
Contributor

aalmiray commented Oct 23, 2017

Take the following example

Promise<List<Person>, Throwable, Void> promise = deferredManager.when(() -> {
    HttpClient client = ...
    Response response = client.get(...);
    return response.body();
});

This task could take a long time to resolve its value. One could argue thew task may have it's own timeout mechanism (if the HttpClient supports it), but what if it doesn't? What if the underlying task has no explicit means to issue a timeout? Consider this alternative

deferredManager.when(2, TimeUnit.SECONDS, () -> ...);

Or even

deferredManager.when(Duration.of(2, TimeUnit.SECONDS), () -> ...);

The advantage being that the amount and the time unit are contained between an immutable pair. This will require a custom Duration class.

@aalmiray
Copy link
Contributor Author

Perhaps the builder pattern may be applied to avoid an explosion of methods in DeferredManager (which already contains quite a good number due to type safety concerns). What if there were a type (better name pending) with the following API:

promiseScheduler.withDelay(Duration.of(2, TimeUnit.SECONDS))
                .withTimeout(Duration.of(10, SECONDS))
                .when(r1, r2, ...);

This means PromiseScheduler keeps track of state internally, and when the time is right (no pun intended) it schedules the tasks/runnables/etc using a DeferredManager impl that relies on a ScheduledExecutorService (because this type can schedule tasks with a delay). The timeout feature could be implemented with a CountDownLatch for example.

@aalmiray
Copy link
Contributor Author

Btw, JDK9 added delay and timeout to CompletableFuture.

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

No branches or pull requests

1 participant