-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
216 lines (190 loc) · 4.28 KB
/
server.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package t1k
import (
"log"
"net"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/chaitin/t1k-go/detection"
"github.com/chaitin/t1k-go/misc"
)
const (
DEFAULT_POOL_SIZE = 8
HEARTBEAT_INTERVAL = 20
)
type Server struct {
socketFactory func() (net.Conn, error)
poolCh chan *conn
poolSize int
count int
closeCh chan struct{}
logger *log.Logger
mu sync.Mutex
healthCheck *HealthCheckService
}
func (s *Server) newConn() error {
sock, err := s.socketFactory()
if err != nil {
return err
}
s.count += 1
s.poolCh <- makeConn(sock, s)
return nil
}
func (s *Server) GetConn() (*conn, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.count < s.poolSize {
for i := 0; i < (s.poolSize - s.count); i++ {
err := s.newConn()
if err != nil {
return nil, err
}
}
}
c := <-s.poolCh
return c, nil
}
func (s *Server) PutConn(c *conn) {
s.mu.Lock()
defer s.mu.Unlock()
if c.failing {
s.count -= 1
c.Close()
} else {
s.poolCh <- c
}
}
func (s *Server) broadcastHeartbeat() {
l := len(s.poolCh)
for i := 0; i < l; i++ {
select {
case c := <-s.poolCh:
c.Heartbeat()
s.PutConn(c)
default:
return
}
}
}
func (s *Server) runHeartbeatCo() {
interval := HEARTBEAT_INTERVAL
intervalRaw := os.Getenv("T1K_HEARTBEAT_INTERVAL")
if intervalRaw != "" {
val, err := strconv.Atoi(intervalRaw)
if err == nil {
interval = val
}
}
for {
timer := time.NewTimer(time.Duration(interval) * time.Second)
select {
case <-s.closeCh:
return
case <-timer.C:
}
s.broadcastHeartbeat()
}
}
func (s *Server) UpdateHealthCheckConfig(config *HealthCheckConfig) error {
return s.healthCheck.UpdateConfig(config)
}
func (s *Server) IsHealth() bool {
return s.healthCheck.IsHealth()
}
func (s *Server) HealthCheckStats() HealthCheckStats {
stats := s.healthCheck.HealthCheckStats()
return stats
}
func NewFromSocketFactoryWithPoolSize(socketFactory func() (net.Conn, error), poolSize int) (*Server, error) {
ret := &Server{
socketFactory: socketFactory,
poolCh: make(chan *conn, poolSize),
poolSize: poolSize,
closeCh: make(chan struct{}),
logger: log.New(os.Stdout, "snserver", log.LstdFlags),
mu: sync.Mutex{},
}
for i := 0; i < poolSize; i++ {
err := ret.newConn()
if err != nil {
return nil, err
}
}
healthCheck, err := NewHealthCheckService()
if err != nil {
return nil, err
}
ret.healthCheck = healthCheck
go ret.runHeartbeatCo()
go ret.healthCheck.Run()
return ret, nil
}
func NewFromSocketFactory(socketFactory func() (net.Conn, error)) (*Server, error) {
return NewFromSocketFactoryWithPoolSize(socketFactory, DEFAULT_POOL_SIZE)
}
func NewWithPoolSize(addr string, poolSize int) (*Server, error) {
return NewFromSocketFactoryWithPoolSize(func() (net.Conn, error) {
return net.Dial("tcp", addr)
}, poolSize)
}
func New(addr string) (*Server, error) {
return NewWithPoolSize(addr, DEFAULT_POOL_SIZE)
}
func (s *Server) DetectRequestInCtx(dc *detection.DetectionContext) (*detection.Result, error) {
c, err := s.GetConn()
if err != nil {
return nil, err
}
defer s.PutConn(c)
return c.DetectRequestInCtx(dc)
}
func (s *Server) DetectResponseInCtx(dc *detection.DetectionContext) (*detection.Result, error) {
c, err := s.GetConn()
if err != nil {
return nil, misc.ErrorWrap(err, "")
}
defer s.PutConn(c)
return c.DetectResponseInCtx(dc)
}
func (s *Server) Detect(dc *detection.DetectionContext) (*detection.Result, *detection.Result, error) {
c, err := s.GetConn()
if err != nil {
return nil, nil, misc.ErrorWrap(err, "")
}
reqResult, rspResult, err := c.Detect(dc)
if err == nil {
s.PutConn(c)
}
return reqResult, rspResult, err
}
func (s *Server) DetectHttpRequest(req *http.Request) (*detection.Result, error) {
c, err := s.GetConn()
if err != nil {
return nil, err
}
defer s.PutConn(c)
return c.DetectHttpRequest(req)
}
func (s *Server) DetectRequest(req detection.Request) (*detection.Result, error) {
c, err := s.GetConn()
if err != nil {
return nil, err
}
defer s.PutConn(c)
return c.DetectRequest(req)
}
// blocks until all pending detection is completed
func (s *Server) Close() {
close(s.closeCh)
for i := 0; i < s.count; i++ {
c, err := s.GetConn()
if err != nil {
return
}
c.Close()
}
s.healthCheck.Close()
}