-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmanager.go
90 lines (77 loc) · 1.67 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
package main
import "sync"
/*
Simple interface that allows us to switch out both implementations of the Manager
*/
type ConfigManager interface {
Set(*Config)
Get() *Config
Close()
}
/*
This struct manages the configuration instance by
preforming locking around access to the Config struct.
*/
type MutexConfigManager struct {
conf *Config
mutex *sync.Mutex
}
func NewMutexConfigManager(conf *Config) *MutexConfigManager {
return &MutexConfigManager{conf, &sync.Mutex{}}
}
func (self *MutexConfigManager) Set(conf *Config) {
self.mutex.Lock()
self.conf = conf
self.mutex.Unlock()
}
func (self *MutexConfigManager) Get() *Config {
self.mutex.Lock()
temp := self.conf
self.mutex.Unlock()
return temp
}
func (self *MutexConfigManager) Close() {
//Do Nothing
}
/*
This struct manages the configuration instance by feeding a
pointer through a channel whenever the user calls Get()
*/
type ChannelConfigManager struct {
conf *Config
get chan *Config
set chan *Config
done chan bool
}
func NewChannelConfigManager(conf *Config) *ChannelConfigManager {
parser := &ChannelConfigManager{conf, make(chan *Config), make(chan *Config), make(chan bool)}
parser.Start()
return parser
}
func (self *ChannelConfigManager) Start() {
go func() {
defer func() {
close(self.get)
close(self.set)
close(self.done)
}()
for {
select {
case self.get <- self.conf:
case value := <-self.set:
self.conf = value
case <-self.done:
return
}
}
}()
}
func (self *ChannelConfigManager) Close() {
self.done <- true
}
func (self *ChannelConfigManager) Set(conf *Config) {
self.set <- conf
}
func (self *ChannelConfigManager) Get() *Config {
return <-self.get
}