Skip to content

Commit

Permalink
Switch to io.grpc.Grpc.* factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk committed Aug 14, 2024
1 parent 35c67b2 commit 1120746
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ lazy val server =

## Creating a client

A `ManagedChannel` is the type used by `grpc-java` to manage a connection to a particular server. This library provides syntax for `ManagedChannelBuilder` which creates a `Resource` which can manage the shutdown of the channel, by calling `.resource[F]` where `F` has an instance of the `Sync` typeclass. This implementation will do a drain of the channel, and attempt to shut down the channel, forcefully closing after 30 seconds. An example of the syntax using `grpc-netty` is:
A `ManagedChannel` is the type used by `grpc-java` to manage a connection to a particular server. This library provides syntax for `ManagedChannelBuilder` which creates a `Resource` which can manage the shutdown of the channel, by calling `.resource[F]` where `F` has an instance of the `Sync` typeclass. This implementation will do a drain of the channel, and attempt to shut down the channel, forcefully closing after 30 seconds. An example of the syntax is:

```scala
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder
import io.grpc._
import fs2.grpc.syntax.all._

val managedChannelResource: Resource[IO, ManagedChannel] =
NettyChannelBuilder
.forAddress("127.0.0.1", 9999)
Grpc.newChannelBuilderForAddress("127.0.0.1", 9999, InsecureChannelCredentials.create())
.resource[IO]
```

Expand All @@ -84,14 +83,14 @@ The generated code provides a method `bindServiceResource[F]`, for any `F` which
A `Server` is the type used by `grpc-java` to manage the server connections and lifecycle. This library provides syntax for `ServerBuilder`, which mirrors the pattern for the client. An example is:

```scala
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder
import io.grpc._
import fs2.grpc.syntax.all._

val helloService: Resource[IO, ServerServiceDefinition] =
MyFs2Grpc.bindServiceResource[IO](new MyImpl())

def run(service: ServerServiceDefinition) = NettyServerBuilder
.forPort(9999)
def run(service: ServerServiceDefinition) = Grpc
.newServerBuilderForPort(9999, InsecureServerCredentials.create())
.addService(service)
.resource[IO]
.evalMap(server => IO(server.start()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import cats.effect._
import io.grpc.{ManagedChannel, ManagedChannelBuilder}

trait ManagedChannelBuilderSyntax {
implicit final def fs2GrpcSyntaxManagedChannelBuilder[MCB <: ManagedChannelBuilder[MCB]](
builder: MCB
): ManagedChannelBuilderOps[MCB] =
new ManagedChannelBuilderOps[MCB](builder)
implicit final def fs2GrpcSyntaxManagedChannelBuilder(
builder: ManagedChannelBuilder[?]
): ManagedChannelBuilderOps =
new ManagedChannelBuilderOps(builder)
}

final class ManagedChannelBuilderOps[MCB <: ManagedChannelBuilder[MCB]](val builder: MCB) extends AnyVal {
final class ManagedChannelBuilderOps(val builder: ManagedChannelBuilder[?]) extends AnyVal {

/** Builds a `ManagedChannel` into a resource. The managed channel is shut down when the resource is released.
* Shutdown is as follows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import cats.effect._
import io.grpc.{Server, ServerBuilder}

trait ServerBuilderSyntax {
implicit final def fs2GrpcSyntaxServerBuilder[SB <: ServerBuilder[SB]](builder: SB): ServerBuilderOps[SB] =
new ServerBuilderOps[SB](builder)
implicit final def fs2GrpcSyntaxServerBuilder(builder: ServerBuilder[?]): ServerBuilderOps =
new ServerBuilderOps(builder)
}

final class ServerBuilderOps[SB <: ServerBuilder[SB]](val builder: SB) extends AnyVal {
final class ServerBuilderOps(val builder: ServerBuilder[?]) extends AnyVal {

/** Builds a `Server` into a resource. The server is shut down when the resource is released. Shutdown is as follows:
*
Expand Down
10 changes: 7 additions & 3 deletions runtime/src/test/scala/fs2/grpc/client/ClientSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,15 @@ class ClientSuite extends Fs2GrpcSuite {

}

runTest0("resource awaits termination of managed channel") { (tc, io, _) =>
runTest0("resource awaits termination of managed channel") { (tc, r, _) =>
import fs2.grpc.syntax.all._
import netty.shaded.io.grpc.netty.NettyChannelBuilder
import io.grpc._

val result = NettyChannelBuilder.forAddress("127.0.0.1", 0).resource[IO].use(IO.pure).unsafeToFuture()(io)
val result = Grpc
.newChannelBuilderForAddress("127.0.0.1", 0, InsecureChannelCredentials.create())
.resource[IO]
.use(IO.pure)
.unsafeToFuture()(r)

tc.tick()

Expand Down
8 changes: 6 additions & 2 deletions runtime/src/test/scala/fs2/grpc/server/ServerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ class ServerSuite extends Fs2GrpcSuite {

runTest0("resource awaits termination of server") { (tc, r, _) =>
import fs2.grpc.syntax.all._
import netty.shaded.io.grpc.netty.NettyServerBuilder
import io.grpc._

val result = NettyServerBuilder.forPort(0).resource[IO].use(IO.pure).unsafeToFuture()(r)
val result = Grpc
.newServerBuilderForPort(0, InsecureServerCredentials.create())
.resource[IO]
.use(IO.pure)
.unsafeToFuture()(r)
tc.tick()

val server = result.value.get.get
Expand Down

0 comments on commit 1120746

Please sign in to comment.