resilience

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 14 Imported by: 0

README

resilience

A powerful read and handle shared_ptr & retry workflow for golang.


Install

With a correctly configured Go toolchain:

go get -u github.com/searKing/golib/x/resilience

License

MIT licensed. See the LICENSE file for details.

Documentation

Overview

Package resilience provides helpers for dealing with resilience. This code is borrowed from https://github.com/ory/x/tree/master/resilience/retry.go

Index

Constants

View Source
const (
	// DefaultConnectTimeout is the default timeout to establish a connection to
	// a ZooKeeper node.
	DefaultResilienceConstructTimeout = 0
	// DefaultSessionTimeout is the default timeout to keep the current
	// ZooKeeper session alive during a temporary disconnect.
	DefaultResilienceTaskMaxRetryDuration = 15 * time.Second

	DefaultTaskRetryTimeout      = 1 * time.Second
	DefaultTaskRescheduleTimeout = 1 * time.Second
)

Variables

View Source
var (
	ErrEmptyValue       = fmt.Errorf("empty value")
	ErrAlreadyShutdown  = fmt.Errorf("already shutdown")
	ErrNotReady         = fmt.Errorf("not ready")
	ErrAlreadyAddedTask = fmt.Errorf("task is already added")
)

Functions

func NewSharedPtrSafe added in v1.0.21

func NewSharedPtrSafe(ctx context.Context, new func() (Ptr, error), l logrus.FieldLogger) *sharedPtr

func Retry

func Retry(ctx context.Context, logger logrus.FieldLogger, maxWait time.Duration, failAfter time.Duration, f func() error) (err error)

Retry executes a f until no error is returned or failAfter is reached. A failAfter Timeout of zero means no timeout. maxWait max interval of two f

func WithFuncNewer

func WithFuncNewer(new func() (interface{}, error),
	ready func(x interface{}) error,
	close func(x interface{})) func() (Ptr, error)

Types

type Command

type Command struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(cmd *exec.Cmd) *Command

func (*Command) AppendPostHandles added in v1.0.19

func (r *Command) AppendPostHandles(h ...func(err error) error)

func (*Command) AppendPreHandles added in v1.0.19

func (r *Command) AppendPreHandles(h ...func() error)

func (*Command) Close

func (r *Command) Close()

func (*Command) Ready

func (r *Command) Ready() error

func (*Command) Run

func (r *Command) Run() error

func (*Command) Value

func (r *Command) Value() interface{}

type CommandSharedPtr

type CommandSharedPtr struct {
	*SharedPtr
	// contains filtered or unexported fields
}

func NewCommandSharedPtr

func NewCommandSharedPtr(ctx context.Context, cmd func() *exec.Cmd,
	preHandles []func() error, postHandles []func(err error) error,
	l logrus.FieldLogger) *CommandSharedPtr

func (*CommandSharedPtr) AppendPostHandles added in v1.0.19

func (g *CommandSharedPtr) AppendPostHandles(h ...func() error)

func (*CommandSharedPtr) AppendPreHandles added in v1.0.19

func (g *CommandSharedPtr) AppendPreHandles(h ...func() error)

func (*CommandSharedPtr) Get

func (g *CommandSharedPtr) Get() (*Command, error)

func (*CommandSharedPtr) GetUntilReady

func (g *CommandSharedPtr) GetUntilReady() (*Command, error)

func (*CommandSharedPtr) GetWithRetry

func (g *CommandSharedPtr) GetWithRetry() (*Command, error)

func (*CommandSharedPtr) Run

func (g *CommandSharedPtr) Run() error

type Event

type Event int
const (
	EventNew     Event = iota // new and start
	EventClose                // close
	EventExpired              // restart
)

func (Event) MarshalJSON added in v1.0.21

func (r Event) MarshalJSON() ([]byte, error)

MarshalJSON is generated so Event satisfies json.Marshaler.

func (Event) String

func (i Event) String() string

func (*Event) UnmarshalJSON added in v1.0.21

func (r *Event) UnmarshalJSON(data []byte) error

UnmarshalJSON is generated so Event satisfies json.Unmarshaler.

type Ptr

type Ptr interface {
	Value() interface{} //actual instance
	Ready() error
	Close()
}

func BackgroundPtr

func BackgroundPtr() Ptr

BackgroundPtr returns a non-nil, empty Ptr.

func TODOPtr

func TODOPtr() Ptr

TODO returns a non-nil, empty Ptr. Code should use context.TODO when it's unclear which Ptr to use or it is not yet available .

func WithFunc

func WithFunc(x interface{}, ready func(x interface{}) error,
	close func(x interface{})) (Ptr, error)

type SharedPtr

type SharedPtr struct {
	WatchStopped atomic_.Bool
	// contains filtered or unexported fields
}

func BackgroundSharedPtr

func BackgroundSharedPtr(ctx context.Context, l logrus.FieldLogger) *SharedPtr

BackgroundPtr returns a non-nil, empty Ptr.

func NewSharedPtr

func NewSharedPtr(ctx context.Context, new func() (Ptr, error), l logrus.FieldLogger) *SharedPtr

func NewSharedPtrFunc

func NewSharedPtrFunc(ctx context.Context,
	new func() (interface{}, error),
	ready func(x interface{}) error,
	close func(x interface{}), l logrus.FieldLogger) *SharedPtr

func TODOSharedPtr

func TODOSharedPtr(ctx context.Context, l logrus.FieldLogger) *SharedPtr

TODO returns a non-nil, empty Ptr. Code should use context.TODO when it's unclear which Ptr to use or it is not yet available .

func (*SharedPtr) AddTask

func (g *SharedPtr) AddTask(task *Task) error

func (*SharedPtr) AddTaskFunc

func (g *SharedPtr) AddTaskFunc(taskType TaskType, handle func() error, descriptions ...string) error

func (*SharedPtr) AddTaskFuncAsConstruct

func (g *SharedPtr) AddTaskFuncAsConstruct(handle func() error, descriptions ...string) error

func (*SharedPtr) AddTaskFuncAsConstructRepeat

func (g *SharedPtr) AddTaskFuncAsConstructRepeat(handle func() error, descriptions ...string) error

func (*SharedPtr) AddTaskFuncAsRepeat added in v1.0.1

func (g *SharedPtr) AddTaskFuncAsRepeat(handle func() error, descriptions ...string) error

func (*SharedPtr) AddTaskFuncAsRetry added in v1.0.1

func (g *SharedPtr) AddTaskFuncAsRetry(handle func() error, descriptions ...string) error

func (*SharedPtr) Context

func (g *SharedPtr) Context() context.Context

The returned context is always non-nil; it defaults to the background context.

func (*SharedPtr) Get

func (g *SharedPtr) Get() Ptr

std::shared_ptr.get()

func (*SharedPtr) GetLogger added in v1.0.21

func (g *SharedPtr) GetLogger() logrus.FieldLogger

func (*SharedPtr) GetTaskById added in v1.0.21

func (g *SharedPtr) GetTaskById(id string) (task *Task, has bool)

func (*SharedPtr) GetTasks added in v1.0.21

func (g *SharedPtr) GetTasks() map[string]*Task

func (*SharedPtr) GetUntilReady

func (g *SharedPtr) GetUntilReady() (Ptr, error)

std::shared_ptr.get() until ptr is ready & std::shared_ptr.make_unique() if necessary

func (*SharedPtr) GetWithRetry

func (g *SharedPtr) GetWithRetry() (Ptr, error)

std::shared_ptr.get() & std::shared_ptr.make_unique() if necessary

func (*SharedPtr) InShutdown

func (g *SharedPtr) InShutdown() bool

func (*SharedPtr) Ready

func (g *SharedPtr) Ready() error

func (*SharedPtr) Release

func (g *SharedPtr) Release() Ptr

std::shared_ptr.release()

func (*SharedPtr) RemoveAllTask

func (g *SharedPtr) RemoveAllTask()

func (*SharedPtr) RemoveTaskById

func (g *SharedPtr) RemoveTaskById(id string)

func (*SharedPtr) Reset

func (g *SharedPtr) Reset()

std::shared_ptr.reset()

func (*SharedPtr) TaskIds

func (g *SharedPtr) TaskIds() []string

func (*SharedPtr) Watch

func (g *SharedPtr) Watch() chan<- Event

type Task

type Task struct {
	Type        TaskType
	State       TaskState
	Description string // for debug
	Handle      func() error

	RepeatDuration time.Duration
	RetryDuration  time.Duration

	Ctx      context.Context
	CancelFn context.CancelFunc
	// contains filtered or unexported fields
}

func (*Task) Clone

func (t *Task) Clone() *Task

func (*Task) Context

func (t *Task) Context() context.Context

The returned context is always non-nil; it defaults to the background context.

func (*Task) ID

func (t *Task) ID() string

func (*Task) String

func (t *Task) String() string

type TaskState

type TaskState int
const (
	TaskStateNew               TaskState = iota // Task state for a task which has not yet started.
	TaskStateRunning                            // Task state for a running task. A task in the running state is executing in the Go routine but it may be waiting for other resources from the operating system such as processor.
	TaskStateDoneErrorHappened                  // Task state for a terminated state. The task has completed execution with some errors happened
	TaskStateDoneNormally                       // Task state for a terminated state. The task has completed execution normally
	TaskStateDormancy                           // Task state for a terminated state. The task has completed execution normally and will be started if New's called
	TaskStateDeath                              // Task state for a terminated state. The task has completed execution normally and will never be started again
	TaskStateButt
)

func (TaskState) MarshalJSON added in v1.0.21

func (r TaskState) MarshalJSON() ([]byte, error)

MarshalJSON is generated so TaskState satisfies json.Marshaler.

func (TaskState) String

func (i TaskState) String() string

func (*TaskState) UnmarshalJSON added in v1.0.21

func (r *TaskState) UnmarshalJSON(data []byte) error

UnmarshalJSON is generated so TaskState satisfies json.Unmarshaler.

type TaskType

type TaskType struct {
	Drop      bool // task will be dropped
	Retry     bool // task will be retried if error happens
	Construct bool // task will be called after New
	Repeat    bool // Task will be executed again and again
}

func (TaskType) String

func (t TaskType) String() string

Jump to

Keyboard shortcuts

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