-
Notifications
You must be signed in to change notification settings - Fork 69
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
Feature Request: allow clients to act as context manager and add close() method to cleanup resources #575
Comments
Note: this issue is even more important in the microgenerator, as the |
This is made slightly more complicated because of ownership. Because it's an injected parameter, we can't be sure that the client has the sole reference to the transport; we may wind up closing a transport that other clients are still using. This kind of a corner case, though, and it can probably be ameliorated with documentation and warnings to USE AT YOUR OWN RISK. |
Fixes #618 🦕 httplib2 leaves sockets open by default. This can lead to situations where programs run out of available sockets. httplib2 added a method last year to clean up connections. https://github.com/httplib2/httplib2/blob/9bf300cdc372938f4237150d5b9b615879eb51a1/python3/httplib2/__init__.py#L1498-L1506 This PR adds two ways to close http connections. The interface is intentionally similar to the proposed design for GAPIC clients. googleapis/gapic-generator-python#575
🤖 I have created a release \*beep\* \*boop\* --- ### [1.12.2](https://www.github.com/googleapis/google-api-python-client/compare/v1.12.1...v1.12.2) (2020-09-23) ### Bug Fixes * add method to close httplib2 connections ([#1038](https://www.github.com/googleapis/google-api-python-client/issues/1038)) ([98888da](https://www.github.com/googleapis/google-api-python-client/commit/98888dadf04e7e00524b6de273d28d02d7abc2c0)), closes [#618](https://www.github.com/googleapis/google-api-python-client/issues/618) [/github.com/httplib2/httplib2/blob/9bf300cdc372938f4237150d5b9b615879eb51a1/python3/httplib2/__init__.py#L1498-L1506](https://www.github.com/googleapis//github.com/httplib2/httplib2/blob/9bf300cdc372938f4237150d5b9b615879eb51a1/python3/httplib2/__init__.py/issues/L1498-L1506) [googleapis/gapic-generator-python#575](https://www.github.com/googleapis/gapic-generator-python/issues/575) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please).
To cross-link a bit, this / change in We're currently working around this issue in googleapis/python-bigquery#278 by calling
when done with the BigQuery Storage client. |
Do we need to bump the priority of this feature? |
Can we just add |
Since a
transport
object may be passed into a client constructor, the client does not close that transport in when the client object is deleted. This results in leaking socket resources in programs that create client objects dynamically.Proposed design:
Add the following two ways to explicitly clean-up sockets owned by the client: (1, preferred) allow clients to act as a context manager and (2) a close() method.
Context manager
Allow all client objects to be used as a context manager. Call close() on the client in the exit method to clean up sockets owned by the client.
In code samples with short-lived clients, this is the cleanup method we should recommend.
Example:
close()
methodAdd a
close()
method to all client objects. When called, clean up sockets owned by the client, including the transport channel.Example:
References:
The text was updated successfully, but these errors were encountered: