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

Consider context manager: irc.ensure_connection #15

Closed
impredicative opened this issue Apr 6, 2019 · 6 comments
Closed

Consider context manager: irc.ensure_connection #15

impredicative opened this issue Apr 6, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@impredicative
Copy link

impredicative commented Apr 6, 2019

Per our prior discussion, this issue exists to think about and possibly implement a context manager which offers a better than normal reliability of an active connection. It is intended to be used for short periods of time such as when sending a batch of messages or other commands. It could work by internally using PING and expecting PONG at a far higher rate than usual rate of ping_interval. The user's code can look something like this:

while True:
    with irc.ensure_connection(period=3, timeout=2):
        for msg in messages:
            irc.msg(channel, msg)
            time.sleep(2)
        for cmd in commands:
            irc.quote(*cmd)
            time.sleep(2)
    except irc.ConnectionError as exc:
        logging.exception('A connection error was encountered while processing the batch. It will be retried.')
        time.sleep(5)
    else:
        break

If the PING/PONG strategy is used, if the PONG is not received within say timeout seconds (default 2) of the PING, irc.ConnectionError can be raised. A PING could be sent every period seconds (default 3), and also definitely when releasing the context! I don't know IRC well enough, so this is my naive strategy.

I especially haven't given enough thought to the blocking vs non-blocking nature of this context manager, and so I'm hesitant to request it at all. Often the commands that are run are very time-sensitive, and it may not make sense to wait much to run them.

@impredicative impredicative changed the title Implement context manager: irc.ensure_connection Consider context manager: irc.ensure_connection Apr 6, 2019
@luk3yx
Copy link
Owner

luk3yx commented Apr 7, 2019

If this is implemented (and I understand it correctly), then:

  • irc.quote() inside irc.ensure_connection will raise an error if miniirc is disconnected, and the ping interval will be temporarily reduced.
  • irc.quote() outside irc.ensure_connection will work as per normal, and if miniirc is not connected to IRC, will not be sent until it is.

Commands such as irc.msg() call irc.quote() internally and would work with irc.ensure_connection().

Is it possible to tell (without using threading.local) if the current thread is inside a context manager?

@luk3yx luk3yx added the enhancement New feature or request label Apr 7, 2019
@luk3yx
Copy link
Owner

luk3yx commented Apr 30, 2019

I have created a (WIP) irc.ensure_connection in my new miniirc_extras (formerly megairc) repo.

@impredicative
Copy link
Author

Is the new megairc package meant to be used alongside miniirc in order to get the desired functionality?

@luk3yx
Copy link
Owner

luk3yx commented Apr 30, 2019

It is, however it is not on PyPI yet https://pypi.org/project/miniirc-extras/.

@luk3yx
Copy link
Owner

luk3yx commented Oct 20, 2019

Are you still interested in this being added to miniirc itself (as opposed to being a feature in another package)?

@impredicative
Copy link
Author

impredicative commented Oct 20, 2019

No. For now I think it's okay to have it in miniirc-extras. If I later have a compelling reason for wanting it in miniirc itself, I may then revisit the topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants