Skip to content

Commit

Permalink
Add an option to set Timeout for ActorProxy (#748)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
halspang authored Sep 8, 2021
1 parent aba49d1 commit cee49bf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Dapr.Actors/Client/ActorProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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);
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
var nonRemotingClient = new ActorNonRemotingClient(daprInteractor);
actorProxy.Initialize(nonRemotingClient, actorId, actorType, options);

Expand All @@ -70,7 +70,7 @@ public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string
{
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();
Expand Down
5 changes: 5 additions & 0 deletions src/Dapr.Actors/Client/ActorProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ public JsonSerializerOptions JsonSerializerOptions
/// </remarks>
/// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();

/// <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;
}
}
4 changes: 3 additions & 1 deletion src/Dapr.Actors/DaprHttpInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ internal class DaprHttpInteractor : IDaprInteractor
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<string> GetStateAsync(string actorType, string actorId, string keyName, CancellationToken cancellationToken = default)
Expand Down
2 changes: 1 addition & 1 deletion src/Dapr.Actors/Runtime/ActorRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal ActorRuntime(ActorRuntimeOptions options, ILoggerFactory loggerFactory,
// 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),
Expand Down
2 changes: 1 addition & 1 deletion test/Dapr.Actors.Test/Runtime/ActorManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class ActorManagerTests
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);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Shared/TestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class TestClient
internal static TestClient<DaprHttpInteractor> CreateForDaprHttpInterator(string? apiToken = null)
{
var handler = new CapturingHandler();
return new TestClient<DaprHttpInteractor>(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken), handler);
return new TestClient<DaprHttpInteractor>(new DaprHttpInteractor(handler, "http://localhost:3500", apiToken, null), handler);
}
#endif

Expand Down

0 comments on commit cee49bf

Please sign in to comment.