Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Add support for binary custom metadata fields #102

Merged
merged 34 commits into from
Jan 15, 2019

Conversation

per-gron
Copy link
Collaborator

@per-gron per-gron commented Dec 5, 2018

Adds support for base64 encoded "-bin" gRPC custom metadata.

[EDIT] I thought this would be enough to solve #79 but it's not: With this PR you can write custom metadata but not yet read it in a sane way, and it's not possible at all to differentiate between initial and trailing metadata in a response (not sure it even stores both).

Necessary for #27. See discussion at #90. This is a follow-up of #92.

Things to look for when reviewing:

  • How do you like what the user-facing API ends up like?
  • The ValueEncoding trait is afaik not importable from outside of this crate. Is that enough to prevent others from making implementations of it? It's not supposed to be possible.

Ping @carllerche here is a more or less fully fleshed out PR of what we discussed before.

What this commit does is to introduce _bin APIs for dealing with binary
metadata keys, and distinct Binary vs Ascii MetadataKey types. It doesn't
actually do any encoding or enforce any invariants yet.
It is just unhelpful for the user of MetadataMap that they were separate.
With the split of binary vs ascii keys I can't think of a reasonable way
to implement this that doesn't have an API that is so complicated that
it's not worth using for the very small performance benefits it provides.
It's still possible to go down to the lower HeaderMap level and use drain
from there.
That way it's more natural that both MetadataValue and MetadataKey use them.
This is a first step towards making the code automatically base64 encode
and decode the binary values in MetadataMaps.
They should be constructed from byte arrays, not strings.
The "name" name is an outdated reference to HeaderName.
Converting to Binary is possible but I can't think of a use case.
Let's add it if it turns out it's useful. MVP etc.
I can't think of a reasonable use case where this shortcut helps meaningfully.
Instead of implementing the necessary base64 encoding here I'll just remove it.
… way

This is a step towards making this method do the right thing for
MetadataValue<Binary>.
It seems unnecessary to provide this helper for binary values.
…e for Ascii

It's not possible to return a slice to a the decoded base64 data for Binary
values.
Turns out it is useful after all, otherwise there is no way to construct
binary metadata values without copying.
@per-gron per-gron changed the title Request metadata bin Support for binary custom metadata fields Dec 5, 2018
@per-gron per-gron changed the title Support for binary custom metadata fields Add support for binary custom metadata fields Dec 5, 2018
Copy link
Contributor

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the plan here is pretty sound!

src/metadata_value.rs Outdated Show resolved Hide resolved
src/metadata_encoding.rs Outdated Show resolved Hide resolved
src/metadata_encoding.rs Outdated Show resolved Hide resolved
src/metadata_encoding.rs Outdated Show resolved Hide resolved
src/metadata_encoding.rs Outdated Show resolved Hide resolved
@per-gron
Copy link
Collaborator Author

I have addressed the review comments, except for preventing construction of the error types outside of the crate. I might be missing something but still think the potential future benefit of being able to add non-Default, non-Option fields to those error types (which I expect will remain empty structs forever) is much smaller than the current benefit of not requiring every single client with unit tests to wrap every call to this library in order to be able to test error conditions.

Copy link
Contributor

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@per-gron
Copy link
Collaborator Author

@carllerche do you also want to review this PR before I merge? I'll probably merge this tomorrow unless I get more comments.

@carllerche
Copy link
Member

I'm back now! I will try to take a look in the next 24 hours. I'm still catching up on email :)

@per-gron
Copy link
Collaborator Author

@carllerche Aha, I see! No worries, I'm in no particular rush with this really. I'm off to vacation myself next week and will likely not spend much time on this until January.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it! Thanks so much for this PR (and sorry for the very long 24 hours).

I like the API strategy you took. I think it is probably the best we can do.

I left some questions inline, but they are not related to this PR, so not worth blocking the merge.

unsafe impl<'a> Sync for Drain<'a> {}
unsafe impl<'a> Send for Drain<'a> {}
unsafe impl<'a, VE: ValueEncoding> Sync for ValueDrain<'a, VE> {}
unsafe impl<'a, VE: ValueEncoding> Send for ValueDrain<'a, VE> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two unsafe impls required? The struct doesn't contain any unsafe types (the version in http did require it as it had raw pointers).

unsafe impl<'a> Sync for ValueIterMut<'a> {}
unsafe impl<'a> Send for ValueIterMut<'a> {}
unsafe impl<'a, VE: ValueEncoding> Sync for ValueIterMut<'a, VE> {}
unsafe impl<'a, VE: ValueEncoding> Send for ValueIterMut<'a, VE> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I don't think these unsafe impls are needed?

@carllerche carllerche merged commit 01defa8 into tower-rs:master Jan 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants