Skip to content

Commit cee49bf

Browse files
authored
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. #728 * Removed extra constructor and e2e tests.
1 parent aba49d1 commit cee49bf

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

src/Dapr.Actors/Client/ActorProxyFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op
5858
options ??= this.DefaultOptions;
5959

6060
var actorProxy = new ActorProxy();
61-
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken);
61+
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
6262
var nonRemotingClient = new ActorNonRemotingClient(daprInteractor);
6363
actorProxy.Initialize(nonRemotingClient, actorId, actorType, options);
6464

@@ -70,7 +70,7 @@ public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string
7070
{
7171
options ??= this.DefaultOptions;
7272

73-
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken);
73+
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
7474
var remotingClient = new ActorRemotingClient(daprInteractor);
7575
var proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(actorInterfaceType);
7676
var actorProxy = proxyGenerator.CreateActorProxy();

src/Dapr.Actors/Client/ActorProxyOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ public JsonSerializerOptions JsonSerializerOptions
4949
/// </remarks>
5050
/// <value></value>
5151
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
52+
53+
/// <summary>
54+
/// The timeout allowed for an actor request. Can be set to System.Threading.Timeout.InfiniteTimeSpan to disable any timeouts.
55+
/// </summary>
56+
public TimeSpan? RequestTimeout { get; set; } = null;
5257
}
5358
}

src/Dapr.Actors/DaprHttpInteractor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ internal class DaprHttpInteractor : IDaprInteractor
3636
public DaprHttpInteractor(
3737
HttpMessageHandler clientHandler,
3838
string httpEndpoint,
39-
string apiToken)
39+
string apiToken,
40+
TimeSpan? requestTimeout)
4041
{
4142
this.handler = clientHandler ?? defaultHandler;
4243
this.httpEndpoint = httpEndpoint;
4344
this.daprApiToken = apiToken;
4445
this.httpClient = this.CreateHttpClient();
46+
this.httpClient.Timeout = requestTimeout ?? this.httpClient.Timeout;
4547
}
4648

4749
public async Task<string> GetStateAsync(string actorType, string actorId, string keyName, CancellationToken cancellationToken = default)

src/Dapr.Actors/Runtime/ActorRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal ActorRuntime(ActorRuntimeOptions options, ILoggerFactory loggerFactory,
4141
// Revisit this if actor initialization becomes a significant source of delay for large projects.
4242
foreach (var actor in options.Actors)
4343
{
44-
var daprInteractor = new DaprHttpInteractor(clientHandler: null, httpEndpoint: options.HttpEndpoint, apiToken: options.DaprApiToken);
44+
var daprInteractor = new DaprHttpInteractor(clientHandler: null, httpEndpoint: options.HttpEndpoint, apiToken: options.DaprApiToken, requestTimeout: null);
4545
this.actorManagers[actor.Type.ActorTypeName] = new ActorManager(
4646
actor,
4747
actor.Activator ?? this.activatorFactory.CreateActivator(actor.Type),

test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class ActorManagerTests
1717
private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
1818
{
1919
var registration = new ActorRegistration(ActorTypeInformation.Get(type));
20-
var interactor = new DaprHttpInteractor(clientHandler: null, "http://localhost:3500", apiToken: null);
20+
var interactor = new DaprHttpInteractor(clientHandler: null, "http://localhost:3500", apiToken: null, requestTimeout: null);
2121
return new ActorManager(registration, activator ?? new DefaultActorActivator(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory, interactor);
2222
}
2323

test/Shared/TestClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class TestClient
3030
internal static TestClient<DaprHttpInteractor> CreateForDaprHttpInterator(string? apiToken = null)
3131
{
3232
var handler = new CapturingHandler();
33-
return new TestClient<DaprHttpInteractor>(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken), handler);
33+
return new TestClient<DaprHttpInteractor>(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken, null), handler);
3434
}
3535
#endif
3636

0 commit comments

Comments
 (0)