Compare commits
2 Commits
461d0b77e9
...
735d314a19
Author | SHA1 | Date | |
---|---|---|---|
735d314a19 | |||
4f4c7a0627 |
35
examples/panic/main.go
Normal file
35
examples/panic/main.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Example panic demonstrates how the environment can restart actors if they
|
||||||
|
// fail.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
import "time"
|
||||||
|
import "errors"
|
||||||
|
import "context"
|
||||||
|
import "math/rand"
|
||||||
|
import "git.tebibyte.media/sashakoshka/camfish"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
camfish.Run("panic",
|
||||||
|
"Example panic demonstrates how the environment can restart " +
|
||||||
|
"actors if they fail",
|
||||||
|
new(actor))
|
||||||
|
}
|
||||||
|
|
||||||
|
// actor is an incorrectly implemented actor that panics and errs randomly.
|
||||||
|
type actor struct { }
|
||||||
|
var _ camfish.Runnable = new(actor)
|
||||||
|
func (this *actor) Type() string { return "panic" }
|
||||||
|
|
||||||
|
func (this *actor) Run(ctx context.Context) error {
|
||||||
|
log.Println("(i) [panic] panicking in 10 seconds")
|
||||||
|
select {
|
||||||
|
case <- ctx.Done(): return ctx.Err()
|
||||||
|
case <- time.After(time.Second * 10):
|
||||||
|
if rand.Int() % 2 == 0 {
|
||||||
|
panic("this is a panic")
|
||||||
|
} else {
|
||||||
|
return errors.New("this is an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
util.go
25
util.go
@ -12,6 +12,19 @@ import "strings"
|
|||||||
import "context"
|
import "context"
|
||||||
import "sync/atomic"
|
import "sync/atomic"
|
||||||
import "unicode/utf8"
|
import "unicode/utf8"
|
||||||
|
import "runtime/debug"
|
||||||
|
|
||||||
|
func panicErr(message any, stack []byte) (err error) {
|
||||||
|
if panErr, ok := message.(error); ok {
|
||||||
|
err = panErr
|
||||||
|
} else {
|
||||||
|
err = errors.New(fmt.Sprint(message))
|
||||||
|
}
|
||||||
|
if stack != nil {
|
||||||
|
err = fmt.Errorf("%w: %s", err, stack)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func defaul[T comparable](value, def T) T {
|
func defaul[T comparable](value, def T) T {
|
||||||
var zero T
|
var zero T
|
||||||
@ -22,11 +35,7 @@ func defaul[T comparable](value, def T) T {
|
|||||||
func panicWrap(f func() error) (err error) {
|
func panicWrap(f func() error) (err error) {
|
||||||
defer func () {
|
defer func () {
|
||||||
if pan := recover(); pan != nil {
|
if pan := recover(); pan != nil {
|
||||||
if panErr, ok := pan.(error); ok {
|
err = panicErr(pan, debug.Stack())
|
||||||
err = panErr
|
|
||||||
} else {
|
|
||||||
err = errors.New(fmt.Sprint(pan))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} ()
|
} ()
|
||||||
|
|
||||||
@ -37,11 +46,7 @@ func panicWrap(f func() error) (err error) {
|
|||||||
func panicWrapCtx(ctx context.Context, f func(context.Context) error) (err error) {
|
func panicWrapCtx(ctx context.Context, f func(context.Context) error) (err error) {
|
||||||
defer func () {
|
defer func () {
|
||||||
if pan := recover(); pan != nil {
|
if pan := recover(); pan != nil {
|
||||||
if panErr, ok := pan.(error); ok {
|
err = panicErr(pan, debug.Stack())
|
||||||
err = panErr
|
|
||||||
} else {
|
|
||||||
err = errors.New(fmt.Sprint(pan))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} ()
|
} ()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user