-
Notifications
You must be signed in to change notification settings - Fork 228
Use DaprChannel instead of closeable ActorProxyBuilder. #451
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f85a84
Use DaprChannel instead of closeable ActorProxyBuilder.
artursouza 55f2388
Refactor Actor channel into DaprChannel and out of ActorProxyBuilder.
artursouza dbc712a
Rename DaprChannel to ActorClient and make it a DaprClient.
artursouza 5d4dfe7
Make ActorClient not implement DaprClient but implement same method.
artursouza 8bc6723
Update sdk-actors/src/main/java/io/dapr/actors/client/ActorProxyBuild…
artursouza 93df429
Update sdk-actors/src/test/java/io/dapr/actors/client/ActorProxyBuild…
artursouza 42669e1
Rename ActorProxyForTestsImpl to ActorProxyImplForTests
artursouza 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
This file contains hidden or 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 hidden or 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
113 changes: 113 additions & 0 deletions
113
sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java
This file contains hidden or 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,113 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.actors.client; | ||
|
|
||
| import io.dapr.client.DaprApiProtocol; | ||
| import io.dapr.client.DaprHttpBuilder; | ||
| import io.dapr.config.Properties; | ||
| import io.dapr.v1.DaprGrpc; | ||
| import io.grpc.Channel; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * Holds a client for Dapr sidecar communication. ActorClient should be reused. | ||
| */ | ||
| public class ActorClient implements AutoCloseable { | ||
|
|
||
| /** | ||
| * gRPC channel for communication with Dapr sidecar. | ||
| */ | ||
| private final ManagedChannel grpcManagedChannel; | ||
|
|
||
| /** | ||
| * Dapr's client. | ||
| */ | ||
| private final DaprClient daprClient; | ||
|
|
||
| /** | ||
| * Instantiates a new channel for Dapr sidecar communication. | ||
| */ | ||
| public ActorClient() { | ||
| this(Properties.API_PROTOCOL.get()); | ||
| } | ||
|
|
||
| /** | ||
| * Instantiates a new channel for Dapr sidecar communication. | ||
| * | ||
| * @param apiProtocol Dapr's API protocol. | ||
| */ | ||
| private ActorClient(DaprApiProtocol apiProtocol) { | ||
| this(apiProtocol, buildManagedChannel(apiProtocol)); | ||
| } | ||
|
|
||
| /** | ||
| * Instantiates a new channel for Dapr sidecar communication. | ||
| * | ||
| * @param apiProtocol Dapr's API protocol. | ||
| */ | ||
| private ActorClient(DaprApiProtocol apiProtocol, ManagedChannel grpcManagedChannel) { | ||
| this.grpcManagedChannel = grpcManagedChannel; | ||
| this.daprClient = buildDaprClient(apiProtocol, grpcManagedChannel); | ||
| } | ||
|
|
||
| /** | ||
| * Invokes an Actor method on Dapr. | ||
| * | ||
| * @param actorType Type of actor. | ||
| * @param actorId Actor Identifier. | ||
| * @param methodName Method name to invoke. | ||
| * @param jsonPayload Serialized body. | ||
| * @return Asynchronous result with the Actor's response. | ||
| */ | ||
| Mono<byte[]> invoke(String actorType, String actorId, String methodName, byte[] jsonPayload) { | ||
| return daprClient.invoke(actorType, actorId, methodName, jsonPayload); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| if (grpcManagedChannel != null && !grpcManagedChannel.isShutdown()) { | ||
| grpcManagedChannel.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a GRPC managed channel (or null, if not applicable). | ||
| * | ||
| * @param apiProtocol Dapr's API protocol. | ||
| * @return GRPC managed channel or null. | ||
| */ | ||
| private static ManagedChannel buildManagedChannel(DaprApiProtocol apiProtocol) { | ||
| if (apiProtocol != DaprApiProtocol.GRPC) { | ||
| return null; | ||
| } | ||
|
|
||
| int port = Properties.GRPC_PORT.get(); | ||
| if (port <= 0) { | ||
| throw new IllegalArgumentException("Invalid port."); | ||
| } | ||
|
|
||
| return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port).usePlaintext().build(); | ||
| } | ||
|
|
||
| /** | ||
| * Build an instance of the Client based on the provided setup. | ||
| * | ||
| * @return an instance of the setup Client | ||
| * @throws java.lang.IllegalStateException if any required field is missing | ||
| */ | ||
| private static DaprClient buildDaprClient(DaprApiProtocol apiProtocol, Channel grpcManagedChannel) { | ||
| switch (apiProtocol) { | ||
| case GRPC: return new DaprGrpcClient(DaprGrpc.newFutureStub(grpcManagedChannel)); | ||
| case HTTP: return new DaprHttpClient(new DaprHttpBuilder().build()); | ||
| default: throw new IllegalStateException("Unsupported protocol: " + apiProtocol.name()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.