-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: gRPC endpoint support #161
Merged
+326
−13
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4eb8068
feat: gRPC endpoint support
johanandren b1638f5
sample using it
johanandren 1ac3045
stuff
johanandren 6ca32bc
runtime 1.3.1-6cd7992 bump
johanandren 57f7640
fromaging
johanandren 6b09e71
bump akka-grpc version
johanandren 60d548f
Extra note about aligning akka-grpc version
johanandren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...essor-tests/rest-endpoints-descriptors/src/main/java/com/example/UserGrpcServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package com.example; | ||
|
||
import akka.javasdk.annotations.GrpcEndpoint; | ||
|
||
@GrpcEndpoint | ||
public class UserGrpcServiceImpl { | ||
// would extend generated service stub and implement grpc methods but that's not important for this test | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
akka-javasdk-tests/src/test/java/akkajavasdk/components/grpc/TestGrpcServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akkajavasdk.components.grpc; | ||
|
||
import akka.javasdk.annotations.GrpcEndpoint; | ||
import akkajavasdk.protocol.*; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
@GrpcEndpoint | ||
public class TestGrpcServiceImpl implements TestGrpcService { | ||
@Override | ||
public CompletionStage<TestGrpcServiceOuterClass.Out> simple(TestGrpcServiceOuterClass.In in) { | ||
return CompletableFuture.completedFuture( | ||
TestGrpcServiceOuterClass.Out.newBuilder().setData(in.getData()).build() | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
akka-javasdk-tests/src/test/protobuf/akkajavasdk/test_grpc_service.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
|
||
// gRPC interface for a gRPC endpoint component | ||
|
||
syntax = "proto3"; | ||
|
||
package akkajavasdk; | ||
|
||
option java_package = "akkajavasdk.protocol"; | ||
|
||
message In { | ||
string data = 1; | ||
} | ||
|
||
message Out { | ||
string data = 1; | ||
} | ||
|
||
service TestGrpcService { | ||
rpc Simple(In) returns (Out) {}; | ||
} |
27 changes: 27 additions & 0 deletions
27
akka-javasdk/src/main/java/akka/javasdk/annotations/GrpcEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.javasdk.annotations; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Mark a class to be made available as a gRPC endpoint. The annotated class should extend a gRPC service interface | ||
* generated using Akka gRPC, be public and have a public constructor. | ||
* <p> | ||
* Annotated classes can accept the following types to the constructor: | ||
* <ul> | ||
* <li>{@link akka.javasdk.client.ComponentClient}</li> | ||
* <li>{@link akka.javasdk.http.HttpClientProvider}</li> | ||
* <li>{@link akka.javasdk.timer.TimerScheduler}</li> | ||
* <li>{@link akka.stream.Materializer}</li> | ||
* <li>{@link com.typesafe.config.Config}</li> | ||
* <li>Custom types provided by a {@link akka.javasdk.DependencyProvider} from the service setup</li> | ||
* </ul> | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface GrpcEndpoint { | ||
} |
65 changes: 65 additions & 0 deletions
65
akka-javasdk/src/main/scala/akka/javasdk/impl/GrpcEndpointDescriptorFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.javasdk.impl | ||
|
||
import akka.actor.typed.ActorSystem | ||
import akka.grpc.ServiceDescription | ||
import akka.grpc.scaladsl.InstancePerRequestFactory | ||
import akka.http.scaladsl.model.HttpRequest | ||
import akka.http.scaladsl.model.HttpResponse | ||
import akka.runtime.sdk.spi.GrpcEndpointDescriptor | ||
import akka.runtime.sdk.spi.GrpcEndpointRequestConstructionContext | ||
|
||
import scala.concurrent.Future | ||
|
||
object GrpcEndpointDescriptorFactory { | ||
|
||
def apply[T](grpcEndpointClass: Class[T], factory: () => T)(implicit | ||
system: ActorSystem[_]): GrpcEndpointDescriptor[T] = { | ||
// FIXME now way right now to know that it is a gRPC service interface | ||
val serviceDefinitionClass: Class[_] = { | ||
val interfaces = grpcEndpointClass.getInterfaces | ||
if (interfaces.length != 1) { | ||
throw new IllegalArgumentException( | ||
s"Class [${grpcEndpointClass.getName}] must implement exactly one interface, the gRPC service generated by Akka gRPC.") | ||
} | ||
interfaces(0) | ||
} | ||
|
||
// FIXME a derivative should be injectable into user code as well | ||
val instanceFactory = { (_: GrpcEndpointRequestConstructionContext) => | ||
factory() | ||
} | ||
|
||
val handlerFactory = | ||
system.dynamicAccess | ||
.createInstanceFor[InstancePerRequestFactory[T]](serviceDefinitionClass.getName + "ScalaHandlerFactory", Nil) | ||
.get | ||
|
||
// Pick up generated companion object for file descriptor (for reflection) and creating router | ||
// static akka.grpc.ServiceDescription description in generated service interface | ||
val description = serviceDefinitionClass.getField("description").get(null).asInstanceOf[ServiceDescription] | ||
if (description eq null) | ||
throw new RuntimeException( | ||
s"Could not access static description from gRPC service interface [${serviceDefinitionClass.getName}]") | ||
|
||
val routeFactory: (HttpRequest => T) => PartialFunction[HttpRequest, Future[HttpResponse]] = { serviceFactory => | ||
handlerFactory.partialInstancePerRequest( | ||
serviceFactory, | ||
description.name, | ||
// FIXME default error handler, is it fine to leave like this, should runtime define? | ||
PartialFunction.empty, | ||
system) | ||
} | ||
|
||
new GrpcEndpointDescriptor[T]( | ||
grpcEndpointClass.getName, | ||
description.name, | ||
description.descriptor, | ||
instanceFactory, | ||
routeFactory) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will certainly need to revisit this magic section later...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the "integrate two generated code blocks with logic in the runtime" part is a bit tricky...