forked from gateway-fm/service-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices_pool.go
144 lines (111 loc) · 3.49 KB
/
services_pool.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
141
142
143
144
package pool
import (
"github.com/gateway-fm/prover-pool-lib/service"
)
// IServicesPool holds information about reachable
// active services, manage connections and discovery
type IServicesPool interface {
// Start run service pool discovering
// and healthchecks loops
Start(healthchecks bool)
// NextService returns next active service
// to take a connection
NextService() service.IService
// Count return numbers of
// all healthy services in pool
Count() int
// List return ServicesPool ServicesList instance
//List() IServicesList
// Close Stop all service pool
Close()
AddService(srv service.IService)
AnyByTag(tag string) service.IService
NextLeastLoaded(tag string) service.IService
NextLeastLoadedProver(tag string) service.IService
// FromHealthyToJail move Unhealthy service
// from Healthy slice to Jail map
FromHealthyToJail(id string)
// FromJailToHealthy move Healthy service
// from Jail map to Healthy slice
FromJailToHealthy(srv service.IService)
// RemoveFromJail remove given
// service from jail map
RemoveFromJail(srv service.IService)
}
// ServicesPool holds information about reachable
// active services, manage connections and discovery
type ServicesPool struct {
name string
list IServicesList
stop chan struct{}
MutationFnc func(srv service.IService) (service.IService, error)
}
// ServicesPoolsOpts is options that needs
// to configure ServicePool instance
type ServicesPoolsOpts struct {
Name string // service name to use in service pool
ListOpts *ServicesListOpts // service list configuration
}
type ServiceCallbackE func(srv service.IService) error
type ServiceCallback func(srv service.IService)
type ServiceCallbackB func(srv service.IService) bool
// NewServicesPool create new Services Pool
// based on given params
func NewServicesPool(opts *ServicesPoolsOpts) IServicesPool {
pool := &ServicesPool{
name: opts.Name,
stop: make(chan struct{}),
}
pool.list = NewServicesList(opts.Name, opts.ListOpts)
return pool
}
// Start run service pool discovering
// and healthchecks loops
func (p *ServicesPool) Start(healthchecks bool) {
if healthchecks {
go p.list.HealthChecksLoop()
}
}
// NextService returns next active service
// to take a connection
func (p *ServicesPool) NextService() service.IService {
// TODO maybe is better to return error if next service is nil
return p.list.Next()
}
func (p *ServicesPool) NextLeastLoaded(tag string) service.IService {
// TODO maybe is better to return error if next service is nil
return p.list.NextLeastLoaded(tag)
}
func (p *ServicesPool) NextLeastLoadedProver(tag string) service.IService {
// TODO maybe is better to return error if next service is nil
return p.list.NextLeastLoadedProver(tag)
}
func (p *ServicesPool) AddService(srv service.IService) {
p.list.Add(srv)
}
// Count return numbers of
// all healthy services in pool
func (p *ServicesPool) Count() int {
return len(p.list.Healthy())
}
// List return ServicesPool ServicesList instance
//func (p *ServicesPool) List() IServicesList {
// return p.list
//}
// Close Stop all service pool
func (p *ServicesPool) Close() {
p.list.Close()
close(p.stop)
}
func (p *ServicesPool) AnyByTag(tag string) service.IService {
return p.list.AnyByTag(tag)
}
func (p *ServicesPool) FromHealthyToJail(id string) {
p.list.FromHealthyToJail(id)
}
func (p *ServicesPool) FromJailToHealthy(srv service.IService) {
p.list.FromJailToHealthy(srv)
}
func (p *ServicesPool) RemoveFromJail(srv service.IService) {
p.list.RemoveFromJail(srv)
}