loop · 循环
loop把一段子图(body)按列表逐项或按次数跑 N 轮,是"逐个收件人发送""重试直到成功"这类逻辑的写法。
输出:{"iteration": 已跑轮数};被 break 提前收工时 {"iteration": 已跑轮数, "broken": true}
在画布上怎么配
截图来自控制台「工作流编排」:选择本页示例对应的路由,点击画布上的节点,右侧就是它的配置表单。点击图片可看大图。
- 迭代方式:两个页签 =
with.over(遍历集合)与with.count(按次数)。 - 循环体:把成员连成子图,成员回连
loop的body边表示一轮结束,done边接后续。 - 最大迭代:与运行护栏取更小值,超出即节点失败。
什么时候用
- payload 里有列表,要逐项做同一件事(收件人、告警条目、分片)。
- 同一动作要重复 N 次,且轮与轮之间可能要等一下(重试、轮询)。
- 需要"跑一轮看结果,成了就停"——配 break 用;想让同一条消息分发给多个渠道,用 fanout 拉出多条边(它只做数据分流,不产生并发执行)。
字段
| 字段 | 类型 | 必填 | 默认 | 说明 |
|---|---|---|---|---|
over | 表达式 | 与 count 二选一 | — | 求值得到列表,逐项迭代 |
count | 整数或表达式 | 与 over 二选一 | — | 按次数迭代,item = 0..N-1 |
as | string | 否 | item | 自定义当前项绑定名(仅 body 内可见) |
body | string[] | 是 | — | body 成员归属(校验闭包、旧图数组序用) |
maxIterations | int | 否 | 100 | 迭代上限,只能收紧 |
出边只有三种:body(进入 body 成员)、done(循环结束后继续)、error。
两种迭代来源
over:逐项迭代
{ "id": "l1", "type": "loop", "with": {
"over": "payload.recipients", "as": "rcpt",
"body": ["tpl_one", "send_one"] } }over 求值结果必须是列表(切片/数组);求值为 null 按空列表处理(0 轮,直接走 done); 求值成对象/字符串/数字 → 节点失败(表达式 "…" 结果不是列表(得到 string))。迭代内 item = 当前元素、index = 下标(从 0 起)。
count:按次数迭代
{ "id": "retry", "type": "loop", "with": { "count": 5, "body": ["send_try", "br_done"] } }count 是数字字面量或求值为整数的表达式("count": "payload.retry_times"), 此时 item 与 index 都是 0..N-1。固定小次数也可写 "over": "[0,1,2]",动态次数请用 count。
两个来源并存 → 400(with.over 与 with.count 只能二选一);两者皆缺 → 400(缺少 with.over 或 with.count)。
as:自定义绑定名
默认绑定名是 item;写 "as": "rcpt" 后,body 内表达式与模板都用 rcpt(rcpt.email、{{ .rcpt.name }})。 别名只注入给该 loop 的 body 成员,loop 自身的 over/count 表达式里没有它;index 始终可用。
body 成员与执行序
循环体内画什么顺序就跑什么顺序——成员之间的边就是执行图,with.body 数组只管成员归属。
| 情形 | 判定 | 执行序 |
|---|---|---|
| 边驱动(推荐) | 成员之间存在成员→成员边 | 入口 = loop 的 body 出边目标(多条按边序),沿成员边推进;同层多就绪按节点定义序串行 |
| 数组序兼容(旧图) | 成员之间零边 | 按 with.body 数组序逐个执行 |
| 混用 | 部分成员有边、部分是孤岛 | 连通部分按边跑完,孤立成员按 with.body 数组序追加(已有记录不重跑) |
- 迭代是串行的:一轮排空(挂起则唤醒后续跑完)才进下一轮;同一轮内多个就绪成员也按节点定义序串行。
- 就绪判定 = 全部成员前驱在本迭代落定 + 任一入边激活且
when匹配;就绪但无入边激活 →skipped。 when标签在体内照常路由:condition 走true/false、switch 走 case 键/default、成功节点走空边。- 失败节点不走空边:它的下游在本迭代
skipped——失败要么被兜底吸收,要么炸循环。 - 成员回连 loop 的
body边是迭代收尾的视觉标记,迭代在所有成员排空后才结束。
最小示例
{
"trigger": { "type": "api" },
"nodes": [
{ "id": "l1", "type": "loop", "with": { "over": "payload.recipients", "body": ["s1"] } },
{ "id": "s1", "type": "send", "with": { "channel": "im-ops", "template": "通知:{{ .payload.title }}" } }
],
"edges": [
{ "from": "l1", "to": "s1", "when": "body" },
{ "from": "s1", "to": "l1", "when": "body" }
]
}单成员循环,每个收件人跑一遍 s1。注意 loop 的出边必须带标签,空标签 → 400(loop 出边仅 body|done|error)。
输出与下游引用
| 场景 | loop 输出 |
|---|---|
| 正常跑完 | {"iteration": N},N = 跑过的轮数 |
| 被 break 收工 | {"iteration": N, "broken": true} |
下游用 nodes.l1.iteration。body 节点的输出不聚合:nodes.s1 指向最后一轮的输出(引擎按最新迭代取值),逐轮结果见运维与调试。
maxIterations 护栏
实际上限 = min(with.maxIterations, limits.maxLoopIterations, 100)——只能收紧不能放宽,100 是硬顶。 超出即 loop 节点失败:loop 超过最大迭代次数: 120 > 100;接了 error 边由外层兜底,否则 run 失败。 with.maxIterations 或 limits.maxLoopIterations 取负 → 400(… 必须 > 0),0 表示取缺省(100)。
body 失败与 catch 语义
| body 节点失败时 | 行为 |
|---|---|
接了 error 兜底(when: "error" → error-handler) | 跑兜底分支(error-handler 及其空边下游),本迭代算已处理,循环继续 |
| 没接兜底 | 循环失败(loop 的 error 边仍可在外层接住,否则 run failed) |
| 兜底分支自身失败 | 双重故障 → 循环失败(不吞) |
| 兜底分支里 break | 跳出,loop 提前成功(broken: true) |
兜底分支不必也不应放进 with.body:它由 error 边激活,每轮失败时跑一次;放进 body 也兼容(已成功的兜底节点被跳过)。 兜底上下文在 nodes.<eh>.error 与 nodes.<eh>.failed_node。
与 delay / wait-until / send(await) 组合
迭代内节点挂起时,loop 跟着进入 waiting(保留本轮 iteration 锚点),run 置 waiting,到期续跑: 同一迭代内已完成的节点不重跑,进度以 (节点, 迭代序号) 记录为准。wait-until 的 at 是绝对时刻, 第二轮再执行同一节点时若已过点就立即通过({"waited": false});等待时长计入 limits.deadline。
完整示例:收件人列表展开
{
"trigger": { "type": "api" },
"nodes": [
{ "id": "l1", "type": "loop", "with": {
"over": "payload.recipients", "as": "rcpt", "maxIterations": 50,
"body": ["tpl_one", "send_one"] } },
{ "id": "tpl_one", "type": "template", "with": {
"template": "致 {{ .rcpt.name }}:{{ .payload.title }}" } },
{ "id": "send_one", "type": "send", "with": {
"channel": "im-ops", "await": true, "target": "{{ .rcpt.id }}",
"template": "{{ .nodes.tpl_one.text }}" } },
{ "id": "eh", "type": "error-handler" },
{ "id": "warn_one", "type": "send", "with": {
"channel": "im-oncall",
"template": "收件人 {{ .rcpt.id }} 失败:{{ .nodes.eh.error }}" } }
],
"edges": [
{ "from": "l1", "to": "tpl_one", "when": "body" },
{ "from": "tpl_one", "to": "send_one", "when": "body" },
{ "from": "send_one", "to": "l1", "when": "body" },
{ "from": "send_one", "to": "eh", "when": "error" },
{ "from": "eh", "to": "warn_one" }
]
}target 目前按字面投递
send.with.target 原样写进投递任务、不做表达式求值与模板渲染(以 internal/flow/node.go 的 execSend 为准), 所以 "{{ .rcpt.id }}" 现在等于把这段文本当接收方 ID;稳妥做法是让渠道配默认接收方,再用每轮渲染的正文区分收件人。
完整示例:重试直到成功
5 轮「发送 → 成功就跳出 → 失败则等 30s 再下一轮」,第 5 轮仍失败就告警。
{
"trigger": { "type": "api" },
"nodes": [
{ "id": "retry", "type": "loop", "with": {
"count": 5, "maxIterations": 5, "body": ["send_try", "br_done"] } },
{ "id": "send_try", "type": "send", "with": {
"channel": "sms-oncall", "await": true, "target": "oncall-1",
"template": "{{ .payload.title }}(第 {{ .index }} 次尝试)" } },
{ "id": "br_done", "type": "break", "with": { "expr": "nodes.send_try.delivery_id != ''" } },
{ "id": "eh", "type": "error-handler" },
{ "id": "last_round", "type": "condition", "with": { "expr": "index == 4" } },
{ "id": "alert", "type": "send", "with": { "channel": "im-oncall", "template": "5 轮重试仍失败:{{ .nodes.eh.error }}" } },
{ "id": "wait_next", "type": "delay", "with": { "duration": "30s" } },
{ "id": "report", "type": "send", "with": { "channel": "im-ops", "template": "循环结束,共 {{ .nodes.retry.iteration }} 轮" } }
],
"edges": [
{ "from": "retry", "to": "send_try", "when": "body" },
{ "from": "send_try", "to": "br_done", "when": "body" },
{ "from": "br_done", "to": "retry", "when": "body" },
{ "from": "send_try", "to": "eh", "when": "error" },
{ "from": "eh", "to": "last_round" },
{ "from": "last_round", "to": "alert", "when": "true" },
{ "from": "last_round", "to": "wait_next", "when": "false" },
{ "from": "retry", "to": "report", "when": "done" }
]
}读法:send_try 成功才激活 br_done,break 条件恒真 → 第 N 轮成功即收工,retry 输出 {"iteration": N, "broken": true}; 失败则 send_try 的下游被跳过、兜底链判定是否最后一轮,不是就 delay 挂起、下一轮重来。第 3 轮成功时 report 读到 iteration = 3。
常见错误
- 把兜底分支放进
with.body又画 body 入边:兜底节点会被当普通成员跑一遍,正确做法是只由error边激活。 over结果不是列表:对象/字符串/数字 → 节点失败(不是 400 文案),先用 transform 兜成列表。- 嵌套 loop 未实现:body 里放 loop → 400(
嵌套 loop 未实现(M4c/后续))。 - loop 出边的标签:空标签或不是
body/done/error→ 400;done指向 body 成员也是 400。 - 同一节点出现在两个 loop 的 body:400(
同时出现在 loop … 与 loop … 的 body 中)。
