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.
This commit is contained in:
halspang 2021-09-08 19:06:07 +00:00 committed by GitHub
parent aba49d1446
commit cee49bff92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 6 deletions

View File

@ -58,7 +58,7 @@ namespace Dapr.Actors.Client
options ??= this.DefaultOptions; options ??= this.DefaultOptions;
var actorProxy = new ActorProxy(); 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); var nonRemotingClient = new ActorNonRemotingClient(daprInteractor);
actorProxy.Initialize(nonRemotingClient, actorId, actorType, options); actorProxy.Initialize(nonRemotingClient, actorId, actorType, options);
@ -70,7 +70,7 @@ namespace Dapr.Actors.Client
{ {
options ??= this.DefaultOptions; 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 remotingClient = new ActorRemotingClient(daprInteractor);
var proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(actorInterfaceType); var proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(actorInterfaceType);
var actorProxy = proxyGenerator.CreateActorProxy(); var actorProxy = proxyGenerator.CreateActorProxy();

View File

@ -49,5 +49,10 @@ namespace Dapr.Actors.Client
/// </remarks> /// </remarks>
/// <value></value> /// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint(); 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;
} }
} }

View File

@ -36,12 +36,14 @@ namespace Dapr.Actors
public DaprHttpInteractor( public DaprHttpInteractor(
HttpMessageHandler clientHandler, HttpMessageHandler clientHandler,
string httpEndpoint, string httpEndpoint,
string apiToken) string apiToken,
TimeSpan? requestTimeout)
{ {
this.handler = clientHandler ?? defaultHandler; this.handler = clientHandler ?? defaultHandler;
this.httpEndpoint = httpEndpoint; this.httpEndpoint = httpEndpoint;
this.daprApiToken = apiToken; this.daprApiToken = apiToken;
this.httpClient = this.CreateHttpClient(); 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) public async Task<string> GetStateAsync(string actorType, string actorId, string keyName, CancellationToken cancellationToken = default)

View File

@ -41,7 +41,7 @@ namespace Dapr.Actors.Runtime
// Revisit this if actor initialization becomes a significant source of delay for large projects. // Revisit this if actor initialization becomes a significant source of delay for large projects.
foreach (var actor in options.Actors) 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( this.actorManagers[actor.Type.ActorTypeName] = new ActorManager(
actor, actor,
actor.Activator ?? this.activatorFactory.CreateActivator(actor.Type), actor.Activator ?? this.activatorFactory.CreateActivator(actor.Type),

View File

@ -17,7 +17,7 @@ namespace Dapr.Actors.Runtime
private ActorManager CreateActorManager(Type type, ActorActivator activator = null) private ActorManager CreateActorManager(Type type, ActorActivator activator = null)
{ {
var registration = new ActorRegistration(ActorTypeInformation.Get(type)); 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); return new ActorManager(registration, activator ?? new DefaultActorActivator(), JsonSerializerDefaults.Web, NullLoggerFactory.Instance, ActorProxy.DefaultProxyFactory, interactor);
} }

View File

@ -30,7 +30,7 @@ namespace Dapr
internal static TestClient<DaprHttpInteractor> CreateForDaprHttpInterator(string? apiToken = null) internal static TestClient<DaprHttpInteractor> CreateForDaprHttpInterator(string? apiToken = null)
{ {
var handler = new CapturingHandler(); 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 #endif