Skip to content

Commit b526473

Browse files
authored
fix: make ThreadPoolExecutor a class var (#461)
1 parent dafb41f commit b526473

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

google/auth/transport/grpc.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,22 @@ class AuthMetadataPlugin(grpc.AuthMetadataPlugin):
5151
object used to refresh credentials as needed.
5252
"""
5353

54+
# Python 2.7 has no default for max_workers.
55+
# In Python >= 3.5, ThreadPoolExecutor defaults to the
56+
# number of processors on the machine, multiplied by 5.
57+
if six.PY2: # pragma: NO COVER
58+
max_workers = 5
59+
else:
60+
max_workers = None
61+
_AUTH_THREAD_POOL = futures.ThreadPoolExecutor(max_workers=max_workers)
62+
5463
def __init__(self, credentials, request):
5564
# pylint: disable=no-value-for-parameter
5665
# pylint doesn't realize that the super method takes no arguments
5766
# because this class is the same name as the superclass.
5867
super(AuthMetadataPlugin, self).__init__()
5968
self._credentials = credentials
6069
self._request = request
61-
self._pool = futures.ThreadPoolExecutor(max_workers=1)
6270

6371
def _get_authorization_headers(self, context):
6472
"""Gets the authorization headers for a request.
@@ -89,12 +97,9 @@ def __call__(self, context, callback):
8997
callback (grpc.AuthMetadataPluginCallback): The callback that will
9098
be invoked to pass in the authorization metadata.
9199
"""
92-
future = self._pool.submit(self._get_authorization_headers, context)
100+
future = self._AUTH_THREAD_POOL.submit(self._get_authorization_headers, context)
93101
future.add_done_callback(self._callback_wrapper(callback))
94102

95-
def __del__(self):
96-
self._pool.shutdown(wait=False)
97-
98103

99104
def secure_authorized_channel(
100105
credentials,

0 commit comments

Comments
 (0)