forked from scotow/burgoking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
166 lines (130 loc) · 3.25 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package burgoking
import (
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"sync"
"time"
)
var (
InvalidPoolSettings = errors.New("invalid pool settings")
)
type code struct {
payload string
cancel *time.Timer
}
type codeRequest struct {
codeC chan<- string
id uuid.UUID
}
type Pool struct {
size int
expiration time.Duration
retry time.Duration
codeLock sync.Mutex
codes []code
queueLock sync.Mutex
queue []codeRequest
}
func NewPool(size int, expiration, retry time.Duration) (pool *Pool, err error) {
if size <= 0 || expiration <= 0 || retry < 0 {
err = InvalidPoolSettings
return
}
pool = &Pool{
size: size,
expiration: expiration,
retry: retry,
codes: make([]code, 0),
queue: make([]codeRequest, 0),
}
go pool.fill()
return
}
func (p *Pool) fill() {
for i := 0; i < p.size; i++ {
p.generateCode()
}
}
func (p *Pool) GetCode(codeC chan<- string, cancelC <-chan struct{}) {
p.codeLock.Lock()
if len(p.codes) >= 1 {
code := p.codes[0]
code.cancel.Stop()
p.codes = p.codes[1:]
p.codeLock.Unlock()
go p.generateCode()
logrus.WithFields(logrus.Fields{"code": code.payload}).Info("Code removed from pool and transferred.")
codeC <- code.payload
} else {
p.codeLock.Unlock()
payloadC := make(chan string)
request := codeRequest{payloadC, uuid.New()}
p.queueLock.Lock()
p.queue = append(p.queue, request)
p.queueLock.Unlock()
logrus.WithFields(logrus.Fields{"uuid": request.id}).Info("Request appended to the queue.")
select {
case code := <-payloadC:
codeC <- code
case <-cancelC:
p.cancelRequest(request)
}
}
}
func (p *Pool) generateCode() {
var c code
for {
payload, err := GenerateCode(nil)
if err == nil {
c = code{payload: payload}
break
} else {
logrus.WithFields(logrus.Fields{"error": err}).Warn("Code generation failed.")
time.Sleep(p.retry)
}
}
p.queueLock.Lock()
if len(p.queue) >= 1 {
r := p.queue[0]
p.queue = p.queue[1:]
p.queueLock.Unlock()
r.codeC <- c.payload
go p.generateCode()
logrus.WithFields(logrus.Fields{"code": c.payload}).Info("Code generated and directly transferred.")
} else {
p.queueLock.Unlock()
p.codeLock.Lock()
c.cancel = time.AfterFunc(p.expiration, func() {
p.expireCode(&c)
})
p.codes = append(p.codes, c)
p.codeLock.Unlock()
logrus.WithFields(logrus.Fields{"code": c.payload}).Info("Code generated and added to the pool.")
}
}
func (p *Pool) expireCode(code *code) {
p.codeLock.Lock()
defer p.codeLock.Unlock()
for i, c := range p.codes {
if c.payload == code.payload {
p.codes = append(p.codes[:i], p.codes[i+1:]...)
go p.generateCode()
logrus.WithFields(logrus.Fields{"code": code.payload}).Info("Code deleted from the pool for expiration.")
return
}
}
logrus.WithFields(logrus.Fields{"code": code.payload}).Warn("Cannot delete code from pool.")
}
func (p *Pool) cancelRequest(request codeRequest) {
p.queueLock.Lock()
defer p.queueLock.Unlock()
for i, r := range p.queue {
if r.id == request.id {
p.queue = append(p.queue[:i], p.queue[i+1:]...)
logrus.WithFields(logrus.Fields{"uuid": request.id}).Info("Request canceled.")
return
}
}
logrus.WithFields(logrus.Fields{"uuid": request.id}).Warn("Cannot remove request from queue.")
}