I've been searching for an answer for several days now to find out why my C# MassTransit configuration is using so much CPU when it is idle.
I found very few responses to other users asking this same question, and mostly they state "you must be doing something wrong". Problem is, I've followed the examples on the MassTransit website and even done a bare bones project and still see it using 30+% cpu when it is just idle.
Is there something that can be done to make it use 0-10% when idle? In our environment running a process that will consume 30+% cpu at idle is significant.
Here is an example of how I have MT configured in my dotnet 7 app:
.ConfigureServices((host, services) =>
{
IAgentInformation agentInformation = new AgentInformation(host.Configuration);
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
var entryAssembly = Assembly.GetEntryAssembly();
x.AddConsumer<AgentConsumer>();
x.AddSagaStateMachines(entryAssembly);
x.AddSagas(entryAssembly);
x.AddActivities(entryAssembly);
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(agentInformation.ATLASServer, "/", h =>
{
h.Username(agentInformation.RabbitId);
h.Password(agentInformation.Rabbit);
});
cfg.ConfigureEndpoints(context);
});
services.AddHostedService<Worker>();
});
});
And when consuming:
public class AgentConsumer : IConsumer<ServerMessageContract>
{ readonly ILogger _logger;
public AgentConsumer(ILogger<AgentConsumer> logger)
{
_logger = logger;
}
public Task Consume(ConsumeContext<ServerMessageContract> context)
{
_logger.LogInformation("Received Message: {Text}", context.Message.Payload == null ? "" : context.Message.Payload.ToString());
TaskRunner.CommandsFromServer(context.Message);
return Task.CompletedTask;
}
}
And when publishing:
var endpoint = await _bus.GetSendEndpoint(new Uri($"rabbitmq://{ServerAddress}/{SendQueue}"));
await endpoint.Send(agentMessage, stoppingToken);
Pretty straight forward....