orion

package module
v0.0.0-...-31f5ead Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2020 License: MIT Imports: 20 Imported by: 0

README

Build Status Test coverage GoDoc

Installation

$ go get github.com/gig/orion-go-sdk

or

$ dep ensure -add github.com/gig/orion-go-sdk

Basic example

Note - You will need to have running all of the orion dependencies

Add the following into foo.go and run go run foo.go --verbose

package main

import (
	orion "github.com/gig/orion-go-sdk"
	"github.com/gig/orion-go-sdk/interfaces"
	"github.com/gig/orion-go-sdk/request"
	"github.com/gig/orion-go-sdk/response"
)

type params struct {
	A int `msgpack:"a"`
	B int `msgpack:"b"`
}

type addReq struct {
	request.Request
	Params params `msgpack:"params"`
}

type addPayload struct {
	Result int `msgpack:"result"`
}

type addRes struct {
	response.Response
	Payload addPayload `msgpack:"payload"`
}

func main() {
	svc := orion.New("calc")

	factory := func() interfaces.Request {
		return &addReq{}
	}

	handle := func(req *addReq) *addRes {
		return &addRes{
			Payload: addPayload{
				Result: req.Params.A + req.Params.B,
			},
		}
	}

	svc.Handle("add", handle, factory)

	svc.Listen(func() {
		svc.Logger.CreateMessage("ready").Send()
	})
}

Then add the following into bar.go and run go run bar.go

package main

import (
	orion "github.com/gig/orion-go-sdk"
	"github.com/gig/orion-go-sdk/request"
	"github.com/gig/orion-go-sdk/response"
)

type params struct {
	A int `msgpack:"a"`
	B int `msgpack:"b"`
}

type addReq struct {
	request.Request
	Params params `msgpack:"params"`
}

type addPayload struct {
	Result int `msgpack:"result"`
}

type addRes struct {
	response.Response
	Payload addPayload `msgpack:"payload"`
}

func main() {
	svc := orion.New("calc")

	req := &addReq{
		Params: params{
			A: 1,
			B: 2,
		},
	}
	req.Path = "/calc/add"

	res := &addRes{}

	svc.Call(req, res)

	println(res.Payload.Result) // 3
}

You can find more examples in the test files.

Health checks

Support for health checking is present if the services are running with the environment variable WATCHDOG=true. Also, a watchdog must be running in the same environment as the service.

For instance, you could write an elasticsearch health check as:

import (
    "context"
	"time"

	el "gopkg.in/olivere/elastic.v5"
	"github.com/gig/orion-go-sdk"
	"github.com/gig/orion-go-sdk/health"
)

func HealthCheck(client * el.Client) (*elastic.ClusterHealthResponse, error) {
	h := client.ClusterHealth()
	return h.Do(context.Background())
}

func ElasticHealthFactoryChecker(service * orion.Service, client * el.Client)  {
	service.RegisterHealthCheck(&health.Dependency{
		Name: "elasticsearch",
		Timeout: 30 * time.Second,
		CheckIsWorking: func () (string, *orion.Error) {
			res, err := HealthCheck(client)

			if err != nil {
				return "Health check went wrong: " + err.Error(), orion.ServiceError(string(health.HC_CRIT))
			}

			if res.Status == "yellow" {
				return "Non-green status: " + res.Status, orion.ServiceError(string(health.HC_WARN))
			}

			if res.Status == "red" {
				return "Non-green status: " + res.Status, orion.ServiceError(string(health.HC_CRIT))
			}

			return "", nil
		},
	})
}

Tests

$ go test -v .

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ServiceError = oerror.New

ServiceError for orion

Functions

func DefaultServiceOptions

func DefaultServiceOptions(opt *Options)

DefaultServiceOptions setup

func UniqueName

func UniqueName(name string, uniqueID string) string

UniqueName for given name and unique id

Types

type Error

type Error = oerror.Error

Error for orion

type Factory

type Factory = func() interfaces.Request

Factory func type - the one that creates the req obj

type Option

type Option func(*Options)

Option type

func SetCodec

func SetCodec(codec interfaces.Codec) Option

SetCodec for orion

func SetTransport

func SetTransport(transport interfaces.Transport) Option

SetTransport for orion

type Options

type Options struct {
	Codec               interfaces.Codec
	Transport           interfaces.Transport
	Logger              interfaces.Logger
	DisableHealthChecks bool
	HTTPPort            int
}

Options object

type Request

type Request = request.Request

Request from microservice

type Response

type Response = response.Response

Response from microservice

type Service

type Service struct {
	ID                  string
	Name                string
	Timeout             int
	Codec               interfaces.Codec
	Transport           interfaces.Transport
	Logger              interfaces.Logger
	ThreadPool          *ants.PoolWithFunc
	HealthChecks        []health.Dependency
	StopHealthCheck     chan struct{}
	HTTPServer          *http.Server
	HTTPPort            int
	DisableHealthChecks bool
}

Service for orion

func New

func New(name string, options ...Option) *Service

New orion service

func (*Service) Call

func (s *Service) Call(req interfaces.Request, raw interface{})

Call orion service

func (*Service) Close

func (s *Service) Close()

Close the transport protocol

func (*Service) Decode

func (s *Service) Decode(data []byte, to interface{}) error

Decode bytes to passed interface

func (*Service) Emit

func (s *Service) Emit(topic string, data interface{}) error

Emit to services

func (*Service) Handle

func (s *Service) Handle(path string, handler interface{}, factory Factory)

Handle has enabled logging. What that means is when the request arrives the service will log the request including the raw params. Once the response is returned, the service will check for error and if there is such, the error will be logged

func (*Service) HandleWithoutLogging

func (s *Service) HandleWithoutLogging(path string, handler interface{}, factory Factory)

HandleWithoutLogging enabled

func (*Service) Listen

func (s *Service) Listen(callback func())

Listen to the transport protocol

func (*Service) On

func (s *Service) On(topic string, handler func([]byte))

On service emit

func (*Service) OnClose

func (s *Service) OnClose(handler func())

OnClose adds a handler to a transport connection closed event

func (*Service) RegisterHealthCheck

func (s *Service) RegisterHealthCheck(check *health.Dependency)

func (*Service) String

func (s *Service) String() string

String return the name and the id of the service

func (*Service) SubscribeForRawMsg

func (s *Service) SubscribeForRawMsg(topic string, handler func(interface{}))

SubscribeForRawMsg is like service.On except that it receives the raw messages specific for the transport protocol instead of the message payload

Directories

Path Synopsis
codec

Jump to

Keyboard shortcuts

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