[swift-server-dev] Prototype of the discussed HTTP API Spec

Michael Chiu hatsuneyuji at icloud.com
Wed May 31 19:00:43 CDT 2017


I think i need to clarify something: I’m ok with a asynchronous api that executes synchronously, for example if the api is something like [[ a. {  b() } ; c() ]], executes as [[ a(); b(); c() ]], it is totally fine since it’s just synchronous api with syntactic sugar.

>>> Not really, what you do when you use kqueue/(e)poll/select is that only said calls are blocking and you set your file descriptors to non-blocking.
>> 
>> Despite kqueue is a blocking, it really only blocks when there’s nothing to do. So semantic-wise, the thread will never block as long as there’s work to do.
> 
> That is generally true. Also read only blocks if there's nothing to read. However you unfortunately don't know when the other end of the network connection will write something. It might be immediate, it might be minutes, hours or days

 I’m not quite sure how this is relevant.

> That is exactly inversion of control. You'll notice that you can't just read when you feel like, you can only read (and write) when kevent tell you to. If you want, do extend your example to make it an echo server, ie. write the bytes that you read. You'll notice that you can't just do what a straightforward synchronous API would look like:
> 
>    var bytes = ...
>    let readLengh = read(..., &bytes, maxLen)
>    write(..., bytes, readLength)
> 
> you will need to save the bytes and write them when kevent _tells you_ to write them. That is inversion of control.

You don’t have to, especially for write, you can write read/to a socket anytime you want despite kevent or not, If you don’t want things to block, use the MSG_DONTWAIT flag.
A working example is here: https://gist.github.com/michael-yuji/f06ac328c3cb052fbe7aaa325486fcf1#file-main-c-L57 <https://gist.github.com/michael-yuji/f06ac328c3cb052fbe7aaa325486fcf1#file-main-c-L57> . It does contains some kevent logic but no, you don’t have to write when kevent tells you to.
I’d love to discuss more on kqueue but it’s a bit off topic here.

> I'd guess that most programmers prefer an asynchronous API with callback (akin to Node.js/DispatchIO) to using the eventing mechanism directly and I was therefore assuming you wanted to build that from kevent() (which is what they're often used for). Nevertheless, kevent() won't make your programming model any nicer than asynchronous APIs and as I mentioned before you can build one from the other in a quite straightforward way. What we don't get from that is ordinary synchronous APIs that don't block kernel threads and that happens to be what most people would prefer eventually. Hence libdill/mill/venice and Zewo :).

Johannes, I totally agree with you. A asynchronous API is more intuitive and I agree with that. But since we are providing low level API for ppl like Zewo, Prefect, and Kitura, it is not right for us to assume their model of programming.

For libdill/mill/venice, even with green threads they will block when there’s nothing to do, in fact all the example you listed above all uses events api internally. Hence I don’t think if an api will block a kernel thread is a good argument here. And even if such totally non-blocking programming model it will be expensive since the kernel is constantly scheduling a do-nothing-thread. (( if the io thread of a server-side application need to do something constantly despite there’s no user and no connection it sounds like a ghost story to me )).  

Btw, I just made a working echo server with no event/coroutine api and it’s truly synchronous and non-blocking in C++ just for fun (no it’s not good code).
https://gist.github.com/michael-yuji/94eda2f8cf3910aa3ff670112728f641 <https://gist.github.com/michael-yuji/94eda2f8cf3910aa3ff670112728f641>

The main difference between (a)sync api is that sync api do only one thing, read/write to an io, async api is doing two things, read/write and scheduling. 

It is true that we can expose protocol to programmers and let them integrate their own scheduling model, but that will be dead painful to do and introduce unnecessary overheads vs just give them a synchronous API and they can just call it from their code.

It’s like ordering food over phone from a restaurant, you can either pickup (synchronous) your order or have it deliver to you (asynchronous). Yes most people will prefer delivery, But a pickup option as always more flexible despite you have to wait in the restaurant if the food is not ready. Now if we only expose the asynchronous model, what happen is the main code from the user tell the restaurant where to deliver and what to do when different things happen, which can be a very complex piece of logic.

On the other hand, with a synchronous (pick up) option available, we can do something like ask Bob to pickup the food in situation A, give Alice the food if B happens. Now with some event notification system, says the restaurant will give you a phone call when the food is ready, it is much easier to integrate your every day schedule, without having a delivery guy constantly drive around a shared highway and hence creating traffic (thread scheduling).   [[[ you can probably tell i just have a really bad experience with a food delivery guy at this moment ]]]

Generally speaking, we as the server side work group should always give the end user that has a bit more room and freedom, from the API. If they call a blocking call, they probably know what they’re doing.


> 
> I'm aware of that but you'll suffer from the inversion of control. I'm pretty sure you'll end up with an event loop that calls kevent/epoll all the time and once there's something to do, it'll call the handler registered for that file descriptor (which is what DispatchSources are).
> 

Yes and at least I can do it.

> 
> you can do the same in the asynchronous API with back-pressure. I'll quote again from the echo server
> 
> --- SNIP ---
>     return .processBody { (chunk, stop) in
>         switch chunk {
>             case .chunk(let data, let finishedProcessing):
>                 res.writeBody(data: data) { _ in
>                     finishedProcessing()
>                 }
>             case .end:
>                 res.done()
>             default:
>                 stop = true /* don't call us anymore */
>                 res.abort()
>         }
>     }
> --- SNAP ---
> 
> the call the finishedProcessing() meant that you're ready for more data to be read, ie. the callback to processBody will only be called again after finishedProcessing() was called inside it. That makes sure that the bytes have been written (res.writeBody(...) { ... }) successfully before we accept more.

As I mentioned the parsing is another story, and your solution also apply to the synchronous version (by no mean I think it’s good. just prove of concept)  I provided. I’ll quote it again.

parser.processBody =  { (chunk, stop) in
         switch chunk {
=====ommit
         }
     }

extension Parser {
	func feed(data: AnyCollection<UnsafeBufferPointer>) -> Done
}

> We're only talking about the API and not the implementation. You can absolutely implement the API I proposed with only one thread with one large shared buffer for all connections if you want. Running everything on one thread with an async-only API is pretty much exactly what Node.js has as its only implementation and you might find that the APIs look remarkably similar [1].
> 
> There's nothing stopping you from implementing the proposed API in one thread that services many requests with as much sharing of buffers as you like. Should there be something concrete missing that is making your life hard, please flag it as soon as possible and we can add it. The only implementations I have done of this API are one multithreaded one on top of DispatchIO and one fully synchronous one with one thread per request. Said that, the DispatchIO implementation should be configurable to only use one thread (using a serial dispatch queue instead of a concurrent one for all the event callbacks).
> 
> [1]: https://nodejs.org/api/http.html <https://nodejs.org/api/http.html>

That only says asynchronous is good and useful and it indeed is, but not a reason to abandon synchronous api. 

Still waiting my lunch,
Cheers,

Micheal.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-server-dev/attachments/20170531/640bdc65/attachment.html>


More information about the swift-server-dev mailing list