-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add async API (2) #1258
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
Add async API (2) #1258
Changes from 4 commits
4c1b6b0
36a30f1
b662ac3
d8d093d
1c458d4
8ace19c
1190273
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.async; | ||
|
||
/** | ||
* See tests for usage (AsyncFunctionsTest). | ||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time | ||
*/ | ||
@FunctionalInterface | ||
public interface AsyncConsumer<T> extends AsyncFunction<T, Void> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.async; | ||
|
||
/** | ||
* See tests for usage (AsyncFunctionsTest). | ||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time | ||
*/ | ||
@FunctionalInterface | ||
public interface AsyncFunction<T, R> { | ||
/** | ||
* This should not be called externally, but should be implemented as a | ||
* lambda. To "finish" an async chain, use one of the "finish" methods. | ||
*/ | ||
void unsafeFinish(T value, SingleResultCallback<R> callback); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.async; | ||
|
||
import com.mongodb.internal.async.function.RetryState; | ||
import com.mongodb.internal.async.function.RetryingAsyncCallbackSupplier; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* See tests for usage (AsyncFunctionsTest). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer that usage documentation is placed in the code rather than the test. It will be easier for maintainers to refer to it if it's here. And that's where all of our existing internal Javadoc is, so it will be less surprising. Of all the new interfaces, this seems like the best place to put it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved; I find that I often look at the test code (more often than the docs) to confirm that I am using the API correctly. |
||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time | ||
*/ | ||
@FunctionalInterface | ||
public interface AsyncRunnable extends AsyncSupplier<Void>, AsyncConsumer<Void> { | ||
|
||
static AsyncRunnable beginAsync() { | ||
return (c) -> c.complete(c); | ||
} | ||
|
||
/** | ||
* Must be invoked at end of async chain | ||
* @param runnable the sync code to invoke (under non-exceptional flow) | ||
* prior to the callback | ||
* @param callback the callback provided by the method the chain is used in | ||
*/ | ||
default void thenRunAndFinish(final Runnable runnable, final SingleResultCallback<Void> callback) { | ||
this.finish((r, e) -> { | ||
if (e != null) { | ||
callback.completeExceptionally(e); | ||
return; | ||
} | ||
try { | ||
runnable.run(); | ||
} catch (Throwable t) { | ||
callback.completeExceptionally(t); | ||
return; | ||
} | ||
callback.complete(callback); | ||
}); | ||
} | ||
|
||
/** | ||
* See {@link #thenRunAndFinish(Runnable, SingleResultCallback)}, but the runnable | ||
* will always be executed, including on the exceptional path. | ||
* @param runnable the runnable | ||
* @param callback the callback | ||
*/ | ||
default void thenAlwaysRunAndFinish(final Runnable runnable, final SingleResultCallback<Void> callback) { | ||
this.finish((r, e) -> { | ||
try { | ||
runnable.run(); | ||
} catch (Throwable t) { | ||
if (e != null) { | ||
t.addSuppressed(e); | ||
} | ||
callback.completeExceptionally(t); | ||
return; | ||
} | ||
callback.completeExceptionally(e); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
|
||
/** | ||
* @param runnable The async runnable to run after this runnable | ||
* @return the composition of this runnable and the runnable, a runnable | ||
*/ | ||
default AsyncRunnable thenRun(final AsyncRunnable runnable) { | ||
return (c) -> { | ||
this.unsafeFinish((r, e) -> { | ||
if (e == null) { | ||
runnable.unsafeFinish(c); | ||
} else { | ||
c.completeExceptionally(e); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param condition the condition to check | ||
* @param runnable The async runnable to run after this runnable, | ||
* if and only if the condition is met | ||
* @return the composition of this runnable and the runnable, a runnable | ||
*/ | ||
default AsyncRunnable thenRunIf(final Supplier<Boolean> condition, final AsyncRunnable runnable) { | ||
return (callback) -> { | ||
this.unsafeFinish((r, e) -> { | ||
if (e != null) { | ||
callback.completeExceptionally(e); | ||
return; | ||
} | ||
boolean matched; | ||
try { | ||
matched = condition.get(); | ||
} catch (Throwable t) { | ||
callback.completeExceptionally(t); | ||
return; | ||
} | ||
if (matched) { | ||
runnable.unsafeFinish(callback); | ||
} else { | ||
callback.complete(callback); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param supplier The supplier to supply using after this runnable | ||
* @return the composition of this runnable and the supplier, a supplier | ||
* @param <R> The return type of the resulting supplier | ||
*/ | ||
default <R> AsyncSupplier<R> thenSupply(final AsyncSupplier<R> supplier) { | ||
return (c) -> { | ||
this.unsafeFinish((r, e) -> { | ||
if (e == null) { | ||
supplier.unsafeFinish(c); | ||
} else { | ||
c.completeExceptionally(e); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param runnable the runnable to loop | ||
* @param shouldRetry condition under which to retry | ||
* @return the composition of this, and the looping branch | ||
* @see RetryingAsyncCallbackSupplier | ||
*/ | ||
default AsyncRunnable thenRunRetryingWhile( | ||
final AsyncRunnable runnable, final Predicate<Throwable> shouldRetry) { | ||
return thenRun(callback -> { | ||
new RetryingAsyncCallbackSupplier<Void>( | ||
new RetryState(), | ||
(rs, lastAttemptFailure) -> shouldRetry.test(lastAttemptFailure), | ||
cb -> runnable.finish(cb) // finish is required here, to handle exceptions | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
).get(callback); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.async; | ||
|
||
import java.util.function.Predicate; | ||
|
||
|
||
/** | ||
* See tests for usage (AsyncFunctionsTest). | ||
* <p> | ||
* This class is not part of the public API and may be removed or changed at any time | ||
*/ | ||
@FunctionalInterface | ||
public interface AsyncSupplier<T> extends AsyncFunction<Void, T> { | ||
/** | ||
* This should not be called externally to this API. It should be | ||
* implemented as a lambda. To "finish" an async chain, use one of | ||
* the "finish" methods. | ||
* | ||
* @see #finish(SingleResultCallback) | ||
*/ | ||
void unsafeFinish(SingleResultCallback<T> callback); | ||
|
||
/** | ||
* This is the async variant of a supplier's get method. | ||
* This method must only be used when this AsyncSupplier corresponds | ||
* to a {@link java.util.function.Supplier} (and is therefore being | ||
* used within an async chain method lambda). | ||
* @param callback the callback | ||
*/ | ||
default void getAsync(final SingleResultCallback<T> callback) { | ||
unsafeFinish(callback); | ||
} | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
default void unsafeFinish(final Void value, final SingleResultCallback<T> callback) { | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafeFinish(callback); | ||
} | ||
|
||
/** | ||
* Must be invoked at end of async chain. | ||
* @param callback the callback provided by the method the chain is used in | ||
*/ | ||
default void finish(final SingleResultCallback<T> callback) { | ||
final boolean[] callbackInvoked = {false}; | ||
try { | ||
this.unsafeFinish((v, e) -> { | ||
callbackInvoked[0] = true; | ||
callback.onResult(v, e); | ||
}); | ||
} catch (Throwable t) { | ||
if (callbackInvoked[0]) { | ||
throw t; | ||
} else { | ||
callback.completeExceptionally(t); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param function The async function to run after this supplier | ||
* @return the composition of this supplier and the function, a supplier | ||
* @param <R> The return type of the resulting supplier | ||
*/ | ||
default <R> AsyncSupplier<R> thenApply(final AsyncFunction<T, R> function) { | ||
return (c) -> { | ||
this.unsafeFinish((v, e) -> { | ||
if (e == null) { | ||
function.unsafeFinish(v, c); | ||
} else { | ||
c.completeExceptionally(e); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
|
||
/** | ||
* @param consumer The async consumer to run after this supplier | ||
* @return the composition of this supplier and the consumer, a runnable | ||
*/ | ||
default AsyncRunnable thenConsume(final AsyncConsumer<T> consumer) { | ||
return (c) -> { | ||
this.unsafeFinish((v, e) -> { | ||
if (e == null) { | ||
consumer.unsafeFinish(v, c); | ||
} else { | ||
c.completeExceptionally(e); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow | ||
* @param errorFunction The branch to execute if the error matches | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @return The composition of this, and the conditional branch | ||
*/ | ||
default AsyncSupplier<T> onErrorIf( | ||
final Predicate<Throwable> errorCheck, | ||
final AsyncFunction<Throwable, T> errorFunction) { | ||
return (callback) -> this.finish((r, e) -> { | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (e == null) { | ||
callback.complete(r); | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
boolean errorMatched; | ||
try { | ||
errorMatched = errorCheck.test(e); | ||
} catch (Throwable t) { | ||
t.addSuppressed(e); | ||
callback.completeExceptionally(t); | ||
return; | ||
} | ||
if (errorMatched) { | ||
errorFunction.unsafeFinish(e, callback); | ||
} else { | ||
callback.completeExceptionally(e); | ||
} | ||
}); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.