-
Notifications
You must be signed in to change notification settings - Fork 180
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
client-api: Add metadata as field on ServiceDiscovererEvent #2902
Closed
bryce-anderson
wants to merge
7
commits into
apple:main
from
bryce-anderson:bl_anderson/ServiceDiscovererMetadata-as-map
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a0b3288
WIP
bryce-anderson 6d216ea
Demonstrate the use of weight
bryce-anderson 409e176
Add way to modify Metadata
bryce-anderson 656ed73
This feels a bit better
bryce-anderson 6f6c911
More cleanup
bryce-anderson ab309cf
Merge branch 'main' into bl_anderson/ServiceDiscovererMetadata-as-map
bryce-anderson 93db35a
Fix build
bryce-anderson 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 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
124 changes: 124 additions & 0 deletions
124
...icetalk-client-api/src/main/java/io/servicetalk/client/api/ServiceDiscovererMetadata.java
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* 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 io.servicetalk.client.api; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Utilities helpful for extracting meta-data from {@link ServiceDiscovererEvent}s. | ||
*/ | ||
public final class ServiceDiscovererMetadata { | ||
|
||
public static final Map<String, Object> EMPTY_MAP = Collections.unmodifiableMap(new HashMap<>(0)); | ||
|
||
/** | ||
* Metadata that describes the relative weight of an endpoint. | ||
*/ | ||
public static final Key<Double> WEIGHT = new ServiceDiscovererMetadata.Key<>(Double.class, "endpoint.weight", 1d); | ||
|
||
/** | ||
* Metadata describing the priority class of an endpoint. | ||
*/ | ||
public static final Key<Integer> PRIORITY = new ServiceDiscovererMetadata.Key<>( | ||
Integer.class, "endpoint.priority", 0); | ||
|
||
/** | ||
* An extractor of meta-data to user with {@link ServiceDiscovererEvent} instances. | ||
* | ||
* A {@link ServiceDiscovererEvent} can carry additional metadata, but this data is not type safe. The key type | ||
* exists to provide a uniform way to define meta-data extractors that can properly extract and cast meta-data | ||
* while also providing a default. | ||
* @param <T> the expected type of the meta-data. | ||
*/ | ||
public static final class Key<T> { | ||
|
||
private final Class<T> clazz; | ||
private final String name; | ||
private final T defaultValue; | ||
|
||
public Key(final Class<T> clazz, final String name, final T defaultValue) { | ||
this.clazz = requireNonNull(clazz, "clazz"); | ||
this.name = requireNonNull(name, "name"); | ||
this.defaultValue = requireNonNull(defaultValue, "defaultValue"); | ||
} | ||
|
||
/** | ||
* Get the name associated with the meta-data. | ||
* @return the name associated with the meta-data. | ||
*/ | ||
public String name() { | ||
return name; | ||
} | ||
|
||
/** | ||
* The java class that is expected to be associated with the meta-data. | ||
* @return the java class that is expected to be associated with the meta-data. | ||
*/ | ||
public Class<T> clazz() { | ||
return clazz; | ||
} | ||
|
||
/** | ||
* Determine whether the meta-data both contains an entry with the keys name and that entry is the correct type. | ||
* @param event the {@link ServiceDiscovererEvent} for which to check if the meta-data exists. | ||
* @return true if the meta-data contains an entry with the keys name and the value is the correct type. | ||
*/ | ||
public boolean contains(ServiceDiscovererEvent<?> event) { | ||
return clazz.isInstance(event.metadata().get(name)); | ||
} | ||
|
||
/** | ||
* Extract the meta-data from a {@link ServiceDiscovererEvent}, or get the default. | ||
* @param event the {@link ServiceDiscovererEvent} from which to extract the meta-data. | ||
* @return the value contained in the meta-data, or the default if it doesn't exist or has the wrong type. | ||
*/ | ||
public T getValue(ServiceDiscovererEvent<?> event) { | ||
Object result = event.metadata().get(name); | ||
if (clazz.isInstance(result)) { | ||
return (T) result; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null || obj.getClass() != Key.class) { | ||
return false; | ||
} | ||
Key<?> other = (Key<?>) obj; | ||
return other.clazz.equals(clazz) && other.name.equals(name); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Key<" + clazz.getName() + ">(" + name + ", " + defaultValue + ")"; | ||
} | ||
} | ||
|
||
private ServiceDiscovererMetadata() { | ||
// no instances. | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason for not using
ContextMap
instead of aMap
with a new customKey
?I opened #2906 in case we can proceed with
ContextMap
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see two main reasons. The first being that ContextMap is mutable so we can't return a shared instance. That makes it so a default method takes the form
Which seems not great.
The second is that ContextMap is keyed on the
Key
instance, not it's name, so the produces and consumers of metadata must share the exact same key making a strong API linkage between the two.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my PR Add
ContextMaps
to let users create variousContextMap
s #2906 that addsContextMaps
with various implementations, including empty, singleton and unmodifiable.That's correct, see
HttpContextKeys
as an example of how a single key as shared. We can have a similar "key holder" class in client-api.