/go-hemera

🔬Writing reliable & fault-tolerant microservices with https://nats.io

Primary LanguageGoMIT LicenseMIT

Hemera

Build Status License MIT

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

  • Setup nats server for testing
  • Implement Add and Act
  • Create Context (trace, meta, delegate) structures
  • Use tree for pattern indexing
  • Support indexing by depth order
  • Support indexing by insetion order
  • Clean request pattern from none primitive values
  • Meta & Delegate support
  • Implement basic pattern matching (router)
  • Implement router remove method

Credits

  • Bloomrun the pattern matching library for NodeJs