ha

package module
v0.0.0-...-155ba22 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2016 License: MIT Imports: 3 Imported by: 0

README

HA For Go

Add a little high availability to your Go functions or methods.

Simple Usage

ha.Watch(func() {...})
ha.Watch(
	func() {...},
	ha.RestartDelay(time.Second),
	// ha.MaxRestart(5),
	// ha.CancelCtx(ctx),
 )

Caution!

Panic in goroutine may panic runtime! Watch is only watch current goroutine.

	Watch(func() {
		go func() {
			panic("No!!") // Will panic runtime
		}()
	})

	// you can do this
	// func will be restart when panic happened
	go Watch(func() {
		panic("No!!")
	})

Example

package main

import (
	"fmt"
	"time"

	"golang.org/x/net/context"

	"github.com/AlexanderChen1989/ha"
)

func main() {
	// a function that shoud be watched
	fn := func() {
		var i *int
		*i = 100
	}

	// when fn stop
	onStop := ha.OnStop(func(err error) {
		fmt.Println(err)
	})

	// try to restart fn at most 5 times
	max := ha.MaxRestart(5)

	// wait sometime before restart fn
	delay := ha.RestartDelay(1 * time.Second)

	// context to stop restarting fn
	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		// sleep 2 seconds
		time.Sleep(2 * time.Second)
		cancel()
	}()

	ha.Watch(fn, onStop, delay, max, ha.CancelCtx(ctx))
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelCtx

func CancelCtx(ctx context.Context) func(conf *config)

CancelCtx when ctx cancled, fn will not restart again

func OnStop

func OnStop(onStop func(error)) func(conf *config)

OnStop run onStop when fn stopped

func RestartDelay

func RestartDelay(d time.Duration) func(conf *config)

RestartDelay wait for d time before restart fn

func RestartTimes

func RestartTimes(max uint) func(conf *config)

RestartTimes max restart times

func Watch

func Watch(fn func(), setups ...func(*config))

Watch run then watch fn, if fn returned, run it again

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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