python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.16k stars 513 forks source link

Allow for custom exponential backoff in websocket client #1395

Closed jakubno closed 1 year ago

jakubno commented 1 year ago

Custom exponential backoff

In asyncio client, there was introduced option to use an infinite asynchronous iterator to reconnect automatically on errors, more in docs.

There were set constants for exponential backoff: client.py. But it's too slow for my use case. I would like to shorten it and I would prefer some cleaner way than this:

auto_reconnect = websockets.connect(...)
auto_reconnect.BACKOFF_INITIAL = 1
async for websocket in auto_reconnect:
    try:
        ...
    except websockets.ConnectionClosed:
        continue

Ideally it should be a parameter of the connect function.

aaugustin commented 1 year ago

I declared these constants as class variables because, somehow, I know that someone would want to adjust them, even if that involved a private API :-)

FYI I expected the following pattern:


websockets.connect.BACKOFF_INITIAL = 1
async for websocket in websockets.connect(...):
    ...

If you're OK with changing the timeout for all uses of connect() in your program, this avoids the convoluted pattern that you've shown.


The obvious answer is to add Yet Another Parameter in the signature of connect(). However, this API has too many parameters already: currently 17. In fact, it would be 4 additional parameters if we want to make everything customizable.

I'm slightly resisting the addition of new parameters because simplicity is a design goal of websockets. More parameters mean overhead in the docs for everyone...

Maybe I have to bite the bullet and move all parameters to a configuration object. Or just live with so many parameters. I'm not sure yet.

Since your request is "public API guaranteed to be stable", I want to be confident that I have the right API before committing to it. I don't feel ready to make this call right now :-/

jakubno commented 1 year ago

Sure, your pattern looks way better :) Thanks for your explanation!

aaugustin commented 1 year ago

In case someone else has the same need -- please leave a comment and I'm likely to reopen the issue!

The request for a public API is legitimate, even if I'm not sure how to handle it.