pull/21954/head
Richard Belleville 5 years ago
parent 753ab16ebb
commit 67a492c946
  1. 14
      src/python/grpcio/grpc/_simple_stubs.py

@ -18,7 +18,8 @@ import datetime
import os import os
import logging import logging
import threading import threading
from typing import Any, AnyStr, Callable, Iterator, OrderedDict, Optional, Sequence, Tuple, TypeVar, Union from typing import (Any, AnyStr, Callable, Iterator, OrderedDict, Optional,
Sequence, Tuple, TypeVar, Union)
import grpc import grpc
@ -26,7 +27,8 @@ RequestType = TypeVar('RequestType')
ResponseType = TypeVar('ResponseType') ResponseType = TypeVar('ResponseType')
OptionsType = Sequence[Tuple[str, str]] OptionsType = Sequence[Tuple[str, str]]
CacheKey = Tuple[str, OptionsType, Optional[grpc.ChannelCredentials], Optional[grpc.Compression]] CacheKey = Tuple[str, OptionsType, Optional[grpc.ChannelCredentials], Optional[
grpc.Compression]]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -35,7 +37,7 @@ if _EVICTION_PERIOD_KEY in os.environ:
_EVICTION_PERIOD = datetime.timedelta( _EVICTION_PERIOD = datetime.timedelta(
seconds=float(os.environ[_EVICTION_PERIOD_KEY])) seconds=float(os.environ[_EVICTION_PERIOD_KEY]))
_LOGGER.debug("Setting managed channel eviction period to %s", _LOGGER.debug("Setting managed channel eviction period to %s",
_EVICTION_PERIOD) _EVICTION_PERIOD)
else: else:
_EVICTION_PERIOD = datetime.timedelta(minutes=10) _EVICTION_PERIOD = datetime.timedelta(minutes=10)
@ -54,7 +56,7 @@ def _create_channel(target: str, options: Sequence[Tuple[str, str]],
) )
if channel_credentials._credentials is grpc.experimental._insecure_channel_credentials: if channel_credentials._credentials is grpc.experimental._insecure_channel_credentials:
_LOGGER.debug(f"Creating insecure channel with options '{options}' " + _LOGGER.debug(f"Creating insecure channel with options '{options}' " +
f"and compression '{compression}'") f"and compression '{compression}'")
return grpc.insecure_channel(target, return grpc.insecure_channel(target,
options=options, options=options,
compression=compression) compression=compression)
@ -67,6 +69,7 @@ def _create_channel(target: str, options: Sequence[Tuple[str, str]],
options=options, options=options,
compression=compression) compression=compression)
class ChannelCache: class ChannelCache:
# NOTE(rbellevi): Untyped due to reference cycle. # NOTE(rbellevi): Untyped due to reference cycle.
_singleton = None _singleton = None
@ -93,7 +96,8 @@ class ChannelCache:
def _evict_locked(self, key: CacheKey): def _evict_locked(self, key: CacheKey):
channel, _ = self._mapping.pop(key) channel, _ = self._mapping.pop(key)
_LOGGER.debug("Evicting channel %s with configuration %s.", channel, key) _LOGGER.debug("Evicting channel %s with configuration %s.", channel,
key)
channel.close() channel.close()
del channel del channel

Loading…
Cancel
Save