/
service_registry.go
46 lines (37 loc) · 1.06 KB
/
service_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
package server
import (
"sync"
)
// ServiceRegistry holds (opinionated) service objects for handling specific requests
type ServiceRegistry struct {
services map[string]Service
lock *sync.RWMutex
}
// NewServiceRegistry creates a new registry
func NewServiceRegistry() ServiceRegistry {
return ServiceRegistry{
services: make(map[string]Service),
lock: &sync.RWMutex{},
}
}
// Register registers a new service with a list of references
func (rlh *ServiceRegistry) Register(listName string, svc Service) *ServiceRegistry {
rlh.lock.Lock()
rlh.services[listName] = svc
rlh.lock.Unlock()
return rlh
}
// GetServiceForList returns a service able to handle a specific list
func (rlh ServiceRegistry) GetServiceForList(name string) Service {
rlh.lock.RLock()
svc := rlh.services[name]
rlh.lock.RUnlock()
return svc
}
// HasServiceForList returns true only if a service has been registered for a certain list
func (rlh ServiceRegistry) HasServiceForList(name string) bool {
rlh.lock.RLock()
_, ok := rlh.services[name]
rlh.lock.RUnlock()
return ok
}