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

Sendable Response #3082

Merged
merged 16 commits into from
Nov 1, 2023
Merged

Sendable Response #3082

merged 16 commits into from
Nov 1, 2023

Conversation

0xTim
Copy link
Member

@0xTim 0xTim commented Oct 6, 2023

Make Response Sendable

@0xTim 0xTim added the semver-minor Contains new API label Oct 6, 2023
@0xTim 0xTim marked this pull request as ready for review October 6, 2023 01:09
@0xTim 0xTim requested a review from gwynne as a code owner October 6, 2023 01:09
@codecov-commenter
Copy link

codecov-commenter commented Oct 6, 2023

Codecov Report

Merging #3082 (c87192f) into main (e38dfe4) will decrease coverage by 0.11%.
Report is 3 commits behind head on main.
The diff coverage is 65.83%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3082      +/-   ##
==========================================
- Coverage   76.34%   76.23%   -0.11%     
==========================================
  Files         211      211              
  Lines        7862     7904      +42     
==========================================
+ Hits         6002     6026      +24     
- Misses       1860     1878      +18     
Files Coverage Δ
Sources/Vapor/Commands/ServeCommand.swift 85.18% <ø> (ø)
Sources/Vapor/Core/Core.swift 68.88% <ø> (ø)
Sources/Vapor/HTTP/Server/HTTPServerHandler.swift 93.87% <100.00%> (ø)
...s/Vapor/HTTP/Server/HTTPServerUpgradeHandler.swift 95.74% <100.00%> (+0.09%) ⬆️
Sources/Vapor/Request/Redirect.swift 100.00% <100.00%> (ø)
Sources/Vapor/Response/Response+Body.swift 66.03% <100.00%> (ø)
Sources/Vapor/Utilities/FileIO.swift 75.75% <100.00%> (+0.14%) ⬆️
Sources/Vapor/View/View.swift 100.00% <100.00%> (ø)
...apor/Concurrency/ResponseCodable+Concurrency.swift 39.47% <75.00%> (+1.63%) ⬆️
.../Vapor/HTTP/Server/HTTPServerResponseEncoder.swift 91.95% <93.33%> (ø)
... and 5 more

... and 1 file with indirect coverage changes

Copy link
Contributor

@MahdiBM MahdiBM left a comment

Choose a reason for hiding this comment

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

one comment otherwise looks good

}
set {
_body.withLockedValue { $0 = newValue }
self.headers.updateContentLength(self.body.count)
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps we could just do self.headers.updateContentLength(newValue) and skip a _body.withLockedValue { $0 }?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good shout

@MahdiBM
Copy link
Contributor

MahdiBM commented Oct 6, 2023

There are some warnings in the CI which don't seem blocking for this PR:

  • Some Request warnings, I think you're planning for adding Sendable conformance to that in the future.
  • Some Commands warnings which there is already a PR up for.

API breakage also seems to be inaccurate per usual.

Comment on lines 155 to 160
private let _version: NIOLockedValueBox<HTTPVersion>
private let _status: NIOLockedValueBox<HTTPResponseStatus>
private let _headers: NIOLockedValueBox<HTTPHeaders>
private let _body: NIOLockedValueBox<Body>
private let _upgrader: NIOLockedValueBox<Upgrader?>
private let _storage: NIOLockedValueBox<Storage>
Copy link
Member

Choose a reason for hiding this comment

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

Fine-grained locking is all well and good for efficiency, but this is an awful lot of additional allocations on a per-response basis. You can easily combine, at the very least, version, status, and headers under a single lock - it's exceedingly rare (in fact, it might never happen at all) to need to access any of these values simultaneously with one or both of the others from another thread, so contention ought not to be an issue. It might even be reasonable to include body in the same lock as well, though I'm less certain on that one.

(Truthfully, I might wonder if it didn't make more sense to put everything under a single lock, maybe with the exception of storage.)

Copy link
Member

Choose a reason for hiding this comment

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

I mean, for example, look at the implementation of description and think for a second about just how painful that's going to actually be at runtime...

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed

Copy link
Contributor

Choose a reason for hiding this comment

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

@0xTim I can't see a new commit on this branch 🤔

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 mean it would really help if I actually pushed my changes

@0xTim 0xTim requested a review from gwynne October 25, 2023 09:55
Copy link
Contributor

@MahdiBM MahdiBM left a comment

Choose a reason for hiding this comment

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

Looks good. 1 issue, 1 suggestion, 1 question 🙂.

Issues found by API checker are also invalid like the last time and can be ignored.

Sources/Vapor/Response/Response.swift Outdated Show resolved Hide resolved
Sources/Vapor/Response/Response.swift Show resolved Hide resolved
Co-authored-by: Mahdi Bahrami <github@mahdibm.com>
@0xTim 0xTim requested a review from MahdiBM October 26, 2023 10:28
@0xTim 0xTim merged commit 3bf4e73 into main Nov 1, 2023
16 of 17 checks passed
@0xTim 0xTim deleted the sendable-response branch November 1, 2023 15:22
@penny-for-vapor
Copy link

These changes are now available in 4.85.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor Contains new API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants