From cee49bff921da70ad9c0e3c1c64af4dff5608a36 Mon Sep 17 00:00:00 2001
From: halspang <70976921+halspang@users.noreply.github.com>
Date: Wed, 8 Sep 2021 19:06:07 +0000
Subject: [PATCH] Add an option to set Timeout for ActorProxy (#748)
* Add an option to set Timeout for ActorProxy
Actors, by there nature, may have to wait for a long period before
being allowed to execute. This could lead to long request times. The
default timeout for the HttpClient being used is fairly low. This
commit allows for the timeout to be set when constructing the
proxy.
https://github.com/dapr/dotnet-sdk/issues/728
* Removed extra constructor and e2e tests.
---
src/Dapr.Actors/Client/ActorProxyFactory.cs | 4 ++--
src/Dapr.Actors/Client/ActorProxyOptions.cs | 5 +++++
src/Dapr.Actors/DaprHttpInteractor.cs | 4 +++-
src/Dapr.Actors/Runtime/ActorRuntime.cs | 2 +-
test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs | 2 +-
test/Shared/TestClient.cs | 2 +-
6 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/src/Dapr.Actors/Client/ActorProxyFactory.cs b/src/Dapr.Actors/Client/ActorProxyFactory.cs
index 91c5e752..95bccf93 100644
--- a/src/Dapr.Actors/Client/ActorProxyFactory.cs
+++ b/src/Dapr.Actors/Client/ActorProxyFactory.cs
@@ -58,7 +58,7 @@ namespace Dapr.Actors.Client
options ??= this.DefaultOptions;
var actorProxy = new ActorProxy();
- var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken);
+ var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
var nonRemotingClient = new ActorNonRemotingClient(daprInteractor);
actorProxy.Initialize(nonRemotingClient, actorId, actorType, options);
@@ -70,7 +70,7 @@ namespace Dapr.Actors.Client
{
options ??= this.DefaultOptions;
- var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken);
+ var 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();
diff --git a/src/Dapr.Actors/Client/ActorProxyOptions.cs b/src/Dapr.Actors/Client/ActorProxyOptions.cs
index f64e79da..e3887700 100644
--- a/src/Dapr.Actors/Client/ActorProxyOptions.cs
+++ b/src/Dapr.Actors/Client/ActorProxyOptions.cs
@@ -49,5 +49,10 @@ namespace Dapr.Actors.Client
///
///
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
+
+ ///
+ /// The timeout allowed for an actor request. Can be set to System.Threading.Timeout.InfiniteTimeSpan to disable any timeouts.
+ ///
+ public TimeSpan? RequestTimeout { get; set; } = null;
}
}
diff --git a/src/Dapr.Actors/DaprHttpInteractor.cs b/src/Dapr.Actors/DaprHttpInteractor.cs
index 51f72d25..4229df97 100644
--- a/src/Dapr.Actors/DaprHttpInteractor.cs
+++ b/src/Dapr.Actors/DaprHttpInteractor.cs
@@ -36,12 +36,14 @@ namespace Dapr.Actors
public DaprHttpInteractor(
HttpMessageHandler clientHandler,
string httpEndpoint,
- string apiToken)
+ string apiToken,
+ TimeSpan? requestTimeout)
{
this.handler = clientHandler ?? defaultHandler;
this.httpEndpoint = httpEndpoint;
this.daprApiToken = apiToken;
this.httpClient = this.CreateHttpClient();
+ this.httpClient.Timeout = requestTimeout ?? this.httpClient.Timeout;
}
public async Task GetStateAsync(string actorType, string actorId, string keyName, CancellationToken cancellationToken = default)
diff --git a/src/Dapr.Actors/Runtime/ActorRuntime.cs b/src/Dapr.Actors/Runtime/ActorRuntime.cs
index ea049240..5b790db6 100644
--- a/src/Dapr.Actors/Runtime/ActorRuntime.cs
+++ b/src/Dapr.Actors/Runtime/ActorRuntime.cs
@@ -41,7 +41,7 @@ namespace Dapr.Actors.Runtime
// Revisit this if actor initialization becomes a significant source of delay for large projects.
foreach (var actor in options.Actors)
{
- var daprInteractor = new DaprHttpInteractor(clientHandler: null, httpEndpoint: options.HttpEndpoint, apiToken: options.DaprApiToken);
+ var daprInteractor = new DaprHttpInteractor(clientHandler: null, httpEndpoint: options.HttpEndpoint, apiToken: options.DaprApiToken, requestTimeout: null);
this.actorManagers[actor.Type.ActorTypeName] = new ActorManager(
actor,
actor.Activator ?? this.activatorFactory.CreateActivator(actor.Type),
diff --git a/test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs b/test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs
index a4708d7e..3a289ba9 100644
--- a/test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs
+++ b/test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs
@@ -17,7 +17,7 @@ namespace Dapr.Actors.Runtime
private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
{
var registration = new ActorRegistration(ActorTypeInformation.Get(type));
- var interactor = new DaprHttpInteractor(clientHandler: null, "http://localhost:3500", apiToken: null);
+ var interactor = new DaprHttpInteractor(clientHandler: null, "http://localhost:3500", apiToken: null, requestTimeout: null);
return new ActorManager(registration, activator ?? new DefaultActorActivator(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory, interactor);
}
diff --git a/test/Shared/TestClient.cs b/test/Shared/TestClient.cs
index ce138536..e18a0600 100644
--- a/test/Shared/TestClient.cs
+++ b/test/Shared/TestClient.cs
@@ -30,7 +30,7 @@ namespace Dapr
internal static TestClient CreateForDaprHttpInterator(string? apiToken = null)
{
var handler = new CapturingHandler();
- return new TestClient(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken), handler);
+ return new TestClient(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken, null), handler);
}
#endif