Skip to content

Calling consumer.close() multiple times should not raise an Exception #676

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

Closed
wants to merge 1 commit into from

Conversation

mkmoisen
Copy link
Contributor

@mkmoisen mkmoisen commented Oct 1, 2019

Currently, calling consumer.close more than once will raise a RuntimeError: Consumer already closed.

The proposed changes will just return NULL if the user calls close on a consumer that has already been closed.

This will make it rerunnable and allow the developer to not have to trap this exception.

Currently, calling `consumer.close` more than once will raise a `RuntimeError: Consumer already closed`.

The proposed changes will just return NULL if the user calls `close` on a consumer that has already been closed.

This will make it rerunnable and allow the developer to not have to trap this exception.
@ghost
Copy link

ghost commented Oct 1, 2019

It looks like @mkmoisen hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement here.

Once you've signed reply with [clabot:check] to prove it.

Appreciation of efforts,

clabot

@mkmoisen
Copy link
Contributor Author

mkmoisen commented Oct 1, 2019

[clabot:check]

@ghost
Copy link

ghost commented Oct 1, 2019

@confluentinc It looks like @mkmoisen just signed our Contributor License Agreement. 👍

Always at your service,

clabot

@mkmoisen mkmoisen closed this Oct 1, 2019
@edenhill
Copy link
Contributor

edenhill commented Oct 3, 2019

What's the use-case for calling close multiple times?

@mkmoisen
Copy link
Contributor Author

mkmoisen commented Oct 3, 2019

Hi @edenhill please check the new PR here which is passing. I've copied and pasted the following over there.

The use case is to make close rerunnable, for example if you wanted to make a context manager, or if you had a large function wrapped in a try/except/finally. Another programmer who calls your context manager can close inside if he wants; otherwise the context manager will close for him.

import contextlib
from confluent_kafka import Consumer

@contextlib.contextmanager
def kafka():
    consumer = Consumer({...})
    try:
        yield consumer
    finally:
        # currently, would need to wrap this in a try/except
        consumer.close()

# ...
# Some programmers may want to call close explicitly
with kafka() as consumer:
        # do something
        consumer.close()

# Other programmers would prefer the context manager to take care
with kafka() as consumer:
    # do something

I'm also under the impression that the majority of the other python libraries that close resources also make close rerunnable. I think it would be a good idea to have confluent_kafka follow this pattern.

from requests import Session
s = Session()
s.close()
s.close()  # does not raise an exception

import sqlite3
conn = sqlite3.connect(':memory:')
conn.close()
conn.close()  # does not raise an exception

@edenhill
Copy link
Contributor

edenhill commented Oct 7, 2019

@rnpridgeon What are your thoughts on this?

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