The state of a client: the requests waiting for responses.
Functions#
__init__#
__init__(send, options, services) -> None| Parameter | Type | Description |
|---|---|---|
send | SendFn | |
options | ClientOptions | |
services | Context[Any] |
def __init__(self, send: SendFn, options: ClientOptions, services: Context[Any]) -> None:
self._send = send
self._options = options
self._services = services
self._entries = PeerKeyDict[RequestId, _Entry]()
self._is_shutdown = Falseclose#
close() -> Effect[None]Returns Effect[None]
def close(self) -> Effect[None]:
def body(parent: Fiber[Any, Any]) -> Effect[None]:
self._is_shutdown = True
return self._clear(Exit.interrupt(parent.id))
return _WithFiber(body)call#
call(rpc, payload, call) -> Effect[Any, Any]| Parameter | Type | Description |
|---|---|---|
rpc | AnyRpc | |
payload | object | |
call | CallOptions |
Returns Effect[Any, Any]
def call(self, rpc: AnyRpc, payload: object, call: CallOptions) -> Effect[Any, Any]:
if self._options.disable_tracing:
return self._effect_request(rpc, payload, call, None)
from pyfect.tracer import use_span
return use_span(
self._span_name(rpc),
lambda span: self._effect_request(rpc, payload, call, span),
kind="client",
attributes=self._span_attributes(rpc),
)open_stream#
open_stream(rpc, payload, call) -> Effect[Queue[Any, Any], Never, Scope]| Parameter | Type | Description |
|---|---|---|
rpc | AnyRpc | |
payload | object | |
call | CallOptions |
Returns Effect[Queue[Any, Any], Never, Scope]
def open_stream(
self, rpc: AnyRpc, payload: object, call: CallOptions
) -> Effect[Queue[Any, Any], Never, Scope]:
context = call.context or Context.empty()
@gen
def open():
if self._is_shutdown:
yield from interrupt_closed("RPC client")
span = None
if not self._options.disable_tracing:
span = yield from make_span_scoped(
self._span_name(rpc), kind="client", attributes=self._span_attributes(rpc)
)
fiber = _current_fiber()
id = self._next_id()
scope = fiber._context.get_unsafe(Scope)
queue = yield from Queue[Any, Any].bounded(call.stream_buffer_size)
def finalize(exit: Exit.Exit[Any, Any]) -> Effect[None]:
queue.shutdown_unsafe()
if id not in self._entries:
return _VOID
del self._entries[id]
interruptors = (
tuple(sorted(exit.cause.interruptors)) if isinstance(exit, Exit.Failure) else ()
)
return self._send_interrupt(id, context, interruptors)
yield from scope.add_finalizer(finalize)
self._entries[id] = _QueueEntry(rpc, queue, context)
send = self._with_middleware(
rpc,
lambda message: self._send(OnFromClient(message, context)),
self._request(fiber, rpc, id, payload=payload, call=call, span=span),
)
if span is not None:
send = send.with_parent_span(span, capture_stack_trace=False)
def failed(cause: Any) -> Effect[None]:
if not cause.has_interrupts_only:
self._entries.pop(id, None)
return queue.fail_cause(cause).as_void()
yield from fork_in(send.on_error(failed).interruptible(), scope, start_immediately=True)
return queue
return openwrite#
write(message) -> Effect[None]Hand the client a response.
| Parameter | Type | Description |
|---|---|---|
message | FromServer |
Returns Effect[None]
def write(self, message: FromServer) -> Effect[None]:
"""Hand the client a response."""
match message:
case ResponseChunk(request_id=request_id, values=values):
entry = self._entries.get(request_id)
if not isinstance(entry, _QueueEntry):
return _VOID
return self._offer(entry, request_id, values)
case ResponseExit(request_id=request_id, exit=exit):
return self._exit(request_id, exit)
case ResponseDefect(defect=defect):
return self._clear(Exit.die(defect))
case ClientEnd():
return _VOIDmethods#
methods(group) -> dict[str, Any]The client's methods, by RPC tag.
| Parameter | Type | Description |
|---|---|---|
group | type[RpcGroup] |
Returns dict[str, Any]
def methods(self, group: type[RpcGroup]) -> dict[str, Any]:
"""The client's methods, by RPC tag."""
methods: dict[str, Any] = {}
for rpc in group.rpcs.values():
methods[rpc.tag] = self._method(rpc)
return methods