/
context.go
122 lines (100 loc) · 3.01 KB
/
context.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
package service
import (
"context"
"fmt"
"git.golaxy.org/core/internal/concurrent"
"git.golaxy.org/core/plugin"
"git.golaxy.org/core/pt"
"git.golaxy.org/core/util/iface"
"git.golaxy.org/core/util/option"
"git.golaxy.org/core/util/reinterpret"
"git.golaxy.org/core/util/uid"
"reflect"
)
// NewContext 创建服务上下文
func NewContext(settings ...option.Setting[ContextOptions]) Context {
return UnsafeNewContext(option.Make(With.Default(), settings...))
}
// Deprecated: UnsafeNewContext 内部创建服务上下文
func UnsafeNewContext(options ContextOptions) Context {
if !options.CompositeFace.IsNil() {
options.CompositeFace.Iface.init(options)
return options.CompositeFace.Iface
}
ctx := &ContextBehavior{}
ctx.init(options)
return ctx.opts.CompositeFace.Iface
}
// Context 服务上下文
type Context interface {
_Context
concurrent.Context
reinterpret.CompositeProvider
Caller
plugin.PluginProvider
pt.EntityPTProvider
fmt.Stringer
// GetName 获取名称
GetName() string
// GetId 获取服务Id
GetId() uid.Id
// GetEntityMgr 获取实体管理器
GetEntityMgr() EntityMgr
}
type _Context interface {
init(opts ContextOptions)
getOptions() *ContextOptions
changeRunningState(state RunningState)
getReflected() reflect.Value
}
// ContextBehavior 服务上下文行为,在需要扩展服务上下文能力时,匿名嵌入至服务上下文结构体中
type ContextBehavior struct {
concurrent.ContextBehavior
opts ContextOptions
reflected reflect.Value
entityMgr _EntityMgrBehavior
}
// GetName 获取名称
func (ctx *ContextBehavior) GetName() string {
return ctx.opts.Name
}
// GetId 获取服务Id
func (ctx *ContextBehavior) GetId() uid.Id {
return ctx.opts.PersistId
}
// GetEntityMgr 获取实体管理器
func (ctx *ContextBehavior) GetEntityMgr() EntityMgr {
return &ctx.entityMgr
}
// GetCompositeFaceCache 支持重新解释类型
func (ctx *ContextBehavior) GetCompositeFaceCache() iface.Cache {
return ctx.opts.CompositeFace.Cache
}
// String implements fmt.Stringer
func (ctx *ContextBehavior) String() string {
return fmt.Sprintf(`{"id":%q, "name":%q}`, ctx.GetId(), ctx.GetName())
}
func (ctx *ContextBehavior) init(opts ContextOptions) {
ctx.opts = opts
if ctx.opts.CompositeFace.IsNil() {
ctx.opts.CompositeFace = iface.MakeFace[Context](ctx)
}
if ctx.opts.Context == nil {
ctx.opts.Context = context.Background()
}
if ctx.opts.PersistId.IsNil() {
ctx.opts.PersistId = uid.New()
}
concurrent.UnsafeContext(&ctx.ContextBehavior).Init(ctx.opts.Context, ctx.opts.AutoRecover, ctx.opts.ReportError)
ctx.reflected = reflect.ValueOf(ctx.opts.CompositeFace.Iface)
ctx.entityMgr.init(ctx.opts.CompositeFace.Iface)
}
func (ctx *ContextBehavior) getOptions() *ContextOptions {
return &ctx.opts
}
func (ctx *ContextBehavior) changeRunningState(state RunningState) {
ctx.opts.RunningHandler.Call(ctx.GetAutoRecover(), ctx.GetReportError(), nil, ctx.opts.CompositeFace.Iface, state)
}
func (ctx *ContextBehavior) getReflected() reflect.Value {
return ctx.reflected
}