-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement OIDC auth for async #1131
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9873b4a
Implement OIDC auth for async
katcharov 901e9d1
Apply suggestions from code review
katcharov 919af41
PR Fixes
katcharov a81e5aa
PR fixes
katcharov 53b4f67
Apply suggestions from code review
katcharov 6cc18af
Fixes
katcharov 2ec4729
PR fixes
katcharov e205e4f
Add full test variations, additional chaining methods, refactor
katcharov e7d4ffd
Fixes, naming
katcharov 4825ebe
Fix inheritance, refactor, more tests
katcharov ad85f8b
Remove redundant invocation
katcharov 8e32eed
PR fixes
katcharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
188 changes: 188 additions & 0 deletions
188
driver-core/src/main/com/mongodb/internal/async/AsyncRunnable.java
This file contains hidden or 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,188 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* See AsyncRunnableTest for usage | ||
*/ | ||
public interface AsyncRunnable { | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static AsyncRunnable beginAsync() { | ||
return (c) -> c.onResult(null, null); | ||
} | ||
|
||
void runUnsafe(SingleResultCallback<Void> callback); // NoResultCallback | ||
|
||
/** | ||
* Must be invoked at end of async chain. Wraps the lambda in an error | ||
* handler. | ||
* @param callback the callback provided by the method the chain is used in | ||
*/ | ||
default void finish(final SingleResultCallback<Void> callback) { | ||
try { | ||
this.runUnsafe((v, e) -> { | ||
try { | ||
callback.onResult(v, e); | ||
} catch (Throwable t) { | ||
throw new CallbackThrew("Unexpected Throwable thrown from callback: ", e); | ||
} | ||
}); | ||
} catch (CallbackThrew t) { | ||
// ignore | ||
} catch (Throwable t) { | ||
callback.onResult(null, t); | ||
} | ||
} | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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.onResult(null, e); | ||
return; | ||
} | ||
try { | ||
runnable.run(); | ||
} catch (Throwable t) { | ||
callback.onResult(null, t); | ||
return; | ||
} | ||
callback.onResult(null, null); | ||
}); | ||
} | ||
|
||
/** | ||
* 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) { | ||
callback.onResult(null, t); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
callback.onResult(r, e); | ||
}); | ||
} | ||
|
||
/** | ||
* @param runnable The async runnable to run after this one | ||
* @return the composition of this and the runnable | ||
*/ | ||
default AsyncRunnable thenRun(final AsyncRunnable runnable) { | ||
return (c) -> { | ||
this.finish((r, e) -> { | ||
if (e != null) { | ||
c.onResult(null, e); | ||
return; | ||
} | ||
try { | ||
runnable.finish(c); | ||
} catch (Throwable t) { | ||
c.onResult(null, t); | ||
} | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param supplier The supplier to supply using after this runnable. | ||
* @return the composition of this runnable and the supplier | ||
* @param <T> The return type of the supplier | ||
*/ | ||
default <T> AsyncSupplier<T> thenSupply(final AsyncSupplier<T> supplier) { | ||
return (c) -> { | ||
this.finish((r, e) -> { | ||
if (e != null) { | ||
c.onResult(null, e); | ||
return; | ||
} | ||
try { | ||
supplier.finish(c); | ||
} catch (Throwable t) { | ||
c.onResult(null, t); | ||
} | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow | ||
* @param runnable The branch to execute if the error matches | ||
* @return The composition of this, and the conditional branch | ||
*/ | ||
default AsyncRunnable onErrorRunIf( | ||
final Predicate<Throwable> errorCheck, | ||
final AsyncRunnable runnable) { | ||
return (callback) -> this.finish((r, e) -> { | ||
if (e == null) { | ||
callback.onResult(r, null); | ||
return; | ||
} | ||
try { | ||
boolean check = errorCheck.test(e); | ||
if (check) { | ||
runnable.finish(callback); | ||
return; | ||
} | ||
} catch (Throwable t) { | ||
callback.onResult(null, t); | ||
return; | ||
} | ||
callback.onResult(r, e); | ||
}); | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @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 this.thenRun(callback -> { | ||
new RetryingAsyncCallbackSupplier<Void>( | ||
new RetryState(), | ||
(rs, lastAttemptFailure) -> shouldRetry.test(lastAttemptFailure), | ||
cb -> runnable.finish(cb) | ||
).get(callback); | ||
}); | ||
} | ||
|
||
final class CallbackThrew extends AssertionError { | ||
private static final long serialVersionUID = 875624357420415700L; | ||
|
||
public CallbackThrew(final String s, final Throwable e) { | ||
super(s, e); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
driver-core/src/main/com/mongodb/internal/async/AsyncSupplier.java
This file contains hidden or 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,78 @@ | ||
/* | ||
* 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; | ||
|
||
import static com.mongodb.internal.async.AsyncRunnable.CallbackThrew; | ||
|
||
/** | ||
* See AsyncRunnableTest for usage | ||
*/ | ||
public interface AsyncSupplier<T> { | ||
|
||
void supplyUnsafe(SingleResultCallback<T> 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) { | ||
try { | ||
this.supplyUnsafe((v, e) -> { | ||
try { | ||
callback.onResult(v, e); | ||
} catch (Throwable t) { | ||
throw new CallbackThrew("Unexpected Throwable thrown from callback: ", e); | ||
} | ||
}); | ||
} catch (CallbackThrew t) { | ||
// ignore | ||
} catch (Throwable t) { | ||
callback.onResult(null, t); | ||
} | ||
stIncMale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @see AsyncRunnable#onErrorRunIf(Predicate, AsyncRunnable). | ||
* | ||
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow | ||
* @param supplier The branch to execute if the error matches | ||
* @return The composition of this, and the conditional branch | ||
*/ | ||
default AsyncSupplier<T> onErrorSupplyIf( | ||
final Predicate<Throwable> errorCheck, | ||
final AsyncSupplier<T> supplier) { | ||
return (callback) -> this.finish((r, e) -> { | ||
if (e == null) { | ||
callback.onResult(r, null); | ||
return; | ||
} | ||
try { | ||
boolean check = errorCheck.test(e); | ||
if (check) { | ||
supplier.finish(callback); | ||
return; | ||
} | ||
} catch (Throwable t) { | ||
callback.onResult(null, t); | ||
return; | ||
} | ||
callback.onResult(r, e); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.