Comments (24)
Happy to PR this, shouldn't be too hard (famous last words).
from async-http-client.
I'll implement it today-tomorrow and will play with failure modes to see what we can do here
from async-http-client.
I've implemented a prototype, it is indeed potentially dangerous. If one returns .continue
when .end
or error
are received, future will never be completed. Only option is to complete promise with some kind of ReturnedNextOnEnd
error if we receive .continue
on .end
, but this is a runtime error, which is not great in already runtime-error-filled environment of async frameworks (with didReceiveEnd you just cannot not provide a result)... One solution here would be to drop promise from HTTPHandler
but in this case users of the library will have to provide a promise, which is not great (you cannot just create a promise, you need access to an EventLoop
). And it could make code that handles redirects to a different URL origin more complex (I'm not yet sure about that, will test it today).
Having state is also not super convenient with this type of functional consumer. With delegate its easier to re-use due to state encapsulation, but in case of a function, you have to have state be captured by a closure, and this is messy, imho, especially if you have more than one variable to capture. One can create a class for that state, but by this point you already have a class, so why not just conform to a protocol and use class as a delegate instead.
I will experiment with dropping promise from HTTPHandler
to see if it has potential. Also, I will experiment with having only one function in a protocol (similar to RxSwift observer).
from async-http-client.
thanks @ianpartridge . The delgate should be handled the HTTPTask
as the first parameter. That's I think also the usual pattern in Cocoa.
from async-http-client.
I'm thinking about re-structuring streaming API a bit and want to do 'internal pitch' here on in slack about possible improvements to it. Depending on outcome, API could change slightly. What are your general thoughts about this delegate API?
from async-http-client.
In general it feels more Cocoa-y than Swift-y, if that makes sense. But you only have to use the delegate if you want to do streaming, which is nice. I haven't really got enough experience with the package yet to have views on improvements. What changes do you have in mind?
from async-http-client.
Delegate has a downside of not being able to implement it inline, and that it's exactly as you say - not "swifty". I think it's even more java-like (this is were most of my experience comes from :) ). I was thinking maybe something like a singe method with an enum will work better:
execute(request: request) { event in
switch event {
case head(let head):
// process head
case body(let part):
// process body
case error(let error):
// handle error
}
}
from async-http-client.
I suggested something a bit like that to @weissi but he asked how cancellation could work?
from async-http-client.
You mean inside this closure? we can have two parameters there, event
and task
for example, or we can return something like .contunue
or .abort
from it
from async-http-client.
I like the return .abort
idea :) @weissi?
from async-http-client.
@artemredkin you also need to return a cancellation token for outside the closure...
from async-http-client.
@weissi that part won't change
from async-http-client.
@artemredkin ah so it'll be
let task = client.get(...)
someQueue.dispach(after: .now() + 2) {
task.cancel()
}
task.execute { event in
switch ...
}
from async-http-client.
no, something like this:
let task = client.execute(request) { event in
switch ...
}
someQueue.dispach(after: .now() + 2) {
task.cancel()
}
from async-http-client.
@artemredkin cool, if we also hand the task
to the closure, then I'm happy with this too. @Lukasa ?
from async-http-client.
@weissi what about returning .continue
, .done(Result)
and .abort
instead of passing in task
?
from async-http-client.
@artemredkin sure, we can do that. That would also allow us to support back-pressure, what does the .done(Result)
do?
from async-http-client.
.done(result)
is to return type-safe value as a result of this function, for example:
let count: Int = try client.execute(request) { event in
var count = 0
switch event {
case .body(let part):
count += part.availableBytes
case .end:
return .done(count)
default:
break
return .continue
}
}.wait()
from async-http-client.
@artemredkin, so .done
== .abort
except that it puts a value in the future and abort
fails it?
When do you fulfil the future if I do
let count: Int = try client.execute(request) { event in
return .continue
}.wait()
? Ie. what happens if I never return .done
or .abort
?
from async-http-client.
hmmm, good point...
from async-http-client.
I'll never be able to provide a value for promise...
from async-http-client.
I'll never be able to provide a value for promise...
AFAIK the only illegal thing would be .continue
on .end
and in that case we could just fail the promise maybe?
from async-http-client.
@artemredkin we could also treat .continue
on .end
like .done
?
from async-http-client.
@weissi the way it is designed right now (not sure if it's the best approach) - we have to fulfill a promise with some kind of value, right now we have promise.succeed(delegate.didFinishRequest())
from async-http-client.
Related Issues (20)
- Shutdown does not close HTTP2 connection
- Linker Error on using AsyncHTTPClient while using Vapor inside XCode Project HOT 2
- crashed: free(): corrupted unsorted chunks #2617 HOT 5
- Support for SSE (EventSource) HOT 3
- _
- Different results for the same request on mac OS and Linux HOT 1
- README.md async/await example does not compile HOT 1
- Crash: precondition failure in HTTPConnectionPool+HTTP2Connections.swift:209 HOT 2
- Remove swift-docc-plugin dependency
- Too low of a decompression ratio in `HTTPClient.shared` settings HOT 11
- `Connection refused` not throwing right away HOT 6
- Rename and clarify the `body.collect(upTo:)` API which gets used in an insecure way all the time HOT 1
- How to initiate a request using a socks5 proxy with authentication? HOT 3
- OOM if setting `concurrentHTTP1ConnectionsPerHostSoftLimit: .max`
- `HTTPClient.execute(...) async throws` API violates Structured Concurrency HOT 17
- Canceling a download `Task` doesn't actually cancel it? HOT 3
- 7% of the overall runtime spent in `SelectableEventLoop.debugDescription`
- > 8% overall wall time spent for simple HTTP request on NSURL initialisation & related things
- Undesired (as in older faster than newer) perf delta between older and newer APIs
- Proxy server implementation example needed HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-http-client.