-
Notifications
You must be signed in to change notification settings - Fork 125
Update README.md for async/await #554
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
Conversation
README.md
Outdated
let progress = Double(receivedBytes)/Double(expectedBytes) | ||
print("progress: \(Int(progress * 100))%") | ||
} | ||
// in case backpressure is needed, all reads will be paused until the end of the for loop. |
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.
backpressure has nothing todo with the loop, and I don't think we should explain it here. It just works ;)
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 think we should mentioned this at least somewhere. AFAIK, this is not documented anywhere or am I missing something? This is a really great feature and we should definitely mention it.
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 with @dnadoba that we should mention it, but I'm with @fabianfett that I don't think we need to explain it: it just works. In this case, maybe at the top of the loop I'd change that comment to something like:
// asynchronously iterates over all body fragments
// this loop will automatically propagate backpressure correctly
README.md
Outdated
} | ||
// in case backpressure is needed, all reads will be paused until the end of the for loop. | ||
} | ||
print("did receive \(receivedBytes) bytes") |
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.
close AHC?
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.
The full example has the shutdown():
async-http-client/Examples/StreamingByteCounter/StreamingByteCounter.swift
Lines 25 to 52 in 3405d11
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) | |
do { | |
let request = HTTPClientRequest(url: "https://apple.com") | |
let response = try await httpClient.execute(request, timeout: .seconds(30)) | |
print("HTTP head", response) | |
// if defined, the content-length headers announces the size of the body | |
let expectedBytes = response.headers.first(name: "content-length").flatMap(Int.init) | |
var receivedBytes = 0 | |
// Asynchronously iterates over all body fragments. | |
for try await buffer in response.body { | |
// For this example, we are just interested in the size of the fragment | |
receivedBytes += buffer.readableBytes | |
if let expectedBytes = expectedBytes { | |
// if the body size is known, we calculate a progress indicator | |
let progress = Double(receivedBytes)/Double(expectedBytes) | |
print("progress: \(Int(progress * 100))%") | |
} | |
// in case backpressure is needed, all reads will be paused until returned future is resolved | |
} | |
print("did receive \(receivedBytes) bytes") | |
} catch { | |
print("request failed:", error) | |
} | |
// it is important to shutdown the httpClient after all requests are done, even if one failed | |
try await httpClient.shutdown() |
We would need to nest it in a do catch to be correct but I'm fine with it if you want to add it. I didn't add it because the old one also has no shutdown. However, I'm just discovering that the old examples doesn't create an AsyncHTTPClient, it just assumes that there is one. I see two options:
- remove HTTPClient creation
- or add shutdown
WDYT?
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 think add the shutdown and nest in a do catch.
// handle body | ||
} else { | ||
// handle remote error | ||
} |
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.
So much nicer! 🎉
README.md
Outdated
let progress = Double(receivedBytes)/Double(expectedBytes) | ||
print("progress: \(Int(progress * 100))%") | ||
} | ||
// in case backpressure is needed, all reads will be paused until the end of the for loop. |
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 with @dnadoba that we should mention it, but I'm with @fabianfett that I don't think we need to explain it: it just works. In this case, maybe at the top of the loop I'd change that comment to something like:
// asynchronously iterates over all body fragments
// this loop will automatically propagate backpressure correctly
README.md
Outdated
} | ||
// in case backpressure is needed, all reads will be paused until the end of the for loop. | ||
} | ||
print("did receive \(receivedBytes) bytes") |
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 think add the shutdown and nest in a do catch.
d82ff28
to
6e974af
Compare
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.
Awesome!
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.
Looks good, I left two tiny nits though
README.md
Outdated
|
||
if let expectedBytes = expectedBytes { | ||
// if the body size is known, we calculate a progress indicator | ||
let progress = Double(receivedBytes)/Double(expectedBytes) |
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.
nit, space makes it more readable:
let progress = Double(receivedBytes)/Double(expectedBytes) | |
let progress = Double(receivedBytes) / Double(expectedBytes) |
We want to highlight the new async/await API in the README.
EventLoopFuture
s1.9.0