-
Notifications
You must be signed in to change notification settings - Fork 243
Add reccomendations for client context #2924
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| (typed-context)= | ||
| # Context | ||
|
|
||
| When implementing a Smithy client, you will almost certainly need to pass | ||
| contextual information throughout the request lifecycle. **Context refers to | ||
JordonPhillips marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data that needs to be shared and tracked across different stages of processing a | ||
| single operation (such as retry counts, authentication tokens, or timing | ||
| information). | ||
|
|
||
| Client plugins in particular need a way to store and retrieve this information | ||
| during operation invocations, since they cannot modify the core request pipeline | ||
| directly. This guide provides guidance on how context objects can be safely | ||
| implemented and exposed to maintain type safety while allowing for | ||
| extensibility. | ||
|
|
||
| ## Implementation | ||
|
|
||
| This context could be defined explicitly as a structure with defined properties, | ||
| but that would quickly become bloated, unwieldy, and difficult to extend without | ||
| changing the core library code. | ||
|
|
||
| It is better to instead use an open map (such as `Map<String, Object>` in Java, | ||
| `dict` in Python, or similar structures in other languages). A given string key | ||
| still maps to a specific value type and serves the same purpose, but now the | ||
| context is open to extension without changing core library code. | ||
kstich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| While this sort of open map usage may be common in some languages, the lack of | ||
| type safety is a significant problem. To retain type safety, it is recommended | ||
| to use an interface that encodes the value type within the key itself using | ||
| generics or similar type system features. | ||
|
|
||
| ```java | ||
| /** | ||
| * A typed context bag. | ||
| */ | ||
| public interface Context { | ||
|
|
||
| /** | ||
| * A key wrapper that tracks the value type it is expected to be assigned | ||
| * to. | ||
| */ | ||
| final class Key<T> { | ||
| private final String name; | ||
|
|
||
| /** | ||
| * @param name Name of the value. | ||
| */ | ||
| public Key(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name; | ||
| } | ||
| } | ||
|
|
||
| static <T> Key<T> key(String name) { | ||
| return new Key<>(name); | ||
| } | ||
|
|
||
| /** | ||
| * Set a property. If it was already present, it is overridden. | ||
| * | ||
| * @param key Property key. | ||
| * @param value Value to set. | ||
| * @param <T> Value type. | ||
| */ | ||
| <T> void put(Key<T> key, T value); | ||
|
|
||
|
|
||
| /** | ||
| * Get a property. | ||
| * | ||
| * @param key Property key to retrieve the value for. | ||
| * @return the value, or null if not present. | ||
| * @param <T> Value type. | ||
| */ | ||
| <T> T get(Key<T> key); | ||
| } | ||
| ``` | ||
|
|
||
| Typed keys can then be statically defined and shared. A compiler or type checker | ||
| will validate that usage is correct, catching type mismatches at compile time | ||
| rather than runtime. These should be defined in the packages that primarily use | ||
| them. | ||
|
|
||
| For example, imagine if retry tracking were extracted to a client plugin. It | ||
| could define a static `RETRY_COUNT` property that can be exposed for use by | ||
| other plugins. | ||
|
|
||
| ```java | ||
| public final class RetryTracker { | ||
| // Any interested client plugin could use this context key to have | ||
| // type-safe and typo-safe access to the context property. | ||
| public static final Context.Key<Integer> RETRY_COUNT = Context.key("retry-count"); | ||
|
|
||
| public void onAttempt(Context context) { | ||
| Integer count = context.get(RETRY_COUNT); | ||
| if (count == null) { | ||
| context.put(RETRY_COUNT, 0); | ||
| } | ||
| context.put(RETRY_COUNT, count + 1); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Lifecycle | ||
|
|
||
| Each operation invocation should create its own context object. This prevents | ||
| unintentionally leaking context into other requests and reduces the chances of | ||
| concurrency issues. | ||
|
|
||
| Smithy clients should pass this context object to any integration hooks. | ||
| [TODO: link to interceptors documentation.] There should be at least one hook at | ||
| the beginning of the request pipeline to enable client plugins to populate the | ||
| context as soon as possible. | ||
|
Comment on lines
+114
to
+117
Contributor
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. Nit: Saying that there "should be at least one hook at the beginning" sounds like a requirement for hooks, not a requirement for context. I think we should drop that sentence from this doc and just let the (to-be-written) interceptors doc do the talking. |
||
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 |
|---|---|---|
|
|
@@ -58,4 +58,5 @@ Smithy clients should follow these tenets: | |
| :maxdepth: 1 | ||
|
|
||
| application-protocols/index | ||
| ``` | ||
| context | ||
| ``` | ||
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.