路由转发助手 (onepath_forward)
onepath_forward 把入站样本一行式转发到目标发布者,常用于在订阅回调里做中继 / 网关。它在内部沿用发布者的默认编码、透传载荷与用户附件,并自动续接链路追踪上下文。
onepath_forward
c
int onepath_forward(onepath_sample_t *in_sample, onepath_publisher_t out_pub);把入站 sample 转发到目标 publisher。
- 参数:
in_sample— 入站样本(通常来自订阅回调参数)out_pub— 目标 publisher 句柄
- 返回值:
ONEPATH_OK成功,其他值表示错误 - 行为细节:
- 载荷与用户附件透传
- 编码沿用 publisher 默认值(不强制覆盖)
- 内部以
onepath_publisher_write发出
适用位置
建议仅在订阅回调或应答回调内调用。在这些回调内调用时,转发出去的消息会自动续接当前链路的追踪上下文(trace_id 跨跳不变),无需任何额外代码。追踪机制详见 链路追踪。
用法
c
static void router_cb(onepath_sample_t *sample, void *userdata) {
onepath_publisher_t next_hop = (onepath_publisher_t)userdata;
onepath_forward(sample, next_hop); /* 一行转发 + 自动续接链路 */
onepath_sample_release(sample); /* 转发后仍须释放入站样本 */
}
/* 声明下一跳发布者, 并把它作为 userdata 传给订阅回调 */
onepath_publisher_t next_hop;
onepath_declare_publisher(s, &next_hop, "out/stream", NULL);
onepath_subscriber_t sub;
onepath_subscribe(s, &sub, "in/stream/**", router_cb, next_hop);释放入站样本
onepath_forward 不接管入站样本的所有权,转发后仍须照常调用 onepath_sample_release(sample)。见 内存管理。