Skip to content
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

Add PathStreamMessage that publishes the contents of a Path #3344

Merged
merged 16 commits into from
Mar 8, 2021

Conversation

ikhoon
Copy link
Contributor

@ikhoon ikhoon commented Feb 11, 2021

Motivation:

It must be nice to create StreamMessage from a File or Path.

Modifications:

  • Add PathStreamMessage that reads bytes from a Path and publishes with backpressure
  • Add useful factories to StreamMessage

Result:

You can now create a StreamMessage from a Path or File.

Path path = Paths.get("...");
StreamMessage<HttpData> publisher = StreamMessage.of(path);

Motivation:

It must be nice to create `StreamMessage` from a `File` or `Path`.

Modifications:

- Add `PathStreamMessage` that reads bytes from a `Path` and publishes with backpressure
- Add useful factories to `StreamMessage`

Result:

You can now create a `StreamMessage` from a `Path` or `File`.
```java
Path path = Paths.get("...");
StreamMessage<HttpData> publisher = StreamMessage.of(path);
```
@ikhoon ikhoon added this to the 1.5.0 milestone Feb 11, 2021
@ikhoon ikhoon changed the title Add PathStreamMessage that publishes a Path Add PathStreamMessage that publishes the contents of a Path Feb 11, 2021
@codecov
Copy link

codecov bot commented Feb 12, 2021

Codecov Report

Merging #3344 (2718c0e) into master (93a2b65) will increase coverage by 0.06%.
The diff coverage is 75.28%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3344      +/-   ##
============================================
+ Coverage     74.19%   74.26%   +0.06%     
- Complexity    13293    13324      +31     
============================================
  Files          1159     1160       +1     
  Lines         50740    50918     +178     
  Branches       6504     6530      +26     
============================================
+ Hits          37646    37812     +166     
- Misses         9775     9782       +7     
- Partials       3319     3324       +5     
Impacted Files Coverage Δ Complexity Δ
.../linecorp/armeria/common/stream/StreamMessage.java 68.18% <21.42%> (-12.59%) 23.00 <1.00> (+1.00) ⬇️
...ecorp/armeria/common/stream/PathStreamMessage.java 79.87% <79.87%> (ø) 19.00 <19.00> (?)
...eria/internal/common/DefaultSplitHttpResponse.java 77.38% <0.00%> (-2.39%) 14.00% <0.00%> (ø%)
...rmeria/common/stream/ConcatArrayStreamMessage.java 63.73% <0.00%> (-2.20%) 6.00% <0.00%> (ø%)
.../com/linecorp/armeria/server/RoutingPredicate.java 70.96% <0.00%> (-1.62%) 21.00% <0.00%> (-1.00%)
...ia/internal/common/stream/ByteBufDecoderInput.java 86.45% <0.00%> (-1.05%) 29.00% <0.00%> (-1.00%)
...rmeria/internal/client/grpc/ArmeriaClientCall.java 79.49% <0.00%> (-0.84%) 69.00% <0.00%> (-1.00%)
...inecorp/armeria/server/HttpResponseSubscriber.java 79.45% <0.00%> (-0.78%) 58.00% <0.00%> (-1.00%)
...om/linecorp/armeria/client/HttpSessionHandler.java 73.14% <0.00%> (+0.57%) 49.00% <0.00%> (+1.00%)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 93a2b65...2718c0e. Read the comment docs.

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Awesome, thanks! 😄

@ikhoon ikhoon modified the milestones: 1.5.0, 1.6.0 Feb 15, 2021
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Great job, @ikhoon!

@@ -0,0 +1,368 @@
/*
* Copyright 2020 LINE Corporation
Copy link
Member

Choose a reason for hiding this comment

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

2021


private static final Logger logger = LoggerFactory.getLogger(PathStreamMessage.class);

static final int DEFAULT_FILE_BUFFER_SIZE = 4096;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe 8192? 4K may be too small for modern OSes.

byteBuf.release();
maybeCloseFileChannel();
} else {
if (result > -1) {
Copy link
Member

Choose a reason for hiding this comment

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

>= 0?

Comment on lines 309 to 316
byteBuf.writerIndex(result);
final HttpData data;
if (withPooledObjects) {
data = HttpData.wrap(byteBuf);
} else {
data = HttpData.wrap(ByteBufUtil.getBytes(byteBuf));
byteBuf.release();
}
Copy link
Member

Choose a reason for hiding this comment

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

Could we do this without updating reader/writerIndex?

Copy link
Contributor Author

@ikhoon ikhoon Mar 8, 2021

Choose a reason for hiding this comment

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

IIUC, we need to call byteBuf.writerIndex(result) because the indexes of byteBuf are not changed by ByteBuffer.
https://github.com/netty/netty/blob/e5951d46fc89db507ba7d2968d2ede26378f0b04/buffer/src/main/java/io/netty/buffer/ByteBuf.java#L2292-L2294

* Exposes this buffer's sub-region as an NIO {@link ByteBuffer}. The returned buffer
* either share or contains the copied content of this buffer, while changing the position
* and limit of the returned NIO buffer does not affect the indexes and marks of this buffer.

Comment on lines 342 to 343
} catch (IOException ignored) {
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe log at WARN?

@ArgumentsSource(SubscriptionOptionsProvider.class)
@ParameterizedTest
void readFile(SubscriptionOption[] options) {
final Path path = Paths.get("src/test/resources/com/linecorp/armeria/common/stream/test.txt");
Copy link
Member

Choose a reason for hiding this comment

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

Could we test a file larger than the buffer size as well as a small one?

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

Nice job, @ikhoon !

@trustin trustin merged commit 08bc2bf into line:master Mar 8, 2021
@ikhoon ikhoon deleted the path-publisher branch April 1, 2021 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants