user606521
user606521

Reputation: 15454

How to configure request timeout and/or message TTL separately in MassTransit?

My goal is to make request-response from API Gateway to microservice. I want request to be durable (not expiring), but response should be expiring (if no consumer will receive response within let's say 30s then it should expire). I am using RequestClient in API GW request handler (request scope). By default timeout is set by MassTransit to 30s and after this time request is removed from the queue. It's here in MassTransit code: https://github.com/MassTransit/MassTransit/blob/5e2a416384f005c392ead139f5c4af34511c56db/src/MassTransit/RequestTimeout.cs#L7.

First of I tried to set timeout in my request client like that (using RabbitMQ):

// on request client level
cfg.AddRequestClient<ICreateGroupPayload>(RequestTimeout.None);

// and/or

// on .GetResponse() level
var response = await _createGroupClient.GetResponse<ICreateGroupResult>(new
            {
                Name = payload.Name,
                Description = payload.Description
            }, new CancellationToken(), RequestTimeout.None);

But neither of those options work. Still message has default 30s TTL.

  1. Why it does not work as expected? Is message TTL something different here than RequestTimeout? If yes then why its 30s?
  2. Do I have to configure message TTL manually in my case?
  3. I want request client to automatically timeout after 30s even when request message will never expire. Is this built in, or I have to write some custom code for timeout, or maybe it's enough if I will pass expiring CancellationToken to .getResponse()?

Upvotes: 0

Views: 8001

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33457

You can configure a default timeout when registering the request client:

cfg.AddRequestClient<ICreateGroupPayload>(RequestTimeout.After(s:20));

And then, when sending the request, you can override the TimeToLive using a header in the message initializer:

var response = await _createGroupClient.GetResponse<ICreateGroupResult>(new
{
    Name = payload.Name,
    Description = payload.Description,
    __TimeToLive = TimeSpan.FromYears(1)
}, httpContext.CancellationToken);

That will replace the default value (which matches the request timeout, by default) with the new value.

Upvotes: 4

Related Questions