forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommander.go
63 lines (46 loc) · 1022 Bytes
/
commander.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
package core
import (
"sync"
"google.golang.org/grpc"
)
// ServiceRegistryCallback is a callback function for registering services.
type ServiceRegistryCallback func(s *grpc.Server)
// Commander is a feature that accepts commands from external source.
type Commander interface {
Feature
// RegisterService registers a service into this Commander.
RegisterService(ServiceRegistryCallback)
}
type syncCommander struct {
sync.RWMutex
Commander
}
func (c *syncCommander) RegisterService(callback ServiceRegistryCallback) {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return
}
c.Commander.RegisterService(callback)
}
func (c *syncCommander) Start() error {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return nil
}
return c.Commander.Start()
}
func (c *syncCommander) Close() {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return
}
c.Commander.Close()
}
func (c *syncCommander) Set(commander Commander) {
c.Lock()
defer c.Unlock()
c.Commander = commander
}