-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunction.go
46 lines (40 loc) · 967 Bytes
/
function.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
package utils
import (
"runtime"
"time"
)
// RetryStopper :nodoc:
type RetryStopper struct {
error
}
// Retry :nodoc:
func Retry(attempts int, sleep time.Duration, fn func() error) error {
if err := fn(); err != nil {
if s, ok := err.(RetryStopper); ok {
// Return the original error for later checking
return s.error
}
if attempts--; attempts > 0 {
time.Sleep(sleep)
return Retry(attempts, 2*sleep, fn)
}
return err
}
return nil
}
// NewRetryStopper :nodoc:
func NewRetryStopper(err error) RetryStopper {
return RetryStopper{err}
}
// MyCaller will return the method caller. skip value defines how many steps to be skipped.
// skip=0 will always return the MyCaller
// skip=1 returns the caller of the MyCaller
// and so on...
func MyCaller(skip int) string {
pc, _, _, ok := runtime.Caller(skip)
details := runtime.FuncForPC(pc)
if ok && details != nil {
return details.Name()
}
return "failed to identify method caller"
}