MQTTnet
MQTTnet copied to clipboard
Handle all messages before server shutdown
Describe your question
Is it possible to make sure broker (server) has processed all queued messages before shutdown? To accomplish message safety.
Which project is your question related to?
- Server
I find out .Net Core host wait 5 seconds by default to hosted service(MQTT server) stop executing and after that shutdowns the hosted service ungracefully.
the time out is configurable in ConfigureServices()
:
.ConfigureServices((hostContext, services) =>
{
services.PostConfigure<HostOptions>(option =>
{
option.ShutdownTimeout = Timeout.InfiniteTimeSpan;
});
});
⚠setting infinite timeout may not be a good idea in all cases.
I don't think we can wait for it because (depending on the use case) a lot of devices is pushing data all the time. So the broker replies with ACK etc. So there will be no time window where it is safe to stop. Espcially QoS 0 is a problem here becuase the TCP stack wil accept new messages but the server ignores them because it is already shutting down. We can also try to send a DISCONNECT (MQTT v5) but this will lead to the problem that sill queued messages cannot be sent over to a client anymore. To me it looks like a chicken egg problem.