natshelper

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-nats-helper

Helpers for using NATS service

Documentation

Index

Constants

View Source
const (
	ConfigURL    = "url"
	ConfigPrefix = "prefix"
)

Variables

View Source
var (
	// ErrDuplicateProcessor - processor already exists
	ErrDuplicateProcessor = errors.New("processor is duplicate")
	ErrNotConnected       = errors.New("nats: not connected")
)

Functions

This section is empty.

Types

type Kind

type Kind string

Kind - kind of notification

const (
	// KindInfo - informational message
	KindInfo Kind = "inf"
	// KindError - error
	KindError Kind = "err"
	// KindRequest - request of some kind
	KindRequest Kind = "req"
	// KindSystem - system message
	KindSystem Kind = "sys"
	// KindEvent - some event in the system
	KindEvent Kind = "evn"
	// KindUnknown may be used in case if kind is not in the list
	KindUnknown Kind = "unknown"
	// KindAbsolut should be used if prefix should not be added
	KindAbsolut Kind = "abs"
)

describes possible kinds

type Option

type Option int

type Processor

type Processor interface {
	// ProcessNatsMessage is called on incoming message
	ProcessNatsMessage(subject string, msg []byte) error
}

Processor is interface for processor of incoming messages

type RequestProcessor

type RequestProcessor interface {
	ProcessNatsRequest(topic string, request []byte) (response []byte, err error)
}

RequestProcessor is interface for processor of incoming requests

type Server

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

Server wrapper around NATS server

func NewServer

func NewServer(cfg map[string]interface{}, params ...any) (*Server, error)

NewServer inits the nats with given configuration and tries to connect

func (*Server) AddProcessor

func (ns *Server) AddProcessor(name string, kind Kind, subject string, processor Processor, replace bool) error

AddProcessor adds the incoming messages processor

func (*Server) AddRequestProcessor

func (ns *Server) AddRequestProcessor(name string, subject string, processor RequestProcessor, replace bool) error

AddRequestProcessor adds the incoming request processor

func (*Server) Publish

func (ns *Server) Publish(subject string, kind Kind, message []byte, options ...Option) error

Publish allows to publish a message on given topic

func (*Server) RequestSync

func (ns *Server) RequestSync(
	subject string,
	message []byte,
	timeout time.Duration,
	options ...Option,
) (resp []byte, err error)

func (*Server) SetLogger

func (ns *Server) SetLogger(logger *zap.Logger)

func (*Server) Stop

func (ns *Server) Stop()

Stop stops the server

Jump to

Keyboard shortcuts

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