revrpc

package
v0.1.12 Latest Latest
Warning

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

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

Documentation

Overview

Package revrpc provides jsonrpc library that matches ns_server's json_rpc_connection module. It allows golang service to connect to ns_server and receive jsonrpc requests via that connection.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRunning = errors.New("service is already running")

ErrAlreadyRunning is returned from Run method to indicate that given Service instance is already running.

View Source
var ErrRevRpcUnauthorized = errors.New("invalid revrpc credentials")

Functions

func BabysitService

func BabysitService(setupBody ServiceSetupCallback, svc *Service, errorPolicy BabysitErrorPolicy) error

BabysitService function runs given service instance, restarting it as needed if allowed by given BabysitErrorPolicy. nil can be passed to errorPolicy argument, in which case value of DefaultBabysitErrorPolicy is used.

Types

type BabysitErrorPolicy

type BabysitErrorPolicy interface {
	// New method is expected to initialize and return
	// ErrorPolicyFn instance. That instance will be used by
	// BabysitService invocation until ErrorPolicyFn returns
	// non-nil error.
	New() ErrorPolicyFn
}

BabysitErrorPolicy represents error handling policy.

var DefaultBabysitErrorPolicy BabysitErrorPolicy = DefaultErrorPolicy{
	RestartsToExit:       -1,
	SleepBetweenRestarts: time.Second,
	LogPrint:             log.Print,
}

DefaultBabysitErrorPolicy is BabysitErrorPolicy instance that is used by default. It's initial value is "suitably configured" DefaultErrorPolicy instance.

var NoRestartsBabysitErrorPolicy BabysitErrorPolicy = FnBabysitErrorPolicy(func(err error) error { return err })

NoRestartsBabysitErrorPolicy is error policy that always forbids restarts.

type DefaultErrorPolicy

type DefaultErrorPolicy struct {
	// RestartsToExit determines how many restarts this error
	// policy will do before giving up. Negative value means
	// restart infinitely.
	RestartsToExit int
	// SleepBetweenRestarts specifies duration to sleep between
	// restarts.
	SleepBetweenRestarts time.Duration
	// LogPrint function, if non-nil, is used by
	// DefaultErrorPolicy to log it's events & decisions.
	// log.Print function is one suitable implementation.
	LogPrint func(args ...interface{})
	// contains filtered or unexported fields
}

DefaultErrorPolicy is default configurable implementation of BabysitErrorPolicy.

func (DefaultErrorPolicy) New

New method of DefaultErrorPolicy implements New method of BabysitErrorPolicy interface. It returns ErrorPolicyFn that will allow configured number of restarts and will sleep configured duration between restarts.

type ErrorPolicyFn

type ErrorPolicyFn func(err error) error

ErrorPolicyFn function is used to make error handling decision in BabysitService. Function returns nil to "eat" error and case BabysitService to restart Service. Otherwise, returned error provides return value for BabysitService invocation.

type FnBabysitErrorPolicy

type FnBabysitErrorPolicy ErrorPolicyFn

FnBabysitErrorPolicy type adapts ErrorPolicyFn to BabysitErrorPolicy interface.

func (FnBabysitErrorPolicy) New

New method simply returns "this" function.

type HttpError added in v0.1.11

type HttpError struct {
	StatusCode int
	Message    string
}

func (*HttpError) Error added in v0.1.11

func (e *HttpError) Error() string

type RevrpcSvc added in v0.1.5

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

func (*RevrpcSvc) UpdateURL added in v0.1.5

func (s *RevrpcSvc) UpdateURL(urlChange URLChange, res *URLChangeResult) error

type Service

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

Service type represents specific configured instance of revrpc.

func GetDefaultServiceFromEnv

func GetDefaultServiceFromEnv(serviceName string) (*Service, error)

GetDefaultServiceFromEnv returns Service instance that connects to ns_server according to CBAUTH_REVRPC_URL environment variable. Trying to obtain same service twice will return error. I.e. you're supposed to get your Service instance once and only once and hold it forever.

func MustService

func MustService(connectURL string) *Service

MustService is like NewService except that it panics on errors. I.e. it is useful in cases where errors are not expected.

func NewService

func NewService(connectURL string) (*Service, error)

NewService creates and returns Service instance that connects to given ns_server url (which is expected to have creds encoded). Returns error if url is malformed. Does not actually connect to ns_server, so it will succeed even if ns_server is not running or if creds are not valid admin creds.

func (*Service) Disconnect added in v0.1.11

func (s *Service) Disconnect() error

func (*Service) Run

func (s *Service) Run(setupBody ServiceSetupCallback) error

Run method connects to ns_server, sets up json rpc instance and handles rpc requests loop until connection is alive. Returned error is always non-nil. In case connection was closed by ns_server io.EOF is returned.

type ServiceSetupCallback

type ServiceSetupCallback func(server *rpc.Server) error

ServiceSetupCallback type defines functions that can be passed to Run and to BabysitService. Function is expected to register "rpc-exported" object instances with given rpc server.

type URLChange added in v0.1.5

type URLChange struct {
	NewURL string `json:"newURL"`
}

type URLChangeResult added in v0.1.5

type URLChangeResult struct {
	IsSucc      bool   `json:"isSucc"`
	Description string `json:"description"`
}

Jump to

Keyboard shortcuts

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