Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Local Mode #1230

Merged
merged 3 commits into from
Feb 16, 2024
Merged

Improve Local Mode #1230

merged 3 commits into from
Feb 16, 2024

Conversation

rany2
Copy link
Contributor

@rany2 rany2 commented Sep 6, 2023

  • Improve Local Mode Reliability

    Local mode devices cannot handle more than 1 concurrent connection
    to the HTTP server. This means that if an additional connection is
    made when another one is pending or is in the process of being
    cleaned up by the device, it will return connection reset error.

    Mention of Failed Attempt

    An approach where we acquire a lock and wait 100ms whenever we
    use the send() method was attempted but did not work consistently
    on all devices. My theory is that depending the on the processing
    on the device, it takes a different amount of time for it to
    reallocate resources to the next new connection.

    Therefore, simply retrying has worked just fine in my testing.
    It also has the added advantage of working despite another HA
    instance, app, etc. taking advantage of local mode.

  • Use time.time_ns() to generate sequence number

    Converting time.time() to an int and then converting that to ms
    risks resulting in the same sequence number for a given second.
    While we have safe guards against this already, it is better
    to prevent this scenario all together and just convert to ms
    properly.

  • Acquire lock when performing comparisons against/returning of sequence

    Prevent the same sequence number being returned for different threads
    as this might cause hard to detect issues.

@rany2 rany2 changed the title Improve LAN Mode Improve Local Mode Sep 7, 2023
Local mode devices cannot handle more than 1 concurrent connection
to the HTTP server. This means that if an additional connection is
made when another one is pending or is in the process of being
cleaned up by the device, it will return connection reset error.

==Mention of Failed Attempt==

An approach where we acquire a lock and wait 100ms whenever we
use the send() method was attempted but did not work consistently
on all devices. My theory is that depending the on the processing
on the device, it takes a different amount of time for it to
reallocate resources to the next new connection.

Therefore, simply retrying has worked just fine in my testing.
It also has the added advantage of working despite another HA
instance, app, etc. taking advantage of local mode.

Signed-off-by: rany <ranygh@riseup.net>
Converting time.time() to an int and then converting that to ms
risks resulting in the same sequence number for a given second.
While we have safe guards against this already, it is better
to prevent this scenario all together and just convert to ms
properly.

Signed-off-by: rany <ranygh@riseup.net>
Prevent the same sequence number being returned for different threads
as this might cause hard to detect issues.

Signed-off-by: rany <ranygh@riseup.net>
@AlexxIT AlexxIT merged commit c216d77 into AlexxIT:master Feb 16, 2024
@AlexxIT
Copy link
Owner

AlexxIT commented Feb 16, 2024

Thanks! Very impressive realisation

@AlexxIT AlexxIT added this to the v3.5.5 milestone Feb 16, 2024
@AlexxIT
Copy link
Owner

AlexxIT commented Feb 16, 2024

@rany2 rany2 deleted the improve_lan_mode branch February 16, 2024 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants