/
runtime_options.go
87 lines (76 loc) · 2.59 KB
/
runtime_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
76
77
78
79
80
81
82
83
84
85
86
87
package core
import (
"fmt"
"git.golaxy.org/core/internal/exception"
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/core/util/option"
"time"
)
type (
CustomGC = generic.DelegateAction1[Runtime] // 自定义GC函数
)
// RuntimeOptions 创建运行时的所有选项
type RuntimeOptions struct {
CompositeFace iface.Face[Runtime] // 扩展者,在扩展运行时自身能力时使用
AutoRun bool // 是否开启自动运行
ProcessQueueCapacity int // 任务处理流水线大小
Frame runtime.Frame // 帧,设置为nil表示不使用帧更新特性
GCInterval time.Duration // GC间隔时长
CustomGC CustomGC // 自定义GC
}
type _RuntimeOption struct{}
// Default 运行时的默认值
func (_RuntimeOption) Default() option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
With.Runtime.CompositeFace(iface.Face[Runtime]{})(o)
With.Runtime.AutoRun(false)(o)
With.Runtime.ProcessQueueCapacity(128)(o)
With.Runtime.Frame(nil)(o)
With.Runtime.GCInterval(10 * time.Second)(o)
With.Runtime.CustomGC(nil)(o)
}
}
// CompositeFace 扩展者,在扩展运行时自身能力时使用
func (_RuntimeOption) CompositeFace(face iface.Face[Runtime]) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
o.CompositeFace = face
}
}
// AutoRun 运行时是否开启自动运行
func (_RuntimeOption) AutoRun(b bool) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
o.AutoRun = b
}
}
// ProcessQueueCapacity 任务处理流水线大小
func (_RuntimeOption) ProcessQueueCapacity(cap int) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
if cap <= 0 {
panic(fmt.Errorf("%w: %w: ProcessQueueCapacity less equal 0 is invalid", ErrRuntime, exception.ErrArgs))
}
o.ProcessQueueCapacity = cap
}
}
// Frame 运行时的帧,设置为nil表示不使用帧更新特性
func (_RuntimeOption) Frame(frame runtime.Frame) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
o.Frame = frame
}
}
// GCInterval 运行时的GC间隔时长
func (_RuntimeOption) GCInterval(dur time.Duration) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
if dur <= 0 {
panic(fmt.Errorf("%w: %w: GCInterval less equal 0 is invalid", ErrRuntime, exception.ErrArgs))
}
o.GCInterval = dur
}
}
// CustomGC 运行时的自定义GC
func (_RuntimeOption) CustomGC(fn CustomGC) option.Setting[RuntimeOptions] {
return func(o *RuntimeOptions) {
o.CustomGC = fn
}
}