hemera

package module
v0.0.0-...-c0cfcfc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2018 License: MIT Imports: 9 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const (
	// RequestType represent the request with default request / reply semantic
	RequestType = "request"
	// PubsubType represent the request with publish / subscribe semantic
	PubsubType = "pubsub"
	// RequestTimeout is the maxiumum act timeout in miliseconds
	RequestTimeout    = 2000
	DepthIndexing     = true
	InsertionIndexing = false
)

Variables

This section is empty.

Functions

func ArgInfo

func ArgInfo(cb Handler) ([]reflect.Type, int)

Dissect the cb Handler's signature

func CleanPattern

func CleanPattern(s *structs.Struct) interface{}

Types

type Context

type Context struct {
	Meta     Meta
	Delegate Delegate
	Trace    Trace
	Error    error
}

type Delegate

type Delegate map[string]interface{}

type Error

type Error struct {
	Name    string `json:"name"`
	Message string `json:"message"`
	Code    int16  `json:"code"`
}

func NewError

func NewError(name, message string, code int16) *Error

func NewErrorSimple

func NewErrorSimple(message string) *Error

func (*Error) Error

func (e *Error) Error() string

type Handler

type Handler interface{}

type Hemera

type Hemera struct {
	Conn   *nats.Conn
	Router *router.Router
	Opts   Options
}

func CreateHemera

func CreateHemera(conn *nats.Conn, options ...Option) (Hemera, error)

New create a new Hemera struct

func (*Hemera) Act

func (h *Hemera) Act(args ...interface{}) *Context

Act is a method to send a message to a NATS subscriber which the specific topic

func (*Hemera) Add

func (h *Hemera) Add(p interface{}, cb Handler) (*nats.Subscription, error)

Add is a method to subscribe on a specific topic

type Meta

type Meta map[string]interface{}

type Option

type Option func(*Options) error

Option is a function on the options for hemera

func IndexingStrategy

func IndexingStrategy(isDeep bool) Option

func Timeout

func Timeout(t time.Duration) Option

Timeout is an Option to set the timeout for a act request

type Options

type Options struct {
	Timeout          time.Duration
	IndexingStrategy bool
}

func GetDefaultOptions

func GetDefaultOptions() Options

type Reply

type Reply struct {
	// contains filtered or unexported fields
}

func (*Reply) Send

func (r *Reply) Send(payload interface{})

type Trace

type Trace struct {
	TraceID      string `json:"traceId"`
	ParentSpanID string `json:"parentSpanId"`
	SpanID       string `json:"spanId"`
	Timestamp    int64  `json:"timestamp"`
	Service      string `json:"service"`
	Method       string `json:"method"`
	Duration     int64  `json:"duration"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL