-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
109 lines (89 loc) · 2.96 KB
/
service.go
File metadata and controls
109 lines (89 loc) · 2.96 KB
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
/*
* 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 (
"sync/atomic"
"git.golaxy.org/core/service"
"git.golaxy.org/core/utils/exception"
"git.golaxy.org/core/utils/iface"
"git.golaxy.org/core/utils/option"
"git.golaxy.org/core/utils/reinterpret"
)
// NewService 创建服务
func NewService(svcCtx service.Context, settings ...option.Setting[ServiceOptions]) Service {
return UnsafeNewService(svcCtx, option.New(With.Service.Default(), settings...))
}
// Deprecated: UnsafeNewService 内部创建服务
func UnsafeNewService(svcCtx service.Context, options ServiceOptions) Service {
var svc Service
if !options.InstanceFace.IsNil() {
svc = options.InstanceFace.Iface
} else {
svc = &ServiceBehavior{}
}
svc.init(svcCtx, options)
return svc
}
// Service 服务
type Service interface {
iService
iWorker
iServiceStats
reinterpret.InstanceProvider
// Context 获取服务上下文
Context() service.Context
}
type iService interface {
init(svcCtx service.Context, options ServiceOptions)
getOptions() *ServiceOptions
getInstance() Service
}
type ServiceBehavior struct {
ctx service.Context
options ServiceOptions
isRunning atomic.Bool
}
// Context 获取服务上下文
func (svc *ServiceBehavior) Context() service.Context {
return svc.ctx
}
// InstanceFaceCache 支持重新解释类型
func (svc *ServiceBehavior) InstanceFaceCache() iface.Cache {
return svc.options.InstanceFace.Cache
}
func (svc *ServiceBehavior) init(svcCtx service.Context, options ServiceOptions) {
if svcCtx == nil {
exception.Panicf("%w: %w: svcCtx is nil", ErrService, ErrArgs)
}
if !service.UnsafeContext(svcCtx).Scoped().CompareAndSwap(false, true) {
exception.Panicf("%w: %w: svcCtx is already bound to another service scope", ErrService, ErrArgs)
}
svc.ctx = svcCtx
svc.options = options
if svc.options.InstanceFace.IsNil() {
svc.options.InstanceFace = iface.NewFaceT[Service](svc)
}
svc.emitEventRunningEvent(service.RunningEvent_Birth)
}
func (svc *ServiceBehavior) getOptions() *ServiceOptions {
return &svc.options
}
func (svc *ServiceBehavior) getInstance() Service {
return svc.options.InstanceFace.Iface
}