-
Notifications
You must be signed in to change notification settings - Fork 16
/
serviceregistry.go
140 lines (116 loc) · 3.22 KB
/
serviceregistry.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package service
import (
"context"
"fmt"
"github.com/Qitmeer/qng/rpc/api"
"reflect"
)
type IService interface {
// APIs retrieves the list of RPC descriptors the service provides
APIs() []api.API
// Start is called after all services have been constructed and the networking
// layer was also initialized to spawn any goroutines required by the service.
Start() error
// Stop terminates all goroutines belonging to the service, blocking until they
// are all terminated.
Stop() error
Status() error
IsStarted() bool
IsShutdown() bool
Context() context.Context
SetKind(kind reflect.Type)
}
type ServiceRegistry struct {
services map[reflect.Type]IService // map of types to services.
serviceTypes []reflect.Type // keep an ordered slice of registered service types.
}
func NewServiceRegistry() *ServiceRegistry {
return &ServiceRegistry{
services: make(map[reflect.Type]IService),
}
}
func (s *ServiceRegistry) StartAll() error {
log.Debug(fmt.Sprintf("Starting %d services: %v", len(s.serviceTypes), s.serviceTypes))
for _, kind := range s.serviceTypes {
log.Debug(fmt.Sprintf("Starting service type %v", kind))
err := s.services[kind].Start()
if err != nil {
return err
}
}
return nil
}
func (s *ServiceRegistry) StopAll() error {
result := ""
for i := len(s.serviceTypes) - 1; i >= 0; i-- {
kind := s.serviceTypes[i]
service := s.services[kind]
if err := service.Stop(); err != nil {
log.Error(fmt.Sprintf("Could not stop the following service: %v, %v", kind, err))
result += fmt.Sprintf("(%v)", kind)
}
}
if len(result) > 0 {
return fmt.Errorf("%s", result)
}
return nil
}
func (s *ServiceRegistry) Statuses() map[reflect.Type]error {
m := make(map[reflect.Type]error)
for _, kind := range s.serviceTypes {
m[kind] = s.services[kind].Status()
}
return m
}
func (s *ServiceRegistry) RegisterService(service IService) error {
kind := reflect.TypeOf(service)
if _, exists := s.services[kind]; exists {
return fmt.Errorf("service already exists: %v", kind)
}
service.SetKind(kind)
s.services[kind] = service
s.serviceTypes = append(s.serviceTypes, kind)
return nil
}
func (s *ServiceRegistry) LowestPriority(service IService) bool {
kind := reflect.TypeOf(service)
_, exists := s.services[kind]
if !exists {
return false
}
serviceTypes:=[]reflect.Type{}
for _, k := range s.serviceTypes {
if kind == k {
continue
}
serviceTypes = append(serviceTypes,k)
}
serviceTypes = append(serviceTypes,kind)
s.serviceTypes = serviceTypes
return true
}
func (s *ServiceRegistry) FetchService(service interface{}) error {
if reflect.TypeOf(service).Kind() != reflect.Ptr {
return fmt.Errorf("input must be of pointer type, received value type instead: %T", service)
}
element := reflect.ValueOf(service).Elem()
if running, ok := s.services[element.Type()]; ok {
element.Set(reflect.ValueOf(running))
return nil
}
return fmt.Errorf("unknown service: %T", service)
}
func (s *ServiceRegistry) GetServices() map[reflect.Type]IService {
return s.services
}
func (s *ServiceRegistry) APIs() []api.API {
apis := []api.API{}
for _, kind := range s.serviceTypes {
a := s.services[kind].APIs()
if a == nil {
continue
}
apis = append(apis, a...)
}
return apis
}