typelevel / typelevel/fs2

Emit a last stream on cancel

Open
#3,636 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.5k
Forks
636
Avg merge
2d 4h
Merged PRs (30d)
7

Description

I have a grpc use case with a stream that go to the server let call it input and a stream that come from the server let call it out put.

The protocol want to send a cancel event to the server when cancelling the process, to signifty to the the server that the process finish.

both stream are run like this

output.concurrently(input)

So when client don't want to consume anymore he can interupt the output stream. This will then interput the input stream.
But I currently don't find a solution to send a last element in the input when the is interuption.
I tryied to have a defered that would be the promise of the last element of the stream.
defered that would be completed in a finialize on the output, but this does not works.

val output = fs2.Stream.eval(Deferred[IO, Packet]).flatMap { finalizingPacket =>
     val input  = input0 ++ fs2.Stream.eval(finalizingPacket.get)
    serverCalls(input) // internally do concurrently
    .onFinalizeCase[IO] {
            case ExitCase.Completed =>              
              finalizingPacket.complete(endOfSteam).start.void
            case ExitCase.Error(e) =>
              finalizingPacket.complete(cancelPacket).start.void
            case ExitCase.Canceled =>
              finalizingPacket.complete(cancelPacket).start.void
          }
}

In this case the finalizingPacket.get is interupted so there is not outcome.

something like .onComplete which is call even in case of interuption would be nice for this case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided output.concurrently(input) example and the onFinalizeCase handling for ExitCase.Canceled. Investigate how cancellation interrupts finalizingPacket.get and how stream finalization is specified. Done means the requested final input element can be delivered when the output stream is interrupted, with behavior covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
grpc, scala
Domain
backend, stream-processing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.