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

It is not possible to express a per-request Scope in a Handler that covers the entire request/response lifetime #3197

Open
jgranstrom opened this issue Nov 5, 2024 · 13 comments · May be fixed by #3229
Labels
💎 Bounty bug Something isn't working

Comments

@jgranstrom
Copy link
Contributor

If a Handler requires a Scope with a lifetime spanning all of request/response, it is not possible to express unless using a broader Scope that is not per-request.

An example case is:

def myPeelingHandler: Handler[Scope, Nothing, Any, Response] = Handler.fromZIO {
  val someStream: ZStream[Any, Nothing, Byte] = ZStream.from(Seq(1, 2, 3).map(_.toByte))

  someStream
    .peel(ZSink.take[Byte](1))
    .map { peeled =>
      Response(body = Body.fromStream(peeled._2))
    }
}

Here we require a Scope for the handler. But we cannot ZIO.scoped within the handler as it would break the stream returned as a response. The other option is to have a higher level Scope that would not be per-request with instead the risk of leaking resources.

Expected behaviour
A proper way to have a Handler provide a Scope for the request such as Handler.scoped { } to cover these cases.

@jgranstrom jgranstrom added the bug Something isn't working label Nov 5, 2024
@jdegoes
Copy link
Member

jdegoes commented Nov 9, 2024

What scope do you want? You want the scope to be closed when the handler is done being used by the server?

@jdegoes
Copy link
Member

jdegoes commented Nov 9, 2024

I think the correct solution here is:

  1. Define handlers to be Handler[R & Scope, ...] inside routes. This means that in addition to the user's own R, the server can supply a Scope, which is defined to be the scope of the handler.
  2. Have the server supply a scope for the entire request/response cycle, which is fed into the handler.

To avoid breaking backward compatibility and avoid introducing overhead for "scope-less" handlers, this would have to be done very carefully using new methods / constructors.

@jdegoes
Copy link
Member

jdegoes commented Nov 9, 2024

/bounty $500

Copy link

algora-pbc bot commented Nov 9, 2024

💎 $500 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3197 with your implementation plan
  2. Submit work: Create a pull request including /claim #3197 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🔴 @987Nabil Nov 9, 2024, 2:24:54 AM WIP
🔴 @Ayush9026 Nov 9, 2024, 7:32:51 PM WIP
🟢 @jgranstrom #3229

@987Nabil
Copy link
Contributor

987Nabil commented Nov 9, 2024

/attempt #3197

Algora profile Completed bounties Tech Active attempts Options
@987Nabil    99 ZIO bounties
+ 4 bounties from 2 projects
Scala
Cancel attempt

@Ayush9026
Copy link

Ayush9026 commented Nov 9, 2024

/attempt #3197

Copy link

algora-pbc bot commented Nov 9, 2024

Note

The user @987Nabil is already attempting to complete issue #3197 and claim the bounty. We recommend checking in on @987Nabil's progress, and potentially collaborating, before starting a new solution.

Copy link

algora-pbc bot commented Nov 16, 2024

@987Nabil: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

Copy link

algora-pbc bot commented Nov 16, 2024

@Ayush9026: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

Copy link

algora-pbc bot commented Nov 23, 2024

The bounty is up for grabs! Everyone is welcome to /attempt #3197 🙌

@jgranstrom
Copy link
Contributor Author

@jdegoes there's quite a bit of complexity that comes with splitting unscoped/scoped handlers. What about we always provide a request-response-lifetimed Scope to handlers? That seems very natural and should simplify it quite a bit. Using any other Scope from outside of handlers within handlers is pretty much a leak. Is there some concern with overhead unless the Scope is needed? I think splitting it will come with overhead in itself too

@jgranstrom
Copy link
Contributor Author

/attempt #3197

Copy link

algora-pbc bot commented Dec 3, 2024

💡 @jgranstrom submitted a pull request that claims the bounty. You can visit your bounty board to reward.

@jgranstrom jgranstrom linked a pull request Dec 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants