common

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2019 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Fmt = fmt.Sprintf

Fmt shorthand, XXX DEPRECATED

Functions

func EnsureDir

func EnsureDir(dir string, mode os.FileMode) error

func Exit

func Exit(s string)

func FileExists

func FileExists(filePath string) bool

func IsHex

func IsHex(s string) bool

IsHex returns true for non-empty hex-string prefixed with "0x"

func LeftPadString

func LeftPadString(s string, totalLength int) string

LeftPadString adds spaces to the left of a string to make it length totalLength

func MaxInt

func MaxInt(a, b int) int

func MinInt

func MinInt(a, b int) int

func MustWriteFile

func MustWriteFile(filePath string, contents []byte, mode os.FileMode)

func PanicConsensus

func PanicConsensus(v interface{})

Indicates a failure of consensus. Someone was malicious or something has gone horribly wrong. These should really boot us into an "emergency-recover" mode

func PanicCrisis

func PanicCrisis(v interface{})

A panic here means something has gone horribly wrong, in the form of data corruption or failure of the operating system. In a correct/healthy system, these should never fire. If they do, it's indicative of a much more serious problem.

func PanicQ

func PanicQ(v interface{})

For those times when we're not sure if we should panic

func PanicSanity

func PanicSanity(v interface{})

A panic resulting from a sanity check means there is a programmer error and some guarantee is not satisfied.

func Parallel

func Parallel(tasks ...func())

func RandBytes

func RandBytes(n int) []byte

func RandInt

func RandInt() int

func RandStr

func RandStr(length int) string

Constructs an alphanumeric string of given length.

func RightPadString

func RightPadString(s string, totalLength int) string

RightPadString adds spaces to the right of a string to make it length totalLength

func StripHex

func StripHex(s string) string

StripHex returns hex string without leading "0x"

func TrapSignal

func TrapSignal(cb func())

TrapSignal catches the SIGTERM and executes cb function. After that it exits with code 1.

func WriteFile

func WriteFile(filePath string, contents []byte, mode os.FileMode) error

Types

type BaseService

type BaseService struct {
	Logger log.Logger

	Quit chan struct{}
	// contains filtered or unexported fields
}

Classical-inheritance-style service declarations. Services can be started, then stopped, then optionally restarted.

Users can override the OnStart/OnStop methods. In the absence of errors, these methods are guaranteed to be called at most once. If OnStart returns an error, service won't be marked as started, so the user can call Start again.

A call to Reset will panic, unless OnReset is overwritten, allowing OnStart/OnStop to be called again.

The caller must ensure that Start and Stop are not called concurrently.

It is ok to call Stop without calling Start first.

Typical usage:

type FooService struct {
	BaseService
	// private fields
}

func NewFooService() *FooService {
	fs := &FooService{
		// init
	}
	fs.BaseService = *NewBaseService(log, "FooService", fs)
	return fs
}

func (fs *FooService) OnStart() error {
	fs.BaseService.OnStart() // Always call the overridden method.
	// initialize private fields
	// start subroutines, etc.
}

func (fs *FooService) OnStop() error {
	fs.BaseService.OnStop() // Always call the overridden method.
	// close/destroy private fields
	// stop subroutines, etc.
}

func NewBaseService

func NewBaseService(logger log.Logger, name string, impl Service) *BaseService

func (*BaseService) IsRunning

func (bs *BaseService) IsRunning() bool

Implements Service

func (*BaseService) OnReset

func (bs *BaseService) OnReset() error

Implements Service

func (*BaseService) OnStart

func (bs *BaseService) OnStart() error

Implements Service NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStart()

func (*BaseService) OnStop

func (bs *BaseService) OnStop()

Implements Service NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStop()

func (*BaseService) Reset

func (bs *BaseService) Reset() (bool, error)

Implements Service

func (*BaseService) SetLogger

func (bs *BaseService) SetLogger(l log.Logger)

func (*BaseService) Start

func (bs *BaseService) Start() (bool, error)

Implements Servce

func (*BaseService) Stop

func (bs *BaseService) Stop() bool

Implements Service

func (*BaseService) String

func (bs *BaseService) String() string

Implements Servce

func (*BaseService) Wait

func (bs *BaseService) Wait()

type CMap

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

CMap is a goroutine-safe map

func NewCMap

func NewCMap() *CMap

func (*CMap) Clear

func (cm *CMap) Clear()

func (*CMap) Delete

func (cm *CMap) Delete(key string)

func (*CMap) Get

func (cm *CMap) Get(key string) interface{}

func (*CMap) Has

func (cm *CMap) Has(key string) bool

func (*CMap) Keys

func (cm *CMap) Keys() []string

func (*CMap) Set

func (cm *CMap) Set(key string, value interface{})

func (*CMap) Size

func (cm *CMap) Size() int

func (*CMap) Values

func (cm *CMap) Values() []interface{}

type Service

type Service interface {
	Start() (bool, error)
	OnStart() error

	Stop() bool
	OnStop()

	Reset() (bool, error)
	OnReset() error

	IsRunning() bool

	String() string

	SetLogger(log.Logger)
}

type StackError

type StackError struct {
	Err   interface{}
	Stack []byte
}

func (StackError) Error

func (se StackError) Error() string

func (StackError) String

func (se StackError) String() string

type ThrottleTimer

type ThrottleTimer struct {
	Name string
	Ch   chan struct{}
	// contains filtered or unexported fields
}

ThrottleTimer fires an event at most "dur" after each .Set() call. If a short burst of .Set() calls happens, ThrottleTimer fires once. If a long continuous burst of .Set() calls happens, ThrottleTimer fires at most once every "dur".

func NewThrottleTimer

func NewThrottleTimer(name string, dur time.Duration) *ThrottleTimer

func (*ThrottleTimer) Set

func (t *ThrottleTimer) Set()

func (*ThrottleTimer) Stop

func (t *ThrottleTimer) Stop() bool

For ease of .Stop()'ing services before .Start()'ing them, we ignore .Stop()'s on nil ThrottleTimers

func (*ThrottleTimer) Unset

func (t *ThrottleTimer) Unset()

Jump to

Keyboard shortcuts

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