go-hemera
go-hemera copied to clipboard
🔬Writing reliable & fault-tolerant microservices with https://nats.io
A Go microservices toolkit for the NATS messaging system
Status: Experimental
Install
go get ./..
go get github.com/nats-io/gnatsd/server
Example
type MathPattern struct {
Topic string
Cmd string
}
type RequestPattern struct {
Topic string
Cmd string
A int
B int
Meta server.Meta
Delegate server.Delegate
}
type Response struct {
Result int
}
nc, _ := nats.Connect(nats.DefaultURL)
hemera, _ := server.CreateHemera(nc, server.Timeout(2000), server.IndexingStrategy(DepthIndexing)...)
// Define the pattern of your action
pattern := MathPattern{Topic: "math", Cmd: "add"}
hemera.Add(pattern, func(req *RequestPattern, reply server.Reply, context *server.Context) {
// Build response
result := Response{Result: req.A + req.B}
// Add meta informations
context.Meta["key"] = "value"
// Send it back
reply.Send(result)
})
// Define the call of your RPC
requestPattern := RequestPattern{
Topic: "math",
Cmd: "add",
A: 1,
B: 2,
Meta: server.Meta{ "Test": 1 },
Delegate: server.Delegate{ "Test": 2 },
}
res := &Response{} // Pointer to struct
ctx := hemera.Act(requestPattern, res)
res = &Response{}
ctx = hemera.Act(requestPattern, res, ctx)
log.Printf("Response %+v", res)
Pattern matching
We implemented two indexing strategys
-
depth order
match the entry with the most properties first. -
insertion order
match the entry with the least properties first.(default)
TODO
- [X] Setup nats server for testing
- [X] Implement Add and Act
- [X] Create Context (trace, meta, delegate) structures
- [X] Use tree for pattern indexing
- [X] Support indexing by depth order
- [X] Support indexing by insetion order
- [X] Clean request pattern from none primitive values
- [X] Meta & Delegate support
- [X] Implement basic pattern matching (router)
- [ ] Implement router
remove
method
Credits
- Bloomrun the pattern matching library for NodeJs