public
Created

Very simple supervisor in Go

  • Download Gist
gosuper.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package gosuper
 
type addFunc struct {
f func()
id chan uint64
}
 
type Supervisor struct {
exit chan uint64
addRoutine chan addFunc
routines map[uint64]func()
incId uint64
}
 
func NewSupervisor() *Supervisor {
super := &Supervisor{
exit: make(chan uint64),
addRoutine: make(chan addFunc),
routines: make(map[uint64]func()),
incId: 0,
}
 
// Listen for goroutine exits and restart
go func() {
for ; ; {
id := <-super.exit
if routine := super.routines[id]; routine != nil {
go super.start(id, routine)
}
}
}()
 
// Add a routine for watching
go func() {
addFunction := <-super.addRoutine
id := super.incId
super.incId = super.incId + 1
super.routines[id] = addFunction.f
addFunction.id <- id
}()
 
return super
}
 
// Run function and inform supervisor incase of panic
func (super *Supervisor) start(id uint64, routine func()) {
defer func() {
if err := recover(); err != nil {
super.exit <- id
}
}()
routine()
}
 
// Start a go routine and restart it if it panics
func (super *Supervisor) GoWatch(routine func()) {
idChan := make(chan uint64)
super.addRoutine <- addFunc{routine, idChan}
id := <-idChan
go super.start(id, routine)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.