-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.go
194 lines (158 loc) · 4.55 KB
/
async.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package core
import (
"context"
"git.golaxy.org/core/ec/ectx"
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/utils/async"
"git.golaxy.org/core/utils/exception"
"git.golaxy.org/core/utils/generic"
"time"
)
// CallAsync 异步执行代码,有返回值
func CallAsync(provider ectx.ConcurrentContextProvider, fun generic.FuncVar1[runtime.Context, any, async.Ret], args ...any) async.AsyncRet {
ctx := runtime.UnsafeConcurrentContext(runtime.Concurrent(provider)).GetContext()
return ctx.CallAsync(func(...any) async.Ret { return fun.UnsafeCall(ctx, args...) })
}
// CallVoidAsync 异步执行代码,无返回值
func CallVoidAsync(provider ectx.ConcurrentContextProvider, fun generic.ActionVar1[runtime.Context, any], args ...any) async.AsyncRet {
ctx := runtime.UnsafeConcurrentContext(runtime.Concurrent(provider)).GetContext()
return ctx.CallVoidAsync(func(...any) { fun.UnsafeCall(ctx, args...) })
}
// GoAsync 使用新线程执行代码,有返回值
func GoAsync(ctx context.Context, fun generic.FuncVar1[context.Context, any, async.Ret], args ...any) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
asyncRet := async.MakeAsyncRet()
go func() {
ret, panicErr := fun.SafeCall(ctx, args...)
if panicErr != nil {
ret.Error = panicErr
}
async.Return(asyncRet, ret)
}()
return asyncRet
}
// GoVoidAsync 使用新线程执行代码,无返回值
func GoVoidAsync(ctx context.Context, fun generic.ActionVar1[context.Context, any], args ...any) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
asyncRet := async.MakeAsyncRet()
go func() {
async.Return(asyncRet, async.MakeRet(nil, fun.SafeCall(ctx, args...)))
}()
return asyncRet
}
// TimeAfterAsync 定时器,指定时长
func TimeAfterAsync(ctx context.Context, dur time.Duration) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
asyncRet := async.MakeAsyncRet()
go func() {
timer := time.NewTimer(dur)
defer timer.Stop()
select {
case <-timer.C:
async.Yield(ctx, asyncRet, async.VoidRet)
case <-ctx.Done():
break
}
async.End(asyncRet)
}()
return asyncRet
}
// TimeAtAsync 定时器,指定时间点
func TimeAtAsync(ctx context.Context, at time.Time) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
asyncRet := async.MakeAsyncRet()
go func() {
timer := time.NewTimer(time.Until(at))
defer timer.Stop()
select {
case <-timer.C:
async.Yield(ctx, asyncRet, async.VoidRet)
case <-ctx.Done():
break
}
async.End(asyncRet)
}()
return asyncRet
}
// TimeTickAsync 心跳器
func TimeTickAsync(ctx context.Context, dur time.Duration) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
asyncRet := async.MakeAsyncRet()
go func() {
tick := time.NewTicker(dur)
defer tick.Stop()
loop:
for {
select {
case <-tick.C:
if !async.Yield(ctx, asyncRet, async.VoidRet) {
break loop
}
case <-ctx.Done():
break loop
}
}
async.End(asyncRet)
}()
return asyncRet
}
// ReadChanAsync 读取channel转换为AsyncRet
func ReadChanAsync(ctx context.Context, ch <-chan any) async.AsyncRet {
return ReadChanAsyncT[any](ctx, ch)
}
// ReadChanAsyncT 读取channel转换为AsyncRet
func ReadChanAsyncT[T any](ctx context.Context, ch <-chan T) async.AsyncRet {
if ctx == nil {
ctx = context.Background()
}
if ch == nil {
exception.Panicf("%w: %w: ch is nil", ErrCore, ErrArgs)
}
asyncRet := async.MakeAsyncRet()
go func() {
loop:
for {
select {
case v, ok := <-ch:
if !ok {
break loop
}
if !async.Yield(ctx, asyncRet, async.MakeRet(v, nil)) {
break loop
}
case <-ctx.Done():
break loop
}
}
async.End(asyncRet)
}()
return asyncRet
}