From 1d9fa2c0e14440f9159fd07b2b35df5ce57c9933 Mon Sep 17 00:00:00 2001 From: Arnaud Burlet Date: Thu, 21 Mar 2024 12:22:08 +0100 Subject: [PATCH] Block in F using F.blocking instead of scala.concurrent.blocking --- .../grpc/syntax/ManagedChannelBuilderSyntax.scala | 14 ++++++-------- .../fs2/grpc/syntax/ServerBuilderSyntax.scala | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/runtime/src/main/scala/fs2/grpc/syntax/ManagedChannelBuilderSyntax.scala b/runtime/src/main/scala/fs2/grpc/syntax/ManagedChannelBuilderSyntax.scala index 95f858b5..b2341793 100644 --- a/runtime/src/main/scala/fs2/grpc/syntax/ManagedChannelBuilderSyntax.scala +++ b/runtime/src/main/scala/fs2/grpc/syntax/ManagedChannelBuilderSyntax.scala @@ -24,8 +24,8 @@ package grpc package syntax import java.util.concurrent.TimeUnit -import scala.concurrent._ import cats.effect._ +import cats.syntax.all._ import io.grpc.{ManagedChannel, ManagedChannelBuilder} trait ManagedChannelBuilderSyntax { @@ -48,13 +48,11 @@ final class ManagedChannelBuilderOps[MCB <: ManagedChannelBuilder[MCB]](val buil */ def resource[F[_]](implicit F: Sync[F]): Resource[F, ManagedChannel] = resourceWithShutdown { ch => - F.delay { - ch.shutdown() - if (!blocking(ch.awaitTermination(30, TimeUnit.SECONDS))) { - ch.shutdownNow() - () - } - } + for { + _ <- F.delay(ch.shutdown()) + terminated <- F.interruptible(ch.awaitTermination(30, TimeUnit.SECONDS)) + _ <- F.unlessA(terminated)(F.delay(ch.shutdownNow())) + } yield (()) } /** Builds a `ManagedChannel` into a resource. The managed channel is shut down when the resource is released. diff --git a/runtime/src/main/scala/fs2/grpc/syntax/ServerBuilderSyntax.scala b/runtime/src/main/scala/fs2/grpc/syntax/ServerBuilderSyntax.scala index 208fdc6c..ce16145a 100644 --- a/runtime/src/main/scala/fs2/grpc/syntax/ServerBuilderSyntax.scala +++ b/runtime/src/main/scala/fs2/grpc/syntax/ServerBuilderSyntax.scala @@ -24,8 +24,8 @@ package grpc package syntax import java.util.concurrent.TimeUnit -import scala.concurrent._ import cats.effect._ +import cats.syntax.all._ import io.grpc.{Server, ServerBuilder} trait ServerBuilderSyntax { @@ -45,13 +45,11 @@ final class ServerBuilderOps[SB <: ServerBuilder[SB]](val builder: SB) extends A */ def resource[F[_]](implicit F: Sync[F]): Resource[F, Server] = resourceWithShutdown { server => - F.delay { - server.shutdown() - if (!blocking(server.awaitTermination(30, TimeUnit.SECONDS))) { - server.shutdownNow() - () - } - } + for { + _ <- F.delay(server.shutdown()) + terminated <- F.interruptible(server.awaitTermination(30, TimeUnit.SECONDS)) + _ <- F.unlessA(terminated)(F.delay(server.shutdownNow())) + } yield (()) } /** Builds a `Server` into a resource. The server is shut down when the resource is released.