-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_call.go
95 lines (81 loc) · 1.62 KB
/
async_call.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
package hub
import (
"errors"
"runtime"
"github.com/rs/zerolog/log"
)
type asyncReturn Return
// 异步返回值
type Return struct {
Value interface{}
Error error
callback func(Return)
out chan interface{}
}
type asyncCall struct {
out chan interface{} // 接收返回值
exec func() // 执行异步方法
}
// 构建异步调用
func newAsyncCall(
fn func(arg interface{}) Return,
arg interface{},
callback func(arg Return),
) asyncCall {
var out chan interface{}
if callback == nil {
out = nil
} else {
out = make(chan interface{}, 1)
}
ac := asyncCall{
out: out,
exec: func() {
go asyncExec(out, fn, arg, callback)
},
}
return ac
}
// 包裹回调,接收放回值,并释放out通道
func recvAsyncReturn(
out chan interface{},
callback func(arg Return),
) func() {
return func() {
v, ok := <-out
if !ok {
if callback != nil {
callback(Return{Error: errors.New("return chan is closed before")})
}
return
}
if callback != nil {
callback(Return(v.(asyncReturn)))
}
close(out)
}
}
// 异步执行,panic保护释放掉out
func asyncExec(
out chan interface{},
fn func(arg interface{}) Return,
arg interface{},
recv func(Return),
) {
if r := recover(); r != nil {
if stackBufferSize > 0 {
buf := make([]byte, stackBufferSize)
l := runtime.Stack(buf, false)
log.Printf("%v: %s", r, buf[:l])
} else {
log.Printf("%v", r)
}
close(out)
}
ar := fn(arg)
ar.out = out
ar.callback = recv
if out != nil {
out <- asyncReturn(ar)
}
}