-
Notifications
You must be signed in to change notification settings - Fork 927
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
Rename HttpData.of to HttpData.wrap, add copyOf, and deprecate offset. #1797
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1797 +/- ##
============================================
- Coverage 72.85% 72.83% -0.03%
- Complexity 8636 8687 +51
============================================
Files 770 772 +2
Lines 34016 34145 +129
Branches 4185 4196 +11
============================================
+ Hits 24781 24868 +87
- Misses 7105 7131 +26
- Partials 2130 2146 +16
Continue to review full report at Codecov.
|
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.
Thanks!
core/src/main/java/com/linecorp/armeria/common/DefaultHttpData.java
Outdated
Show resolved
Hide resolved
.../src/main/java/com/linecorp/armeria/server/annotation/ByteArrayRequestConverterFunction.java
Outdated
Show resolved
Hide resolved
I wonder if it's a good idea to remove the overloaded methods that accept if (offset == 0 && length == data.length) {
return HttpData.of(data);
} else {
return HttpData.of(Arrays.copyOfRange(data, offset, offset + length));
} |
Perhaps we could rename all factory methods of
Thoughts? |
Yeah I was definitely thinking of renaming
|
+1
Agreed. Let's leave them removed in |
24578bf
to
3ba7d6d
Compare
Tried changing to the proposed API let me know if it looks ok |
Also I'm wondering if the ByteBuf versions should be prefixed with unsafe or be in a different package |
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.
API change looks great to me. Thanks!
@@ -192,7 +192,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception | |||
fail(ctx, ContentTooLargeException.get()); | |||
return; | |||
} else { | |||
res.write(HttpData.of(data)); | |||
res.write(HttpData.copyOf(data)); |
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'm curious if we can use wrap()
here.
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 noticed this - didn't want to think too much about it but changed to wrap(retain)
since seems ok.
core/src/main/java/com/linecorp/armeria/common/DefaultHttpData.java
Outdated
Show resolved
Hide resolved
@@ -62,18 +62,13 @@ public boolean isEndOfStream() { | |||
|
|||
@Override | |||
public byte[] array() { | |||
if (buf.hasArray()) { | |||
if (buf.hasArray() && buf.arrayOffset() == 0) { |
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.
&& buf.array().length == length
?
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.
Oof - added tests
Pressed a wrong button. I actually requested changes. 😉 |
@@ -192,7 +192,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception | |||
fail(ctx, ContentTooLargeException.get()); | |||
return; | |||
} else { | |||
res.write(HttpData.of(data)); | |||
res.write(HttpData.copyOf(data)); |
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 noticed this - didn't want to think too much about it but changed to wrap(retain)
since seems ok.
core/src/main/java/com/linecorp/armeria/common/AggregatedHttpRequest.java
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/DefaultHttpData.java
Outdated
Show resolved
Hide resolved
@@ -62,18 +62,13 @@ public boolean isEndOfStream() { | |||
|
|||
@Override | |||
public byte[] array() { | |||
if (buf.hasArray()) { | |||
if (buf.hasArray() && buf.arrayOffset() == 0) { |
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.
Oof - added tests
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.
Nice work!
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.
Still LGTM :-)
0d27285
to
07bdb98
Compare
I found a gotcha with |
That's a good point. Perhaps we should introduce another |
Tried adding a separate implementation, how does it look? |
@anuraaga Looks good. Left just some comments. |
Thanks, @anuraaga! |
line#1797) Motivation: Currently, we allow creating a `HttpData` as a slice of a `byte[]`. However, in practice we almost never use it, only when constructing an `HttpData` after decoding a `CharSequence` (not `String`) which is very rare usage, so I've changed it to copy out the slice. We previously also did so when retrieving the result of Thrift serialization, but replaced that with using `ByteBufHttpData`. So instead, we can change the slicing APIs to use `ByteBuf` and remove the need for users to worry about `offset` / `length` when accessing the data array. Without the ability to slice `HttpData`, a user can access the array of an `HttpData` for processing by simply calling `.array()`, where it has been easy to forget to call `.offset()` and `.length()` as is currently required. It's especially problematic because it still usually works, but couldn't be guaranteed to work in all situations. Modifications: * Deprecate `offset()` and add `HttpData.wrap()` and `HttpData.copyOf()` * Remove usage of offset * Clarify Javadocs for `byte[]` factories. * Add wrapping implementation Result: - Fixes line#1771
Currently, we allow creating a
HttpData
as a slice of abyte[]
. However, in practice we almost never use it, only when constructing anHttpData
after decoding aCharSequence
(notString
) which is very rare usage, so I've changed it to copy out the slice. We previously also did so when retrieving the result of Thrift serialization, but replaced that with usingByteBufHttpData
. So instead, we can change the slicing APIs to useByteBuf
and remove the need for users to worry aboutoffset
/length
when accessing the data array.Without the ability to slice
HttpData
, a user can access the array of anHttpData
for processing by simply calling.array()
, where it has been easy to forget to call.offset()
and.length()
as is currently required. It's especially problematic because it still usually works, but couldn't be guaranteed to work in all situations.Fixes #1771