Skip to content

Python example: reconnect after drops and silence, with backoff - #31

Open
GiuseppeSaluto wants to merge 1 commit into
aisstream:mainfrom
GiuseppeSaluto:python-reconnect
Open

GiuseppeSaluto wants to merge 1 commit into
aisstream:mainfrom
GiuseppeSaluto:python-reconnect

Conversation

@GiuseppeSaluto

Copy link
Copy Markdown

The Python example ends on the first dropped connection, and it can wait forever on a
connection that stays open but stops delivering messages. This change keeps it running
while staying a short, readable example.

What changes (python/main.py only)

  • Iterates over websockets.connect(), so a new connection is opened after each one ends.
  • Reconnects when no message arrives for SILENCE_TIMEOUT_SECONDS (120 s). I hit this
    in practice: the stream stayed open but silent for about 10 minutes.
  • Waits before reconnecting, with exponential backoff and jitter as the documentation
    recommends, and resets the wait once the subscription is confirmed. An invalid API key
    closes the connection right after it opens, so without the wait the client would
    reconnect in a tight loop.
  • When the connection closes before the SubscriptionConfirmation, it says so and
    suggests checking the API key and bounding boxes.

How I tested it

  • Live stream: positions print as before.
  • A bounding box with no traffic and a 5 s silence timeout: it reconnects after each silence.
  • An invalid API key: it backs off 1, 2, 4, 8, 16 s instead of reconnecting in a loop.
  • A local server that drops the connection after 2 s: it reconnects after about a second.

The example ended on the first dropped connection and could wait forever on a
connection that stays open but stops delivering messages.

- Iterate over websockets.connect() to open a new connection after each one ends.
- Reconnect when no message arrives for SILENCE_TIMEOUT_SECONDS.
- Wait before reconnecting, with exponential backoff and jitter as the
  documentation recommends; reset it once the subscription is confirmed. An
  invalid API key closes the connection right after it opens, so without the
  wait the client would reconnect in a tight loop.
- Say so when the connection closes before the SubscriptionConfirmation, the
  usual sign of a wrong API key or bounding boxes.
@lupodevelop

Copy link
Copy Markdown

@aisstream Any updates for this fix?

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