-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
52 lines (39 loc) · 917 Bytes
/
registry.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
package clients
import (
"sync"
)
type Key string
// Registry contains all clients used by application
type Registry struct {
mutex sync.RWMutex
clients map[Key]Client
}
// Add stores provided client in regsitry
func (registry *Registry) Add(key Key, client Client) {
registry.mutex.Lock()
defer registry.mutex.Unlock()
registry.clients[key] = client
}
// Get returns client linked with a key
func (registry *Registry) Get(key Key) Client {
registry.mutex.RLock()
defer registry.mutex.RUnlock()
if client, ok := registry.clients[key]; ok {
return client
}
return Passthrough{Null(NoFallback{})}
}
var registry Registry
func init() {
registry = Registry{
clients: make(map[Key]Client),
}
}
// Get returns client linked with a key
func Get(key Key) Client {
return registry.Get(key)
}
// Add link provided client with a key
func Add(key Key, client Client) {
registry.Add(key, client)
}