跳到主要内容

高级:高可用背压

查看 Markdown

当父 Flow 可能因过载而拒绝请求时,通过一个短生命周期的 SubmitRequestFlow 提交每个请求。

客户端启动 SubmitRequestFlow,而不是在内存中重试 RPC。SubmitStep 为请求选出一个父 Flow,并调用它的 SendRequest RPC。只有父 Flow 接受请求后,这个 Step 才完成。如果父 Flow 返回 false,Step 失败,普通的 Step 重试策略会在退避后再次尝试。启动父 Flow 时的竞争也由同一套重试处理,见多父 Flow 分区。即使提交进程或 Worker 重启,Dex 也会继续重试。

父 Flow 会拒绝处理不了的请求,所以负载保持有界。每个提交请求各自重试,直到有父 Flow 接受。

核心实现

submit_request_flow.pyPython
class SubmitStep(Step[SubmitRequestInput]):
def __init__(
self,
client_provider: Callable[[], AsyncClient],
parent_flow: AdvancedShortLiveParentFlow,
) -> None:
self.client_provider = client_provider
self.parent_flow = parent_flow

async def execute( # type: ignore[override]
self, context: AsyncContext, input: SubmitRequestInput
) -> StepDecision:
if not input.parent_ids:
raise ValueError("at least one parent Flow ID is required")
parent_id = input.parent_ids[partition(input.request, len(input.parent_ids))]
accepted = await enqueue_request(
self.client_provider(), self.parent_flow, parent_id, input.request
)
if not accepted:
raise RuntimeError(f"parent {parent_id} rejected the request")
return graceful_complete(parent_id)


async def enqueue_request(
client: AsyncClient,
parent_flow: AdvancedShortLiveParentFlow,
parent_id: str,
request: str,
) -> bool:
try:

例子: examples/python/dex_examples/patterns/parallel-subflows/submit_request_flow.py

下一步