Adding upstream version 0.28.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
88f1d47ab6
commit
e28c88ef14
933 changed files with 194711 additions and 0 deletions
32
tools/routine/routine.go
Normal file
32
tools/routine/routine.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package routine
|
||||
|
||||
import (
|
||||
"log"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// FireAndForget executes f() in a new go routine and auto recovers if panic.
|
||||
//
|
||||
// **Note:** Use this only if you are not interested in the result of f()
|
||||
// and don't want to block the parent go routine.
|
||||
func FireAndForget(f func(), wg ...*sync.WaitGroup) {
|
||||
if len(wg) > 0 && wg[0] != nil {
|
||||
wg[0].Add(1)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if len(wg) > 0 && wg[0] != nil {
|
||||
defer wg[0].Done()
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("RECOVERED FROM PANIC (safe to ignore): %v", err)
|
||||
log.Println(string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
|
||||
f()
|
||||
}()
|
||||
}
|
27
tools/routine/routine_test.go
Normal file
27
tools/routine/routine_test.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package routine_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/routine"
|
||||
)
|
||||
|
||||
func TestFireAndForget(t *testing.T) {
|
||||
called := false
|
||||
|
||||
fn := func() {
|
||||
called = true
|
||||
panic("test")
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
routine.FireAndForget(fn, wg)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if !called {
|
||||
t.Error("Expected fn to be called.")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue