-
Notifications
You must be signed in to change notification settings - Fork 595
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
First draft of message attributes #2872
Conversation
b9ad8ab
to
4f0c14f
Compare
Test PASSed. |
4f0c14f
to
8ce775f
Compare
Test FAILed. Pull request validation reportFailed Test SuitesTest result for 'akka-http-core / Pr-validation / ./ executeTests'
|
failure was #2349 |
PLS BUILD |
Test PASSed. |
* Try to find the first attribute of the given class and return | ||
* Optional.of(header), otherwise this method returns an empty Optional | ||
*/ | ||
<T> Optional<T> getAttribute(Class<T> attributeClass); |
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.
If we leave it allowing multiple maybe it should rather be firstAttributeOf
or something that conveys what the docs now say (that'd also align it with the collection api somewhat on the Scala side)?
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.
Ah, but header can be multiple and getHeader returns first. Got it. NVM then.
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.
yeah, it's somewhat confusing but being consistent with getHeader
might be worth it anyway
This approach follows the Play API (https://www.playframework.com/documentation/2.8.x/Highlights26) in that you can add attributes of any user type. Otherwise it follows the existing conventions from Akka HTTP Headers, so you can have multiple attributes of the same type. If you want to distinguish different attributes of the same type, like you could in Play with different keys, you would have to introduce a wrapper type (either holding the key or creating a separate wrapper for each key). The main downside of this approach is that it increases the memory usage of a message with one field.
8ce775f
to
024c144
Compare
Test PASSed. |
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.
The main question here is if we want just that bag of attribute values of any kind without any keys or other metadata than the runtime class per value. It's similar to how headers are currently implemented, indeed, but an instance of HttpHeader
is by itself already a key-value entry. Also we shouldn't take the current headers implementation as an example as lookup can be quite inefficient.
Aside from implementation simplicity what are the benefits of the value-only approach?
I picked it mainly for its API familiarity, also given attributes and headers are similar contexts (being attached to the requests/responses and not the requestcontext). We can't predict what kinds of objects people would want to store in the attributes, and adding typed keys/values like in play (https://www.playframework.com/documentation/2.8.x/Highlights26#Request-attributes) seemed somewhat boilerplatey from a user perspective. Do you have another pattern in mind that might fit well? |
Yeah, but I wonder what would be the use case of adding attributes without a way to clearly identify them: req.addAttributes("something", "something else") It feels to me that users will then need to define custom types to wrap values so that they can be identified by that type. Given that, should we use |
I think we should pick how this should look like before merging. Perhaps still add
I agree that doesn't make sense
Do you think that is a good or a bad thing? ;). One of the use cases for message attributes is the TLS metadata. With the proposal in this PR getting that would be something like We could also have the convention to put the key on the companion object, so you'd get something like |
I would like that. Another use case would be the |
I would suggest to allow only a single value per key then (or none). |
Yes, exactly - that's another motivation for not trying to follow the headers API for the attributes, then. I'll try to prototype the approach with |
Sorry for not being clear: I think it is bad, mainly if you are adding your own types into the attributes (for example
That sounds great to me. In Play, there is an inner companion object called |
(other proposal is at #2938) |
Closing in favor of #2938 |
Refs #1500
This approach follows the Play API (https://www.playframework.com/documentation/2.8.x/Highlights26)
in that you can add attributes of any user type. Otherwise it follows the
existing conventions from Akka HTTP Headers, so you can have multiple
attributes of the same type. If you want to distinguish different attributes of
the same type, like you could in Play with different keys, you would have to
introduce a wrapper type (either holding the key or creating a separate wrapper
for each key).
The main downside of this approach is that it increases the memory usage of a
message with one field.