-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
284 lines (251 loc) · 5.88 KB
/
service.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package powtcp
import (
"context"
"encoding/gob"
"errors"
"fmt"
"net"
"os"
"sync"
"time"
"powtcp/pkg/buffer"
"powtcp/pkg/log"
"powtcp/pkg/pool"
"powtcp/pkg/pow"
"powtcp/proto"
)
var quotes = []string{
"Don't be a Jack of all trades and master of none.",
"Be sticky to your research goal.",
}
// Service represents the pow tcp server.
type Service struct {
ctx context.Context
cfg config
log logger
buffer *buffer.Buffer
pool *pool.Pool
difficulty int
rps int
mux sync.RWMutex
listener net.Listener
}
// Init creates Service instance.
func Init(ctx context.Context) (*Service, error) {
cfg, err := loadConfig()
if err != nil {
err = fmt.Errorf("Service.Init: can't load configuration: %w", err)
return nil, err
}
log, err := log.New(cfg.logLevel, os.Stdout, os.Stderr)
if err != nil {
err = fmt.Errorf("Service.Init: can't init logger: %w", err)
return nil, err
}
// init challenge bucket buffer
bufferSize := cfg.rpsTarget
buffer, err := buffer.New(
log,
cfg.challengeLen,
bufferSize)
if err != nil {
err = fmt.Errorf("Service.Init: can't init challenge buffer: %w", err)
return nil, err
}
// init registry pool to track active challenges
pool := pool.New(
ctx,
cfg.challengeTTL,
cfg.challengePoolCleanupInterval)
// init difficulty with min value on start
difficulty := pow.MinDifficulty
s := &Service{
ctx: ctx,
cfg: cfg,
log: log,
buffer: buffer,
pool: pool,
difficulty: difficulty,
}
return s, nil
}
// Run begins to listen tcp socket until ctx is canceled.
func (s *Service) Run() {
ctx, cancel := context.WithCancel(s.ctx)
defer cancel()
addr := fmt.Sprintf("%s:%d", s.cfg.ip, s.cfg.port)
lc := &net.ListenConfig{}
var err error
s.listener, err = lc.Listen(ctx, "tcp", addr)
if err != nil {
s.log.Errorf("Service.Run: tcp socket bind failed: %w", err)
return
}
s.log.Infof("Service.Run: serving tcp socket on %q…", addr)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
defer cancel()
for {
conn, err := s.listener.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
err = fmt.Errorf(
"Service.Run: error on s.listener.Accept: %w", err)
s.log.Error(err)
continue
}
go s.handleConn(conn)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
defer cancel()
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
// adjust difficulty by rps
s.adjustDifficultyByRPS()
}
}
}()
// handle shutdown
select {
case <-ctx.Done():
case <-s.ctx.Done():
s.log.Info("Service.Run: interrupt syscall received")
cancel()
}
s.log.Info("Service.Run: cleaning up…")
s.listener.Close()
s.log.Info("Service.Run: cleanup done, waiting workers to stop…")
wg.Wait()
s.log.Info("Service.Run: shutdown")
}
func (s *Service) handleConn(conn net.Conn) {
defer conn.Close()
if s.cfg.connReadTTL > 0 {
err := conn.SetReadDeadline(time.Now().Add(s.cfg.connReadTTL))
if err != nil {
err = fmt.Errorf(
"Service.handleConn: can't set read deadline: %w", err)
s.log.Error(err)
return
}
}
if s.cfg.connWriteTTL > 0 {
err := conn.SetWriteDeadline(time.Now().Add(s.cfg.connWriteTTL))
if err != nil {
err = fmt.Errorf(
"Service.handleConn: can't set write deadline: %w", err)
s.log.Error(err)
return
}
}
s.log.Debugf(
"Service.handleConn: accept client conn %q",
conn.RemoteAddr().String())
defer func() {
s.log.Debugf(
"Service.handleConn: close client conn %q",
conn.RemoteAddr().String())
}()
for {
dec := gob.NewDecoder(conn)
message := proto.Message{}
err := dec.Decode(&message)
if err != nil {
err = fmt.Errorf(
"Service.handleConn: decode message error: %w", err)
s.log.Error(err)
return
}
switch message.MessageType {
case proto.RequestServiceType:
s.log.Debug("Service.handleConn: got request service")
err = s.handleRequestService(conn)
if err != nil {
err = fmt.Errorf("Service.handleConn: %w", err)
s.log.Error(err)
err = s.handleError(conn, "try again later")
if err != nil {
err = fmt.Errorf("Service.handleConn: %w", err)
s.log.Error(err)
}
return
}
case proto.ResponseChallengeType:
s.log.Debug("Service.handleConn: got response challenge")
err = s.handleResponseChallenge(conn, message.Data)
if err != nil {
err = fmt.Errorf("Service.handleConn: %w", err)
s.log.Error(err)
err = s.handleError(conn, "try again later")
if err != nil {
err = fmt.Errorf("Service.handleConn: %w", err)
s.log.Error(err)
}
return
}
default:
s.log.Debugf(
"Service.handleConn: got unknown message type: %d",
message.MessageType)
err = s.handleError(
conn, fmt.Sprintf("unknown message type: %d", message.MessageType))
if err != nil {
err = fmt.Errorf("Service.handleConn: %w", err)
s.log.Error(err)
}
return
}
}
}
func (s *Service) adjustDifficultyByRPS() {
s.mux.Lock()
rps := s.rps
difficulty := s.difficulty
if s.rps < s.cfg.rpsTarget && s.difficulty > 1 {
s.difficulty -= 1
} else if s.rps > s.cfg.rpsTarget && s.difficulty < pow.MaxDifficulty {
s.difficulty += 1
}
s.rps = 0
s.mux.Unlock()
s.log.Debugf("difficulty: %d, rps: %d", difficulty, rps)
}
func (s *Service) adjustDifficulty(expires time.Time) {
from := expires.Add(-s.cfg.challengeTTL)
if time.Now().Sub(from) < s.cfg.challengeTTL {
s.mux.Lock()
if s.difficulty < pow.MaxDifficulty {
s.difficulty += 1
}
s.mux.Unlock()
} else {
s.mux.Lock()
if s.difficulty > 1 {
s.difficulty -= 1
}
s.mux.Unlock()
}
}
// logger desribes interface of log object.
type logger interface {
Debug(...interface{})
Debugf(string, ...interface{})
Info(...interface{})
Infof(string, ...interface{})
Warning(...interface{})
Warningf(string, ...interface{})
Error(...interface{})
Errorf(string, ...interface{})
}