Giter Club home page Giter Club logo

Comments (4)

weavejester avatar weavejester commented on August 16, 2024

Initially I thought this was a bug, but now that I've investigated it more fully, I don't believe it is.

The reason this doesn't work with synchronous handlers is because the adapter expects the write-body-to-stream protocol method to also be synchronous. That is, the response is complete when both the handler and write-body-to-stream have completed.

This seems like reasonable behavior to have, as synchronous functions have guarantees that asynchronous ones do not. A thread is assigned to the handler, and then to write the body stream, and then given back up to the pool. There's no instance where the body stream could be left open without also blocking the request thread.

Going back to the handler function you gave as an example, we could rewrite this to work correctly just by removing the future. The only case where this doesn't work is if we want to write data asynchronously to the output stream, without blocking a worker thread; but this is what :async? true is for.

My current thinking is that it's better to keep the guarantees that synchronous, blocking I/O give us. Particularly if Java's virtual threads allow us to park a thread for relatively little cost in future.

from ring.

chpill avatar chpill commented on August 16, 2024

My current thinking is that it's better to keep the guarantees that synchronous, blocking I/O give us. Particularly if Java's virtual threads allow us to park a thread for relatively little cost in future.

I think I understand your point, this seems simpler and safer.

I'm re-reading the doc string of the protocol now:

  (write-body-to-stream [body response output-stream]
    "Write a value representing a response body to an output stream. The stream
    will be closed after the value had been written. The stream may be written
    asynchronously.")

In retrospect, maybe "after the value has been written" means "after write-body-to-stream returns"? Which would then only true in the "ring sync" case. I guess this is quite tricky to document because details from the implementation seem to leak into the protocol. The lifecycle of output-stream is managed by ring sync, but left completely up to the user by ring async.

from ring.

weavejester avatar weavejester commented on August 16, 2024

Yes, I think we want to update the protocol docstring to explain the use-case more clearly.

from ring.

sunng87 avatar sunng87 commented on August 16, 2024

Back to servlet spec, it does not allow async thread to write to HttpServletResponse outside handler methods of HttpServlet. Jetty has flexible requirement for this but it requires a callback to be resolved when write finished. For now, it's recommended to use :async ring handlers for this I think.

from ring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.