|
|
|
@ -42,76 +42,114 @@ _DEFAULT_POOL_SIZE = 6 |
|
|
|
|
|
|
|
|
|
class _AutoIntermediary(object): |
|
|
|
|
|
|
|
|
|
def __init__(self, delegate, on_deletion): |
|
|
|
|
def __init__(self, up, down, delegate): |
|
|
|
|
self._lock = threading.Lock() |
|
|
|
|
self._up = up |
|
|
|
|
self._down = down |
|
|
|
|
self._in_context = False |
|
|
|
|
self._delegate = delegate |
|
|
|
|
self._on_deletion = on_deletion |
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr): |
|
|
|
|
return getattr(self._delegate, attr) |
|
|
|
|
with self._lock: |
|
|
|
|
if self._delegate is None: |
|
|
|
|
raise AttributeError('No useful attributes out of context!') |
|
|
|
|
else: |
|
|
|
|
return getattr(self._delegate, attr) |
|
|
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
|
return self |
|
|
|
|
with self._lock: |
|
|
|
|
if self._in_context: |
|
|
|
|
raise ValueError('Already in context!') |
|
|
|
|
elif self._delegate is None: |
|
|
|
|
self._delegate = self._up() |
|
|
|
|
self._in_context = True |
|
|
|
|
return self |
|
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb): |
|
|
|
|
return False |
|
|
|
|
with self._lock: |
|
|
|
|
if not self._in_context: |
|
|
|
|
raise ValueError('Not in context!') |
|
|
|
|
self._down() |
|
|
|
|
self._in_context = False |
|
|
|
|
self._delegate = None |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
def __del__(self): |
|
|
|
|
self._on_deletion() |
|
|
|
|
with self._lock: |
|
|
|
|
if self._delegate is not None: |
|
|
|
|
self._down() |
|
|
|
|
self._delegate = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _StubAssemblyManager(object): |
|
|
|
|
|
|
|
|
|
def __init__( |
|
|
|
|
self, thread_pool, thread_pool_size, end_link, grpc_link, stub_creator): |
|
|
|
|
self._thread_pool = thread_pool |
|
|
|
|
self._pool_size = thread_pool_size |
|
|
|
|
self._end_link = end_link |
|
|
|
|
self._grpc_link = grpc_link |
|
|
|
|
self._stub_creator = stub_creator |
|
|
|
|
self._own_pool = None |
|
|
|
|
|
|
|
|
|
def up(self): |
|
|
|
|
if self._thread_pool is None: |
|
|
|
|
self._own_pool = logging_pool.pool( |
|
|
|
|
_DEFAULT_POOL_SIZE if self._pool_size is None else self._pool_size) |
|
|
|
|
assembly_pool = self._own_pool |
|
|
|
|
else: |
|
|
|
|
assembly_pool = self._thread_pool |
|
|
|
|
self._end_link.join_link(self._grpc_link) |
|
|
|
|
self._grpc_link.join_link(self._end_link) |
|
|
|
|
self._end_link.start() |
|
|
|
|
self._grpc_link.start() |
|
|
|
|
return self._stub_creator(self._end_link, assembly_pool) |
|
|
|
|
|
|
|
|
|
def down(self): |
|
|
|
|
self._end_link.stop(0).wait() |
|
|
|
|
self._grpc_link.stop() |
|
|
|
|
self._end_link.join_link(utilities.NULL_LINK) |
|
|
|
|
self._grpc_link.join_link(utilities.NULL_LINK) |
|
|
|
|
if self._own_pool is not None: |
|
|
|
|
self._own_pool.shutdown(wait=True) |
|
|
|
|
self._own_pool = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _assemble( |
|
|
|
|
channel, host, metadata_transformer, request_serializers, |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size): |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size, stub_creator): |
|
|
|
|
end_link = _core_implementations.invocation_end_link() |
|
|
|
|
grpc_link = invocation.invocation_link( |
|
|
|
|
channel, host, metadata_transformer, request_serializers, |
|
|
|
|
response_deserializers) |
|
|
|
|
if thread_pool is None: |
|
|
|
|
invocation_pool = logging_pool.pool( |
|
|
|
|
_DEFAULT_POOL_SIZE if thread_pool_size is None else thread_pool_size) |
|
|
|
|
assembly_pool = invocation_pool |
|
|
|
|
else: |
|
|
|
|
invocation_pool = thread_pool |
|
|
|
|
assembly_pool = None |
|
|
|
|
end_link.join_link(grpc_link) |
|
|
|
|
grpc_link.join_link(end_link) |
|
|
|
|
end_link.start() |
|
|
|
|
grpc_link.start() |
|
|
|
|
return end_link, grpc_link, invocation_pool, assembly_pool |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _disassemble(end_link, grpc_link, pool): |
|
|
|
|
end_link.stop(24 * 60 * 60).wait() |
|
|
|
|
grpc_link.stop() |
|
|
|
|
end_link.join_link(utilities.NULL_LINK) |
|
|
|
|
grpc_link.join_link(utilities.NULL_LINK) |
|
|
|
|
if pool is not None: |
|
|
|
|
pool.shutdown(wait=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _wrap_assembly(stub, end_link, grpc_link, assembly_pool): |
|
|
|
|
disassembly_thread = threading.Thread( |
|
|
|
|
target=_disassemble, args=(end_link, grpc_link, assembly_pool)) |
|
|
|
|
return _AutoIntermediary(stub, disassembly_thread.start) |
|
|
|
|
stub_assembly_manager = _StubAssemblyManager( |
|
|
|
|
thread_pool, thread_pool_size, end_link, grpc_link, stub_creator) |
|
|
|
|
stub = stub_assembly_manager.up() |
|
|
|
|
return _AutoIntermediary( |
|
|
|
|
stub_assembly_manager.up, stub_assembly_manager.down, stub) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _dynamic_stub_creator(service, cardinalities): |
|
|
|
|
def create_dynamic_stub(end_link, invocation_pool): |
|
|
|
|
return _crust_implementations.dynamic_stub( |
|
|
|
|
end_link, service, cardinalities, invocation_pool) |
|
|
|
|
return create_dynamic_stub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generic_stub( |
|
|
|
|
channel, host, metadata_transformer, request_serializers, |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size): |
|
|
|
|
end_link, grpc_link, invocation_pool, assembly_pool = _assemble( |
|
|
|
|
return _assemble( |
|
|
|
|
channel, host, metadata_transformer, request_serializers, |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size) |
|
|
|
|
stub = _crust_implementations.generic_stub(end_link, invocation_pool) |
|
|
|
|
return _wrap_assembly(stub, end_link, grpc_link, assembly_pool) |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size, |
|
|
|
|
_crust_implementations.generic_stub) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dynamic_stub( |
|
|
|
|
channel, host, service, cardinalities, metadata_transformer, |
|
|
|
|
request_serializers, response_deserializers, thread_pool, |
|
|
|
|
thread_pool_size): |
|
|
|
|
end_link, grpc_link, invocation_pool, assembly_pool = _assemble( |
|
|
|
|
return _assemble( |
|
|
|
|
channel, host, metadata_transformer, request_serializers, |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size) |
|
|
|
|
stub = _crust_implementations.dynamic_stub( |
|
|
|
|
end_link, service, cardinalities, invocation_pool) |
|
|
|
|
return _wrap_assembly(stub, end_link, grpc_link, assembly_pool) |
|
|
|
|
response_deserializers, thread_pool, thread_pool_size, |
|
|
|
|
_dynamic_stub_creator(service, cardinalities)) |
|
|
|
|