Azure / azure-iot-sdk-python

A Python SDK for connecting devices to Microsoft Azure IoT services
MIT License
423 stars 377 forks source link

Hourly new connect of mqtt connection #739

Closed mtin closed 3 years ago

mtin commented 3 years ago

Context

Description of the issue

We are observing regular reconnects (approx. every hour) of the data connection. Listening on the event-grid topic for connected/disconnected events we can see this pattern (we noticed there are usually no disconnected events and there is no data loss)

Screenshot 2021-02-22 at 18 08 28

Console log of the issue

> cat iotconnector-2.log | grep -v "Sending PUBLISH" | grep -v "Received PUBACK"
...
[paho][DEBUG] 2021-02-22 14:27:31,307: Sending SUBSCRIBE (d0, m1) [(b'$iothub/methods/POST/#', 1)]
[paho][DEBUG] 2021-02-22 14:27:31,450: Received SUBACK
[paho][DEBUG] 2021-02-22 15:25:29,435: Sending DISCONNECT
[paho][DEBUG] 2021-02-22 15:25:31,141: Sending CONNECT (u1, p1, wr0, wq0, wf0, c0, k60) client_id=b'b827eb526589'
[paho][DEBUG] 2021-02-22 15:25:31,171: Received CONNACK (1, 0)
[paho][DEBUG] 2021-02-22 16:23:29,440: Sending DISCONNECT
[paho][DEBUG] 2021-02-22 16:23:31,139: Sending CONNECT (u1, p1, wr0, wq0, wf0, c0, k60) client_id=b'b827eb526589'
[paho][DEBUG] 2021-02-22 16:23:31,171: Received CONNACK (1, 0)
[paho][DEBUG] 2021-02-22 17:21:29,446: Sending DISCONNECT
[paho][DEBUG] 2021-02-22 17:21:30,332: Sending CONNECT (u1, p1, wr0, wq0, wf0, c0, k60) client_id=b'b827eb526589'
[paho][DEBUG] 2021-02-22 17:21:30,363: Received CONNACK (1, 0)

No major blocker, but still - is this expected behaviour? Thanks in advance!

cartertinney commented 3 years ago

This is expected behavior. By default when using Shared Access Signature-based authentication (e.g. connection strings), we will disconnect and reconnect approximately every 58 minutes in order to renew the security credentials which will expire (by default) every hour. You should not experience any sort of data-loss during this automatic process.

If you would like to change the amount of time the credentials are valid for, use the optional sastoken_ttl kwarg when instantiating the client with your desired factory method. Pass through the number of seconds you would like your credentials to be valid for.

Is this sufficient to answer your concerns?

mtin commented 3 years ago

ok, cool, thank you for clearing that up!