-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
36 lines (29 loc) · 833 Bytes
/
debug.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
package jdh
import (
"fmt"
"sync/atomic"
"time"
"runtime"
)
// prints message every second until returned thunk is executed.
// useful for determining what function calls are hanging.
func CheckHang(message string, args ...interface{}) func() {
done := int32(0)
go func() {
// do nothing if done is called quickly; reduces printed messages
time.Sleep(1000 * time.Millisecond)
if atomic.LoadInt32(&done) != 0 {
return
}
// loop until done, printing message at each interval
for atomic.LoadInt32(&done) == 0 {
fmt.Printf(message, args...)
fmt.Printf("\t There are %v live goroutines.\n", runtime.NumGoroutine())
time.Sleep(1000 * time.Millisecond)
}
}()
// thunk to be called by client when done
return func() {
atomic.StoreInt32(&done, 1)
}
}