-
Notifications
You must be signed in to change notification settings - Fork 1
/
async.go
91 lines (77 loc) · 2.12 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
package asyncp
import (
"context"
"log"
"github.com/demdxx/rpool/v2"
)
// AsyncOption type options tune
type AsyncOption func(opt *AsyncOptions)
// AsyncOptions contains concurrent execution pool
type AsyncOptions struct {
// pool options
poolOptions []rpool.Option
}
// Pool of execution
func (opt *AsyncOptions) Pool(fnk func(any)) *rpool.PoolFunc[any] {
return rpool.NewPoolFunc(fnk, opt.poolOptions...)
}
// WithWorkerCount change count of workers
func WithWorkerCount(count int) AsyncOption {
return func(opt *AsyncOptions) {
opt.poolOptions = append(opt.poolOptions, rpool.WithWorkerCount(count))
}
}
// WithWorkerPoolSize setup maximal size of worker pool
func WithWorkerPoolSize(size int) AsyncOption {
return func(opt *AsyncOptions) {
opt.poolOptions = append(opt.poolOptions, rpool.WithWorkerPoolSize(size))
}
}
// WithRecoverHandler defined error handler
func WithRecoverHandler(f func(any)) AsyncOption {
return func(opt *AsyncOptions) {
opt.poolOptions = append(opt.poolOptions, rpool.WithRecoverHandler(f))
}
}
type asyncTaskParams struct {
ctx context.Context
event Event
rw ResponseWriter
}
// AsyncTask processor
type AsyncTask struct {
execPool *rpool.PoolFunc[any]
task Task
}
// WrapAsyncTask as async executor
func WrapAsyncTask(task Task, options ...AsyncOption) *AsyncTask {
var opts AsyncOptions
for _, opt := range options {
opt(&opts)
}
asyncTask := &AsyncTask{task: task}
asyncTask.execPool = opts.Pool(asyncTask.handler)
return asyncTask
}
// Execute the list of subtasks with input data collection.
func (t *AsyncTask) Execute(ctx context.Context, event Event, responseWriter ResponseWriter) error {
t.execPool.Call(&asyncTaskParams{ctx: ctx, event: event, rw: responseWriter})
return nil
}
func (t *AsyncTask) handler(ctx any) {
p := ctx.(*asyncTaskParams)
defer func() {
err := p.rw.Release()
if err != nil {
log.Printf("release response writer: %s", err.Error())
}
}()
err := t.task.Execute(p.ctx, p.event, p.rw)
if err != nil {
panic(err)
}
}
// Close execution pool and finish handler processing
func (t *AsyncTask) Close() error {
return t.execPool.Close()
}