-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
75 lines (61 loc) · 1.65 KB
/
options.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
package executors
import (
"log/slog"
"time"
)
type RejectionHandler interface {
RejectExecution(runnable Runnable, e Executor) error
}
type ErrorHandler interface {
CatchError(runnable Runnable, e error)
}
type ErrorHandlerFunc func(runnable Runnable, e error)
func (f ErrorHandlerFunc) CatchError(runnable Runnable, e error) {
f(runnable, e)
}
type _PoolExecutorOption func(opts *poolExecutorOptions)
type poolExecutorOptions struct {
MaxConcurrent int
MaxBlockingTasks int
ExecuteTimeout time.Duration
ErrorHandler ErrorHandler
RejectionHandler RejectionHandler
Logger *slog.Logger
}
var _DefaultPoolExecutorOptions = poolExecutorOptions{
MaxConcurrent: 10,
ExecuteTimeout: 0,
ErrorHandler: LogErrorHandler{},
RejectionHandler: NoopRejectionPolicy{},
Logger: slog.Default(),
}
func WithMaxConcurrent(concurrent int) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.MaxConcurrent = concurrent
}
}
func WithMaxBlockingTasks(max int) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.MaxBlockingTasks = max
}
}
func WithExecuteTimeout(ts time.Duration) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.ExecuteTimeout = ts
}
}
func WithRejectionHandler(handler RejectionHandler) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.RejectionHandler = handler
}
}
func WithErrorHandler(handler ErrorHandler) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.ErrorHandler = handler
}
}
func WithLogger(logger *slog.Logger) _PoolExecutorOption {
return func(opts *poolExecutorOptions) {
opts.Logger = logger
}
}