dotnet / dotnet/MQTTnet

Connection timeout error due to high latency on the network

Open
#1,956 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I try to create a strategy for the fallout messages. When the client loses connection, messages are saved to the local database. After reconnecting the device on the VerneMQ broker, the device resends messages to the VerneMQ broker.

Suppose we have a network with latency of 10 second. I configured latency via:
`
sudo tc qdisc add dev lo root netem delay 10s
`
When I call `ConnectAsync` method, I get an `OperationCanceledException` exception.

The example below shows I use `ConnectAsync` with cancelation token. I have also used:
``` csharp
MqttClientOptionsBuilder()
...
.WithTimeout(20000ms)
...
.Build()
```
``` csharp
try
{
using (var timeoutToken = new CancellationTokenSource(20000))
{
MqttClientConnectResult result = await _client.ConnectAsync(
_connectionInfo.GetMqttConnectOptions(),
timeoutToken.Token);

if (result.ResultCode == MqttClientConnectResultCode.Success)
{
await CompleteAstarteConnection(result.IsSessionPresent);
}
else
{
throw new AstarteTransportException
($"Error connecting to MQTT. Code: {result.ResultCode}");
}
}
}
catch (OperationCanceledException)
{
Trace.WriteLine("Timeout while connecting.");
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.