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

Asynchronous response in respond #74

Open
jfgrang opened this issue Dec 10, 2019 · 7 comments
Open

Asynchronous response in respond #74

jfgrang opened this issue Dec 10, 2019 · 7 comments

Comments

@jfgrang
Copy link

jfgrang commented Dec 10, 2019

I am looking for a way to make respond asynchronous.
In some cases I don't have a local file and I want to download it then deliver it.

The given example seems to only manage local synchronous requests. How would you perform a network download in that nextHandlerchain ?

  public func respond(to request: HTTPRequest, nextHandler: HTTPRequest.Handler) throws -> HTTPResponse? {
    // If this is a GET request, pass it to the next handler
    if request.method == .GET {
      return try nextHandler(request)
    }
    // Otherwise return 403 - Forbidden
    return HTTPResponse(.forbidden, content: "Only GET requests are allowed")
  }
}

I was considering to add a waitHandler that checks the status of a Session but it seems a little odd.

Thanks for your help

@jfgrang jfgrang changed the title Asynchronous respond Asynchronous response Dec 12, 2019
@jfgrang jfgrang changed the title Asynchronous response Asynchronous response in respond Dec 12, 2019
@yvbeek
Copy link
Member

yvbeek commented Jan 12, 2020

@jfgrang Unfortunately Telegraph doesn't support asynchronous responses at this moment. You might be able to implement it by creating your own Server class, inherit from the existing Server class and overriding some of the request handling methods there.

It would be great if we could support a closure return type in a handler, but that requires some refactoring.

Maybe next weekend I'll be able to make some updates to the framework. Add a Package.swift and things like that.

@tlemansec
Copy link

hello @yvbeek , is there any news about this subject? i would like to intercept and execute some requests to a distant server before returning an HTTPResponse, but i don't see anything to do that. Am i wrong?

@yvbeek
Copy link
Member

yvbeek commented Mar 2, 2020

@tlemansec Sorry for my late reply. Adding Swift Package Manager support and fixing the iOS 13 issue were way more difficult than I anticipated.

After this release I'll have a look at async reponses.

@yvbeek
Copy link
Member

yvbeek commented Mar 8, 2020

Version 0.28 is now available 🚀

To support asynchronous requests / responses I'm going to have to make some changes to the Server, Route and Handler classes. The most difficult part is the request handler chain, I have to somehow make that asynchronous.

I hope to have some time next weekend to work on it.

@samueleperricone
Copy link

@jfgrang I solved by using a DispatchGroup. Maybe is not optimal for scalability/concurrency, but for small async operation should be fine.
Here just an example calling a fake function process that returns a string:

private func handleRequest(request: HTTPRequest) -> HTTPResponse {
        var result: String?

        let group = DispatchGroup()
        group.enter()

        DispatchQueue.global().async {
            process { value in 
                 result = value
                 group.leave()
            }
        }

        // wait ...
        group.wait()

        return HTTPResponse(content: result ?? "-")
    }

@paultiarks
Copy link

It looks like this thread has gone quiet but are there any updates on supporting this in Telegraph without having to work-around and use Dispatch Groups?

@yvbeek
Copy link
Member

yvbeek commented Apr 7, 2023

@paultiarks Unfortunately it is going to take quite a bit of (re)architecture and dev work to add this to the library. As I no longer use Telegraph in my day-to-day work, it might take a while for me to look into this.

The DispatchGroup can be a workaround for now and should probably scale well enough for Telegraph's use cases. I'm also happy to look into PRs if there are people who have implemented this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants