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.
(f func(), wg ...*sync.WaitGroup)
| 11 | // **Note:** Use this only if you are not interested in the result of f() |
| 12 | // and don't want to block the parent go routine. |
| 13 | func FireAndForget(f func(), wg ...*sync.WaitGroup) { |
| 14 | if len(wg) > 0 && wg[0] != nil { |
| 15 | wg[0].Add(1) |
| 16 | } |
| 17 | |
| 18 | go func() { |
| 19 | if len(wg) > 0 && wg[0] != nil { |
| 20 | defer wg[0].Done() |
| 21 | } |
| 22 | |
| 23 | defer func() { |
| 24 | if err := recover(); err != nil { |
| 25 | log.Println("[FireAndForget] RECOVERED FROM PANIC:", err) |
| 26 | |
| 27 | stack := make([]byte, 2<<10) // 2 KB |
| 28 | length := runtime.Stack(stack, false) |
| 29 | log.Println(string(stack[:length])) |
| 30 | } |
| 31 | }() |
| 32 | |
| 33 | f() |
| 34 | }() |
| 35 | } |
searching dependent graphs…