-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
246 lines (204 loc) · 6.86 KB
/
manager.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2020 The ZKits Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package environment
import (
"errors"
"sync"
"sync/atomic"
)
var (
// ErrInvalidEnv represents that the given runtime environment is not
// registered or supported.
ErrInvalidEnv = errors.New("environment: invalid env")
// ErrLocked indicates that the current runtime environment is locked
// and cannot be changed.
ErrLocked = errors.New("environment: locked")
)
// Manager interface type defines a runtime environment manager.
// An independent system should share an independent manager instance.
type Manager interface {
// Get method returns the current runtime environment.
Get() Env
// Is returns whether the given runtime environment is equal to the
// current runtime environment.
Is(Env) bool
// In returns whether the current runtime environment is in the given
// runtime environment list.
In(envs []Env) bool
// Register method registers a custom runtime environment.
// If you want to add a custom environment, this method must be called
// before the Manager.Set() method.
Register(Env)
// Registered determines whether the given runtime environment is already registered.
Registered(Env) bool
// Lock method locks the current runtime environment.
// After locking, the current runtime environment cannot be changed.
Lock()
// Locked method returns whether the current runtime environment is locked.
Locked() bool
// Set method sets the current runtime environment.
// If the given runtime environment is not supported, ErrInvalidEnv error is returned.
// If the current runtime environment is locked, ErrLocked error is returned.
Set(Env) error
// SetAndLock method sets and locks the current runtime environment.
// If the runtime environment settings fail, they are not locked.
SetAndLock(Env) error
// Listen method adds a given runtime environment listener.
// If the given listener is nil, ignore it.
Listen(Listener)
// UnListen removes and returns to the recently added listener.
// If there is no listener to be removed, nil is returned.
UnListen() Listener
// UnListenAll removes and returns all added listeners.
// If there is no listener to be removed, nil is returned.
UnListenAll() []Listener
}
// New creates and returns a new instance of the built-in runtime environment manager.
// The default runtime environment is Development, and all built-in runtime environments
// have been registered.
func New() Manager {
return &manager{
current: Development,
registered: []Env{Development, Testing, Prerelease, Production},
}
}
// NewEmpty creates and returns an empty instance of the runtime environment manager.
// The manager returned by this function does not register any runtime environment,
// and the current runtime environment is empty.
func NewEmpty() Manager {
return new(manager)
}
// Listener defines the runtime environment listener.
// Listeners are used to receive notifications when the runtime environment changes.
type Listener func(after, before Env)
// This is a built-in runtime environment manager.
type manager struct {
mutex sync.RWMutex
current Env
locked int32
registered []Env
listeners []Listener
}
// Get method returns the current runtime environment.
func (m *manager) Get() Env {
return m.current
}
// Is returns whether the given runtime environment is equal to the
// current runtime environment.
func (m *manager) Is(env Env) bool {
return m.current.Is(env)
}
// In returns whether the current runtime environment is in the given
// runtime environment list.
func (m *manager) In(envs []Env) bool {
return m.current.In(envs)
}
// Register method registers a custom runtime environment.
// If you want to add a custom environment, this method must be called
// before the Manager.Set() method.
func (m *manager) Register(env Env) {
m.mutex.Lock()
defer m.mutex.Unlock()
if !env.In(m.registered) {
m.registered = append(m.registered, env)
}
}
// Registered determines whether the given runtime environment is already registered.
func (m *manager) Registered(env Env) bool {
m.mutex.RLock()
defer m.mutex.RUnlock()
return env.In(m.registered)
}
// Lock method locks the current runtime environment.
// After locking, the current runtime environment cannot be changed.
func (m *manager) Lock() {
m.mutex.Lock()
defer m.mutex.Unlock()
atomic.StoreInt32(&m.locked, 1)
}
// Locked method returns whether the current runtime environment is locked.
func (m *manager) Locked() bool {
return atomic.LoadInt32(&m.locked) == 1
}
// Set method sets the current runtime environment.
// If the given runtime environment is not supported, ErrInvalidEnv error is returned.
// If the current runtime environment is locked, ErrLocked error is returned.
func (m *manager) Set(env Env) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.set(env)
}
// SetAndLock method sets and locks the current runtime environment.
// If the runtime environment settings fail, they are not locked.
func (m *manager) SetAndLock(env Env) error {
m.mutex.Lock()
defer m.mutex.Unlock()
if err := m.set(env); err != nil {
return err
}
atomic.StoreInt32(&m.locked, 1)
return nil
}
// Sets the current runtime environment.
func (m *manager) set(env Env) error {
if m.Locked() {
return ErrLocked
}
if !env.In(m.registered) {
return ErrInvalidEnv
}
if old := m.current; !old.Is(env) {
m.current = env
// Trigger all listeners synchronously.
for i, j := 0, len(m.listeners); i < j; i++ {
m.listeners[i](env, old)
}
}
return nil
}
// Listen method adds a given runtime environment listener.
// If the given listener is nil, ignore it.
func (m *manager) Listen(listener Listener) {
if listener != nil {
m.mutex.Lock()
m.listeners = append(m.listeners, listener)
m.mutex.Unlock()
}
}
// UnListen removes and returns to the recently added listener.
// If there is no listener to be removed, nil is returned.
func (m *manager) UnListen() (r Listener) {
m.mutex.Lock()
defer m.mutex.Unlock()
if n := len(m.listeners) - 1; n >= 0 {
r = m.listeners[n]
if n == 0 {
m.listeners = nil
} else {
m.listeners = m.listeners[:n]
}
}
return
}
// UnListenAll removes and returns all added listeners.
// If there is no listener to be removed, nil is returned.
func (m *manager) UnListenAll() (r []Listener) {
m.mutex.Lock()
defer m.mutex.Unlock()
if len(m.listeners) > 0 {
r = m.listeners
m.listeners = nil
}
return
}