-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
54 lines (45 loc) · 1.11 KB
/
container.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
package main
import (
log "github.com/sirupsen/logrus"
"sync"
)
type Container struct {
mux sync.RWMutex
services map[string]interface{}
}
func NewContainer() *Container {
return &Container{
services: make(map[string]interface{}),
}
}
func (container *Container) Register(name string, service interface{}) {
log.WithFields(log.Fields{
"name": name,
}).Trace("Registering new service into container")
container.mux.Lock()
defer container.mux.Unlock()
container.services[name] = service
}
func (container *Container) Unregister(name string) {
log.WithFields(log.Fields{
"name": name,
}).Trace("Unregistering new service from container")
container.mux.Lock()
defer container.mux.Unlock()
delete(container.services, name)
}
func (container *Container) Get(name string) interface{} {
log.WithFields(log.Fields{
"name": name,
}).Trace("Getting service from container")
container.mux.RLock()
defer container.mux.RUnlock()
service, ok := container.services[name]
if !ok {
log.WithFields(log.Fields{
"name": name,
}).Debug("Service not available in container")
return nil
}
return service
}