Share data
To share data between nodes, it is recommended to use the Metadata
or Data
of msg, but if you need to pass semaphores or pointers, you can also use the context.Context
method.
Usage example:
ruleEngine.OnMsg(msg, types.WithContext(context.WithValue(context.Background(), shareKey, shareValue)))
1
Component to get shared data:
func (n *TimeNode) OnMsg(ctx types.RuleContext, msg types.RuleMsg) {
//context method to get shared data
v1 := ctx.GetContext().Value(shareKey)
//context method to modify shared data
modifyCtx := context.WithValue(ctx.GetContext(), addShareKey, addShareValue)
ctx.SetContext(modifyCtx)
//msg.Metadata method to get shared data
v2 :=msg.Metadata.GetValue("timestamp")
//msg.Metadata method to modify shared data
msg.Metadata.PutValue("timestamp", time.Now().Format(time.RFC3339))
ctx.TellSuccess(msg)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/23, 10:13:01