session_cache.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright 2018 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """gRPC's APIs for TLS Session Resumption support"""
  15. from grpc._cython import cygrpc as _cygrpc
  16. def ssl_session_cache_lru(capacity):
  17. """Creates an SSLSessionCache with LRU replacement policy
  18. Args:
  19. capacity: Size of the cache
  20. Returns:
  21. An SSLSessionCache with LRU replacement policy that can be passed as a value for
  22. the grpc.ssl_session_cache option to a grpc.Channel. SSL session caches are used
  23. to store session tickets, which clients can present to resume previous TLS sessions
  24. with a server.
  25. """
  26. return SSLSessionCache(_cygrpc.SSLSessionCacheLRU(capacity))
  27. class SSLSessionCache(object):
  28. """An encapsulation of a session cache used for TLS session resumption.
  29. Instances of this class can be passed to a Channel as values for the
  30. grpc.ssl_session_cache option
  31. """
  32. def __init__(self, cache):
  33. self._cache = cache
  34. def __int__(self):
  35. return int(self._cache)