Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """deferred_resource converts blocking apiclient resource to deferred.""" | 5 """deferred_resource converts blocking apiclient resource to deferred.""" |
| 6 | 6 |
| 7 import collections | 7 import collections |
| 8 import datetime | 8 import datetime |
| 9 import functools | 9 import functools |
| 10 import httplib | 10 import httplib |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 self._pool = _pool or self._create_thread_pool(max_concurrent_requests) | 154 self._pool = _pool or self._create_thread_pool(max_concurrent_requests) |
| 155 self._resource = resource | 155 self._resource = resource |
| 156 self.credentials = credentials | 156 self.credentials = credentials |
| 157 self.retry_wait_seconds = retry_wait_seconds | 157 self.retry_wait_seconds = retry_wait_seconds |
| 158 self.retry_attempt_count = retry_attempt_count | 158 self.retry_attempt_count = retry_attempt_count |
| 159 self.verbose = verbose | 159 self.verbose = verbose |
| 160 self.log_prefix = log_prefix | 160 self.log_prefix = log_prefix |
| 161 self.api = self.Api(self) | 161 self.api = self.Api(self) |
| 162 self._th_local = threading.local() | 162 self._th_local = threading.local() |
| 163 self.started = False | 163 self.started = False |
| 164 self.timeout = timeout | 164 self._resource._http.timeout = timeout |
| 165 | 165 |
| 166 @classmethod | 166 @classmethod |
| 167 def _create_thread_pool(cls, max_concurrent_requests): | 167 def _create_thread_pool(cls, max_concurrent_requests): |
| 168 return DaemonThreadPool(minthreads=1, maxthreads=max_concurrent_requests) | 168 return DaemonThreadPool(minthreads=1, maxthreads=max_concurrent_requests) |
| 169 | 169 |
| 170 @classmethod | 170 @classmethod |
| 171 def _create_async( | 171 def _create_async( |
| 172 cls, resource_factory, max_concurrent_requests=1, _pool=None, **kwargs): | 172 cls, resource_factory, max_concurrent_requests=1, _pool=None, **kwargs): |
| 173 _pool = _pool or cls._create_thread_pool(max_concurrent_requests) | 173 _pool = _pool or cls._create_thread_pool(max_concurrent_requests) |
| 174 result = defer.Deferred() | 174 result = defer.Deferred() |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 create_creds = False | 328 create_creds = False |
| 329 if getattr(self._th_local, 'http', None) is None: | 329 if getattr(self._th_local, 'http', None) is None: |
| 330 create_creds = True | 330 create_creds = True |
| 331 elif (self._th_local.credentials_expiry is not None and | 331 elif (self._th_local.credentials_expiry is not None and |
| 332 self._th_local.credentials_expiry <= now): | 332 self._th_local.credentials_expiry <= now): |
| 333 create_creds = True | 333 create_creds = True |
| 334 | 334 |
| 335 if create_creds: | 335 if create_creds: |
| 336 self._th_local.credentials = None | 336 self._th_local.credentials = None |
| 337 self._th_local.credentials_expiry = None | 337 self._th_local.credentials_expiry = None |
| 338 self._th_local.http = httplib2.Http(timeout=self.timeout) | 338 self._th_local.http = self._resource._http |
|
nodir
2016/08/17 07:01:52
That doesn't look thread-safe anymore
dsansome
2016/08/17 08:55:41
Ah so *that's* why it's done this way. I've chang
nodir
2016/08/17 21:07:29
Yes, th_local is threading.Local
| |
| 339 if self.credentials is not None: | 339 if self.credentials is not None: |
| 340 creds = self.credentials | 340 creds = self.credentials |
| 341 ttl = None | 341 ttl = None |
| 342 if isinstance(creds, CredentialFactory): | 342 if isinstance(creds, CredentialFactory): |
| 343 ttl = creds.ttl | 343 ttl = creds.ttl |
| 344 creds = creds() | 344 creds = creds() |
| 345 self._th_local.credentials = creds.from_json(creds.to_json()) | 345 self._th_local.credentials = creds.from_json(creds.to_json()) |
| 346 if ttl is not None: | 346 if ttl is not None: |
| 347 self._th_local.credentials_expiry = now + ttl | 347 self._th_local.credentials_expiry = now + ttl |
| 348 self._th_local.http = self._th_local.credentials.authorize( | 348 self._th_local.http = self._th_local.credentials.authorize( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 372 return d | 372 return d |
| 373 | 373 |
| 374 | 374 |
| 375 def is_transient(ex): | 375 def is_transient(ex): |
| 376 if isinstance(ex, apiclient.errors.HttpError) and ex.resp: | 376 if isinstance(ex, apiclient.errors.HttpError) and ex.resp: |
| 377 return ex.resp.status >= 500; | 377 return ex.resp.status >= 500; |
| 378 if isinstance(ex, ssl.SSLError): | 378 if isinstance(ex, ssl.SSLError): |
| 379 # No reason, no errcode. | 379 # No reason, no errcode. |
| 380 return "timed out" in str(ex) | 380 return "timed out" in str(ex) |
| 381 return False | 381 return False |
| OLD | NEW |