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 FilteredStreamMessage.onCancellation() #3375

Merged
merged 15 commits into from Mar 26, 2021
Merged

Conversation

minwoox
Copy link
Member

@minwoox minwoox commented Mar 9, 2021

Motivation:
An implementation of the FilteredStreamMessage might have resources to release when
the StreamMessage is complete. For example, an HttpResponse from ContentPreviewingService should call
ContentPreviewer.produce() to release the resource and produce the content preview.
However, because we don't provide a hook for Subscription.cancel(), it's easy to forget to clean up resources when the stream message is canceled.

Modification:

  • Add FilteredStreamMessage.onCancellation().

Result:

  • You can now clean up resources by overriding FilteredStreamMessage.onCancellation(...) when Subscription.cancel() is called.
    new MyFileteredHttpResponse(res) {
    
        ...
        @Override
        protected void onCancellation(Subscriber<? super U> subscriber) {
            // Clean up resources.
        }
    }
  • You no longer see that the log is not complete when applying ContentPreviewingService.

Motivation:
An implementation of the `FilteredStreamMessage` might have resources to release when
the `StreamMessage` is complete. For example, an `HttpResponse` from `ContentPreviewingService` should call
`ContentPreviewer.produce()` to release the resource and produce the content preview.
However, the `FilteredStreamMessage` is not notified when the `StreamMessage` is canceled by the `Subscriber`.

Modification:
- Add `FilteredStreamMessage.beforeCancel()` so that an implementation of the message
  can add a hook before `Subscription.cancel()`.

Result:
- You no longer see that the log is not complete when applying `ContentPreviewingService`.
@minwoox minwoox added this to the 1.6.0 milestone Mar 9, 2021
@codecov
Copy link

codecov bot commented Mar 9, 2021

Codecov Report

Merging #3375 (986c488) into master (9fa89e3) will decrease coverage by 0.01%.
The diff coverage is 69.04%.

❗ Current head 986c488 differs from pull request most recent head 3cdc0b3. Consider uploading reports for the commit 3cdc0b3 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3375      +/-   ##
============================================
- Coverage     74.31%   74.29%   -0.02%     
- Complexity    13527    13534       +7     
============================================
  Files          1174     1174              
  Lines         51703    51744      +41     
  Branches       6631     6641      +10     
============================================
+ Hits          38422    38443      +21     
- Misses         9915     9931      +16     
- Partials       3366     3370       +4     
Impacted Files Coverage Δ Complexity Δ
...internal/client/grpc/GrpcWebTrailersExtractor.java 70.00% <ø> (-1.63%) 3.00 <0.00> (ø)
...p/armeria/server/encoding/HttpEncodedResponse.java 65.33% <16.66%> (-3.24%) 17.00 <2.00> (ø)
...rp/armeria/server/encoding/HttpDecodedRequest.java 44.00% <40.00%> (-6.00%) 5.00 <3.00> (+2.00) ⬇️
...p/armeria/client/encoding/HttpDecodedResponse.java 67.34% <75.00%> (+5.18%) 14.00 <7.00> (+4.00)
...rmeria/internal/logging/ContentPreviewingUtil.java 85.45% <80.00%> (+11.03%) 6.00 <2.00> (+2.00)
...p/armeria/common/stream/FilteredStreamMessage.java 81.81% <91.66%> (-1.97%) 16.00 <3.00> (-1.00)
...meria/common/stream/RegularFixedStreamMessage.java 88.67% <0.00%> (-5.67%) 14.00% <0.00%> (-1.00%)
...orp/armeria/client/eureka/EurekaEndpointGroup.java 60.71% <0.00%> (-3.58%) 23.00% <0.00%> (-1.00%)
...ia/common/stream/ConcatPublisherStreamMessage.java 77.09% <0.00%> (-3.06%) 8.00% <0.00%> (ø%)
...eria/internal/common/DefaultSplitHttpResponse.java 77.38% <0.00%> (-1.79%) 14.00% <0.00%> (ø%)
... and 11 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 9fa89e3...3cdc0b3. Read the comment docs.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Could you check this FilteredStreamMessage?

@Override
protected void beforeComplete(Subscriber<? super HttpObject> subscriber) {
publisher.close();
}
@Override
protected Throwable beforeError(Subscriber<? super HttpObject> subscriber, Throwable cause) {
publisher.close();
return cause;
}

@minwoox
Copy link
Member Author

minwoox commented Mar 9, 2021

@ikhoon
Copy link
Contributor

ikhoon commented Mar 9, 2021

Could you check this FilteredStreamMessage?

This one? 😄 #3375 (files)

Indeed it is. 😅

// Call requestContentPreview(null) to make sure that the log is complete.
ctx.logBuilder().responseContentPreview(null);
}
produceResponseContentPreview(responseContentPreviewer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any chance that subscriber.onComplete() and subscription.cancel() are called concurrently?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could have an atomic flag, then we won't even need the cause instanceof CancelledSubscriptionException checks.

Copy link
Member Author

Choose a reason for hiding this comment

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

I realized that if we introduce an atomic flag, a user needs to do the extra work because beforeCancel() and beforeError() can be called twice in certain situation.
So let me just remove beforeCancel() and handle the cancelation event in beforeError.

@minwoox minwoox changed the title Add beforeCancel hook to FilteredStreamMessage Call FilteredStreamMessage.beforeError() when the StreamMessage is canceled Mar 9, 2021
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Thanks a lot! @minwoox

@minwoox minwoox changed the title Call FilteredStreamMessage.beforeError() when the StreamMessage is canceled Add FilteredStreamMessage.onCancellation() Mar 11, 2021
@ikhoon
Copy link
Contributor

ikhoon commented Mar 26, 2021

Would you mind resolving the conflicting files? 😄

@minwoox
Copy link
Member Author

minwoox commented Mar 26, 2021

Thanks fixed. 😉

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.

Thanks, @minwoox !

@trustin trustin merged commit 386268a into line:master Mar 26, 2021
@minwoox minwoox deleted the addBeforeCancel branch March 26, 2021 09:17
@minwoox
Copy link
Member Author

minwoox commented Mar 26, 2021

Thanks for reviewing. 😉

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.

None yet

3 participants