-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
152 lines (122 loc) · 2.67 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gouse
import (
"reflect"
"sync"
"time"
)
/* Lock */
func lockHandler(callback interface{}, lock, unlock func()) interface{} {
callbackType := reflect.TypeOf(callback)
if callbackType.Kind() != reflect.Func {
panic("callback must be a function")
}
return reflect.MakeFunc(callbackType, func(params []reflect.Value) []reflect.Value {
lock()
defer unlock()
return reflect.ValueOf(callback).Call(params)
}).Interface()
}
func LockFunc(callback interface{}) interface{} {
var mutex sync.Mutex
return lockHandler(callback, mutex.Lock, mutex.Unlock)
}
func RWLockFunc(callback interface{}) interface{} {
var rwMutex sync.RWMutex
return lockHandler(callback, rwMutex.RLock, rwMutex.RUnlock)
}
/* Defer wrapper function */
func DeferWrapper(mainFunc func() error, cleanupFunc func()) error {
defer func() {
if cleanupFunc != nil {
cleanupFunc()
}
}()
if err := mainFunc(); err != nil {
return err
}
return nil
}
/* Delay */
type DelayedResult[T any] struct {
Value T
HasReturn bool
}
func delay[T any](f func() T, timeout int, hasReturn bool) DelayedResult[T] {
resultChan := make(chan T, 1)
go func() {
time.Sleep(time.Duration(timeout) * time.Second)
result := f()
if hasReturn {
resultChan <- result
}
}()
if hasReturn {
return DelayedResult[T]{Value: <-resultChan, HasReturn: true}
}
return DelayedResult[T]{HasReturn: false}
}
func DelayF[T any](f func() T, timeout int) DelayedResult[T] {
return delay(f, timeout, true)
}
func DelayFunc(f func(), timeout int) DelayedResult[struct{}] {
return delay(func() struct{} {
f()
return struct{}{}
}, timeout, false)
}
/* Utilities */
func ParallelizeFunc(functions ...func()) {
var waitGroup sync.WaitGroup
waitGroup.Add(len(functions))
ch := make(chan struct{}, len(functions))
done := make(chan struct{}, len(functions))
for _, f := range functions {
ch <- struct{}{}
go func(copyFunc func()) {
defer func() {
<-ch
waitGroup.Done()
}()
copyFunc()
}(f)
}
go func() {
waitGroup.Wait()
close(done)
}()
<-done
}
func RetryFunc(fn func() error, attempts int, sleep int) (err error) {
retry := func() {
if err = fn(); err != nil {
return
}
}
for i := attempts; i > 0; i-- {
if i > 1 {
retry()
SleepSecond(sleep)
} else if i == 1 {
retry()
} else {
return nil
}
}
return
}
func RemainFunc(fn func(), attempts int) {
for i := 0; i < attempts; i++ {
fn()
}
}
func IntervalFunc(fn func(), timeout int) {
for {
fn()
SleepSecond(timeout)
}
}
func RunTimeFunc(startTime time.Time, task func()) time.Duration {
task()
elapsedTime := float64(time.Since(startTime).Seconds() * 1000)
return time.Duration(elapsedTime)
}