Skip to content

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

License

Notifications You must be signed in to change notification settings

AlexanderChen1989/ha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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))
}

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages