Skip to content

Timeouts and Cancellation

Damian Turczynski edited this page Oct 24, 2017 · 1 revision

Every command supports cancellation using a CancellationToken.

Cancellation is cooperative. Mjolnir will not terminate/abort threads when the timeout is reached. Instead, it relies on implementations to use the CancellationToken or pass it through to things that support it (e.g. network operations). Command's Execute(CancellationToken token) and ExecuteAsync(CancellationToken token) receive the token as an argument for this reason.

Timeout Precedence

There are several places a timeout can be set. Here's the order they're used if set.

  • Invocation Timeout or Invocation CancellationToken

    This is passed into the Invoke method when invoking the Command. Example:

    var result = await invoker.InvokeReturnAsync(command, 2000); // Timeout = 2000ms

    Another overload is available that takes a CancellationToken instead of a long timeout.

  • Configured Timeout

    A timeout can be configured for each command (see Installing and Configuring and Configuration).

     MjolnirConfiguration.CommandConfigurations["s3.S3FileExistsAsyncCommand"].Timeout=3000
    

    Command configurations are per-key. The third and fourth components of the configuration key are the Command Name.

    Note: The configuration key changed in version 2.6.0. In prior versions, the configuration key was command.<group-name>.<class-name>.Timeout; version 2.6.0 added mjolnir. to the beginning of the string. Implementations using the older (pre-2.6.0) Command base class will still use the old config, but if you're porting Commands over to the new 2.6.0+ SyncCommand/AsyncCommand base classes, you'll need to update your configured timeouts.

  • Constructor Timeout

    The value passed as a constructor argument to the base SyncCommand or AsyncCommand class. For example:

    class S3FileExistsAsyncCommand : BaseAsyncCommand<bool>
    
        public S3FileExistsAsyncCommand(IS3AsyncClient client, string bucketName, string fileName)
            : base("s3", "s3-read", TimeSpan.FromSeconds(5)) // 5-second constructor timeout
        {
            ...
  • Global Default Timeout

    If none of the timeouts above are set or valid, the default timeout of 2000 milliseconds is used.


« CommandsBulkheads »