Skip to content

Commit

Permalink
feat: teach the connector about transient esp32 errors (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 30, 2022
1 parent 5046781 commit 486fbbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/bleak_retry_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
NO_RSSI_VALUE = -127


# Make sure bleak and dbus-next have time
# Make sure bleak and dbus-fast have time
# to run their cleanup callbacks or the
# retry call will just fail in the same way.
BLEAK_DBUS_BACKOFF_TIME = 0.25
BLEAK_TRANSIENT_BACKOFF_TIME = BLEAK_DBUS_BACKOFF_TIME = 0.25
BLEAK_OUT_OF_SLOTS_BACKOFF_TIME = 1.5
BLEAK_BACKOFF_TIME = 0.1

Expand Down Expand Up @@ -89,7 +89,14 @@
BLEAK_SAFETY_TIMEOUT = 20

# These errors are transient with dbus, and we should retry
TRANSIENT_ERRORS = {"le-connection-abort-by-local", "br-connection-canceled"}
TRANSIENT_ERRORS = {
"le-connection-abort-by-local",
"br-connection-canceled",
"ESP_GATT_CONN_FAIL_ESTABLISH",
"ESP_GATT_CONN_TERMINATE_PEER_USER",
"ESP_GATT_CONN_TERMINATE_LOCAL_HOST",
"ESP_GATT_CONN_CONN_CANCEL",
}

DEVICE_MISSING_ERRORS = {"org.freedesktop.DBus.Error.UnknownObject"}

Expand Down Expand Up @@ -196,14 +203,17 @@ def address_to_bluez_path(address: str, adapter: str | None = None) -> str:

def calculate_backoff_time(exc: Exception) -> float:
"""Calculate the backoff time based on the exception."""

if isinstance(
exc, (BleakDBusError, EOFError, asyncio.TimeoutError, BrokenPipeError)
):
return BLEAK_DBUS_BACKOFF_TIME
if isinstance(exc, BleakError) and any(
error in str(exc) for error in OUT_OF_SLOTS_ERRORS
):
return BLEAK_OUT_OF_SLOTS_BACKOFF_TIME
if isinstance(exc, BleakError):
bleak_error = str(exc)
if any(error in bleak_error for error in OUT_OF_SLOTS_ERRORS):
return BLEAK_OUT_OF_SLOTS_BACKOFF_TIME
if any(error in bleak_error for error in TRANSIENT_ERRORS):
return BLEAK_TRANSIENT_BACKOFF_TIME
return BLEAK_BACKOFF_TIME


Expand Down Expand Up @@ -521,6 +531,7 @@ def _raise_if_needed(name: str, description: str, exc: Exception) -> None:
description,
attempt,
rssi,
exc_info=True,
)
backoff_time = calculate_backoff_time(exc)
await wait_for_disconnect(device, backoff_time)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
BLEAK_BACKOFF_TIME,
BLEAK_DBUS_BACKOFF_TIME,
BLEAK_OUT_OF_SLOTS_BACKOFF_TIME,
BLEAK_TRANSIENT_BACKOFF_TIME,
MAX_TRANSIENT_ERRORS,
BleakAbortedError,
BleakClientWithServiceCache,
Expand Down Expand Up @@ -1499,6 +1500,10 @@ def test_calculate_backoff_time():
)
== BLEAK_OUT_OF_SLOTS_BACKOFF_TIME
)
assert (
calculate_backoff_time(BleakError("ESP_GATT_CONN_FAIL_ESTABLISH"))
== BLEAK_TRANSIENT_BACKOFF_TIME
)


@pytest.mark.asyncio
Expand Down

0 comments on commit 486fbbc

Please sign in to comment.