Search code examples
grpcziozio-streams

zio-grpc bi-stream NOT close in server-side after closing grpcurl by `Ctrl-C`


Consulting about zio-grpc bi-stream closing invoke: When it will be closed? I'm use grpcurl to test bistream, but zio-grpc server side not close immidently(it will closed after some time).

I'm watching grpc server-side stream close event by Stream.ensuring. For detail:

  1. for request stream: consume request stream in a fiber by forkDomaen: I'm suppose the grpc request stream will be closed if the stream closed.
request
    .mapM { reqItem =>
        // do action here
        UIO(println(s"test get some data from request item: ${reqItem}"))
    }
    .runDrain
    .catchAll(error => ZIO(println(s"find some error: $error")))
    .ensuring {
        UIO(println(s"request stream closed"))
    }
    .forkDaemon
  1. for server-side response stream: I'm suppose the grpc response stream will be closed if I'm close the created response Stream instance.
    ZStream.fromEffect {
      Queue.unbounded[String].flatMap { queue =>
            ZStream.fromQueue(queue)
      }
    }.flatten
    .ensuring {UIO(println("response stream closed"))}

The code works well to handle request and response , besides, it will invoke some other business logic in ensuring but ignored here for simplify. Questions:

  1. Is't the best practice to handle bi-stream closed action by ZStream.ensuring with zio-grpc?
  2. Is't by design on zio-grpc to lantancy close the stream even though client-side close the stream? In this situation is grpcurl closed by Ctrl-C which I have noticed the underlaying TCP is closed normally by check FIN req-rsp. Thanks.

Solution

  • For furthermore test, grpcurl closed by Ctrl-C will invoking server-side response stream closed firstly and immediately.

    As for server-side request stream, not invoked, even though 5 minutes later.

    I'm guess request stream lifecycle handle by client because it's beyond underlying HTTP2/TCP connection in gRPC level view.

    It's should be some best practise in normal case:

    1. close request stream fiber resource in server-side if reponse stream closing invoked
    2. server-side should config grpc stream auto closed, especially for request stream, if not received heart beat or ping-pang like protocol in gRPC level to release potential resource handled by gRPC library which is zio-grpc in this case.