Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Add optional queue to handler #86

Closed
wants to merge 3 commits into from

Conversation

helje5
Copy link
Contributor

@helje5 helje5 commented Nov 5, 2017

For async servers, there has to be a synchronization point
for done callbacks. This is what the queue is for.

If the handler doesn't dispatch to other queues but stays
within the calling one, it doesn't need to call the done
callbacks via

queue.async { done() }

but can do a straight

done()

But if the handler does perform its work async, it need
to use the given queue to call the done, e.g.:

func workHard(req: HTTPRequest, res: HTTPResponseWriter, queue: DispatchQueue?) {
    DispatchQueue.background.async {
        // do hard work
        queue.async {
           res.writeHeader(status: .ok)
        }
    }
}

etc.

We are using DispatchQueue here. Another option is to add a
non-optional

HTTPSynchronizationContext protocol

(adapted by DispatchQueue or a uv or sync variant)

For async servers, there has to be a synchronization point
for done callbacks. This is what the `queue` is for.

If the handler doesn't dispatch to other queues but stays
within the calling one, it doesn't need to call the done
callbacks via

    queue.async { done() }

but can do a straight

    done()

But if the handler does perform its work async, it need
to use the given queue to call the done, e.g.:

    func workHard(req: HTTPRequest, res: HTTPResponseWriter, queue: DispatchQueue?) {
        DispatchQueue.background.async {
            // do hard work
            queue.async {
               res.writeHeader(status: .ok)
            }
        }
    }

etc.

We are using `DispatchQueue` here. Another option is to add a
non-optional

  HTTPSynchronizationContext protocol

(adapted by DispatchQueue or a uv or sync variant)
@seabaylea seabaylea requested a review from carlbrown November 7, 2017 20:10
/// - queue: optional, if set, dispatch done callbacks back to this queue (if
/// the handler processing escapes the calling queue)
/// - Returns: HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it.
public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter, DispatchQueue?) -> HTTPBodyProcessing
Copy link
Member

Choose a reason for hiding this comment

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

On Linux you'll need to import Dispatch explicitly to have access to DispatchQueue

Copy link
Contributor

Choose a reason for hiding this comment

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

Aside: from Swift 4.1 that won't be true, thanks to swiftlang/swift-corelibs-foundation#1206 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though importing Foundation is non-sense in the first place, no Foundation is used here ;->

helje5 added a commit to ZeeZide/http that referenced this pull request Nov 10, 2017
Similar to PR swift-server#86. In this case it is non-optional,
that thing being a concrete implementation.

All API functions need to run on the given queue. But
since the handler itself also runs on that queue, it
only needs to be done if the handler actually dispatches
to a different queue.

This is an optimization to avoid excessive dispatching
in async environments. (if API calls could be issued
from arbitrary queues, they would always need to be
queued to the handler queue. which is possible but
pretty expensive).

P.S.: The argument is necessary because there is
nothing like `DispatchQueue.current` in GCD anymore.
@carlbrown
Copy link
Contributor

@helje5 Will this still be relevant after #96 gets completed?

@helje5
Copy link
Contributor Author

helje5 commented Nov 10, 2017

Again this depends on whether we want to use #96 or something like it. If not, we should still have this and not restrict the API artificially.

(but yes, if you merge #96, this can be closed too)

@carlbrown
Copy link
Contributor

Based on the last comment (and my current assumption/hope that we're going to try to merge #96), I'm going to close this. If we decide we can't use #96 for some reason, then we can reopen it.

@carlbrown carlbrown closed this Nov 10, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants