typelevel / typelevel/cats-effect
Make dispatcher safer
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 2.2k
- Forks
- 576
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 18
Description
The current Dispatcher suffers from the following problems :
- algebraic product components of the effect are lost at the point of dispatch (WriterT's log)
- algebraic coproduct components (EitherT, OptionT) of the effect put the dispatch call at risk of never returning.
- self cancellation of dispatched computations prevent the dispatch call from ever returning
This last two points are pretty bad : even if Dispatcher can in theory work against any effect type, in practice it makes it really easy for the user to shoot themselves in the foot.
The problem could be mitigated by requiring the dispatch methods (unsafeRunSomething) to take a callback construct, that'd allow to witness these three above points :
trait DispatchObserver[F[_]] {
def onCancel: F[Unit]
// When the computation returns an algebraic failure and a value
// cannot be produced (for instance OptionT.none)
def onAlgebraicFailure(failed: F[Unit]): F[Unit]
// When the computation returns an algebraic success.
def onAlgebraicSuccess(success: F[Unit]): F[Unit]
}
See a POC here
The incorporation of such a callback would facilitate writing constructs like this one, allowing for communicating cancellation and algebraic information through unsafe regions, in a way that is similar to what cats-effect-cps does today.
A concrete benefit would be a better integration with libraries like https://github.com/sangria-graphql/sangria, which force an unsafe region onto the user.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the dispatch methods described as unsafeRunSomething, then compare the proof of concept in SaferDispatcher.scala with the bridge in DispatchBridge.scala. The desired result is a dispatcher callback that communicates cancellation, algebraic failure, and algebraic success without losing effect information or preventing the dispatch call from returning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100