Skip to content

Timeouts and Cancellation

Chris Lewis edited this page Oct 25, 2017 · 14 revisions

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: Configuration changed in version 3.0.0, if you're using a prior version of Mjolnir see the documentation Version-2-Configuration-Keys.

  • Constructor Timeout

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

    class S3FileExistsAsyncCommand : AsyncCommand<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 MjolnirConfiguration.DefaultCommandConfiguration.Timeout is used. If you haven't configured this value yourself, the default value used by the library will be 2000ms.


« CommandsBulkheads »