Skip to content
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

Migrate actors to use grpc #954

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/itests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ jobs:
install-version: '7.0.x'
env:
NUPKG_OUTDIR: bin/Release/nugets
GOVER: 1.17
GOVER: 1.19
GOOS: linux
GOARCH: amd64
GOPROXY: https://proxy.golang.org
DAPR_CLI_VER: 1.8.0
DAPR_RUNTIME_VER: 1.8.0
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/3dacfb672d55f1436c249057aaebbe597e1066f3/install/install.sh
DAPR_CLI_REF: ''
DAPR_REF: ''
DAPR_REF: '25213183ab8fd20f85f74591ee6ec3a00822adf7'
steps:
- name: Set up Dapr CLI
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}
Expand Down
42 changes: 39 additions & 3 deletions src/Dapr.Actors/Client/ActorProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace Dapr.Actors.Client
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication.Client;

using Autogenerated = Dapr.Client.Autogen.Grpc.v1;
using Grpc.Net.Client;
/// <summary>
/// Represents a factory class to create a proxy to the remote actor objects.
/// </summary>
Expand Down Expand Up @@ -66,7 +67,25 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op
options ??= this.DefaultOptions;

var actorProxy = new ActorProxy();
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
IDaprInteractor daprInteractor;
if (options.UseGrpc) {
var grpcEndpoint = new Uri(options.GrpcEndpoint);
if (grpcEndpoint.Scheme != "http" && grpcEndpoint.Scheme != "https")
{
throw new InvalidOperationException("The gRPC endpoint must use http or https.");
}

if (grpcEndpoint.Scheme.Equals(Uri.UriSchemeHttp))
{
// Set correct switch to maksecure gRPC service calls. This switch must be set before creating the GrpcChannel.
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
var channel = GrpcChannel.ForAddress(options.GrpcEndpoint, options.GrpcChannelOptions);
var client = new Autogenerated.Dapr.DaprClient(channel);
daprInteractor = new DaprGrpcInteractor(channel, client, options.DaprApiToken);
} else {
daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
}
var nonRemotingClient = new ActorNonRemotingClient(daprInteractor);
actorProxy.Initialize(nonRemotingClient, actorId, actorType, options);

Expand All @@ -77,8 +96,25 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op
public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null)
{
options ??= this.DefaultOptions;
IDaprInteractor daprInteractor;
if (options.UseGrpc) {
var grpcEndpoint = new Uri(options.GrpcEndpoint);
if (grpcEndpoint.Scheme != "http" && grpcEndpoint.Scheme != "https")
{
throw new InvalidOperationException("The gRPC endpoint must use http or https.");
}

var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
if (grpcEndpoint.Scheme.Equals(Uri.UriSchemeHttp))
{
// Set correct switch to maksecure gRPC service calls. This switch must be set before creating the GrpcChannel.
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
var channel = GrpcChannel.ForAddress(options.GrpcEndpoint, options.GrpcChannelOptions);
var client = new Autogenerated.Dapr.DaprClient(channel);
daprInteractor = new DaprGrpcInteractor(channel, client, options.DaprApiToken);
} else {
daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
}
var remotingClient = new ActorRemotingClient(daprInteractor);
var proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(actorInterfaceType);
var actorProxy = proxyGenerator.CreateActorProxy();
Expand Down
21 changes: 21 additions & 0 deletions src/Dapr.Actors/Client/ActorProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Dapr.Actors.Client
{
using System;
using System.Text.Json;
using Grpc.Net.Client;

/// <summary>
/// The class containing customizable options for how the Actor Proxy is initialized.
Expand Down Expand Up @@ -58,9 +59,29 @@ public JsonSerializerOptions JsonSerializerOptions
/// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();

/// <summary>
/// Gets or sets the Grpc endpoint URI used to communicate with the Dapr sidecar.
/// </summary>
/// <remarks>
/// The URI endpoint to use for Grpc calls to the Dapr runtime. The default value will be
/// <c>http://127.0.0.1:DAPR_GRPC_PORT</c> where <c>DAPR_GRPC_PORT</c> represents the value of the
/// <c>DAPR_GRPC_PORT</c> environment variable.
/// </remarks>
/// <value></value>
public string GrpcEndpoint { get; set; } = DaprDefaults.GetDefaultGrpcEndpoint();

/// <summary>
/// The timeout allowed for an actor request. Can be set to System.Threading.Timeout.InfiniteTimeSpan to disable any timeouts.
/// </summary>
public TimeSpan? RequestTimeout { get; set; } = null;
/// <summary>
/// Option to use GRPC or HTTP
/// </summary>
public bool UseGrpc { get; set; } = false;

/// <summary>
/// Options for grpc channel
/// </summary>
public GrpcChannelOptions GrpcChannelOptions { get; set; } = new GrpcChannelOptions(){ThrowOperationCanceledOnCancellation = true,};
}
}
Loading