-
Notifications
You must be signed in to change notification settings - Fork 0
/
hookfn.go
92 lines (81 loc) · 2.06 KB
/
hookfn.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
package oo
//简单的hook链,用于非核心流程
//Add和Sort都是非安全、且只调用一次的,Run可以安全调用多次
import (
"reflect"
"sort"
"sync"
)
const HOOK_PRIO_FIRST = 0
const HOOK_PRIO_NORMAL = 10
const HOOK_PRIO_LAST = 99
type hook struct {
Prio int
Fn interface{} //钩子函数类型 func (...) (,,error)
}
var gHookMap *sync.Map = new(sync.Map)
func AddHook(hookname string, prio int, Fn interface{}) {
hltmp, _ := gHookMap.LoadOrStore(hookname, []hook{})
hl, _ := hltmp.([]hook)
hl = append(hl, hook{
Prio: prio,
Fn: Fn,
})
sort.Slice(hl, func(i, j int) bool {
return hl[i].Prio < hl[j].Prio
})
gHookMap.Store(hookname, hl)
}
//按优先级执行
func RunHookAll(hookname string, args ...interface{}) {
if hltmp, ok := gHookMap.Load(hookname); ok {
hl, _ := hltmp.([]hook)
in := make([]reflect.Value, len(args))
for k, v := range args {
in[k] = reflect.ValueOf(v)
}
for _, post := range hl {
f := reflect.ValueOf(post.Fn)
f.Call(in)
}
}
}
//按优先级执行,发现钩子返回error则停下
func RunHookToError(hookname string, args ...interface{}) error {
if hltmp, ok := gHookMap.Load(hookname); ok {
hl, _ := hltmp.([]hook)
in := make([]reflect.Value, len(args))
for k, v := range args {
in[k] = reflect.ValueOf(v)
}
for _, post := range hl {
f := reflect.ValueOf(post.Fn)
retarr := f.Call(in)
iret := len(retarr)
if iret > 0 && !retarr[iret-1].IsNil() {
err, _ := retarr[iret-1].Interface().(error)
return err
}
}
}
return nil
}
//按优先级执行,发现钩子无返回或返回nil则停下
func RunHookToSuccess(hookname string, args ...interface{}) bool {
if hltmp, ok := gHookMap.Load(hookname); ok {
hl, _ := hltmp.([]hook)
in := make([]reflect.Value, len(args))
for k, v := range args {
in[k] = reflect.ValueOf(v)
}
for _, post := range hl {
f := reflect.ValueOf(post.Fn)
retarr := f.Call(in) //不返回/返回error为nil都继续
iret := len(retarr)
if iret == 0 || retarr[iret-1].IsNil() {
return true
}
}
}
return false
}