-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.go
81 lines (75 loc) · 1.53 KB
/
queue.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
package main
import (
"time"
)
type CommandQueue struct {
RPS int
ChunkSize int
CommandsCh chan VKCommand
ChunksCh chan VKCommandsChunk
}
func NewCommandsQueue(rps int) *CommandQueue {
return &CommandQueue{
RPS: rps,
ChunkSize: 25,
CommandsCh: make(chan VKCommand),
ChunksCh: make(chan VKCommandsChunk),
}
}
func (queue *CommandQueue) Run() {
go func() {
buffer := make(map[string]VKCommands)
ticker := time.NewTicker(time.Second)
for {
select {
case command := <-queue.CommandsCh:
logger.Debugf("append command to queue: %+v", command)
buffer[command.AccessToken] = append(
buffer[command.AccessToken],
command,
)
case <-ticker.C:
for accessToken, commands := range buffer {
logger.Debugf(
"delivering %d commands for access token %s",
len(commands),
accessToken,
)
if queue.deliver(commands, accessToken) {
delete(buffer, accessToken)
}
}
}
}
}()
}
func (queue *CommandQueue) deliver(
commands VKCommands,
accessToken string,
) bool {
total := len(commands)
if total == 0 {
return true
}
delivered := 0
i := 0
for ; i < queue.RPS && delivered < total; i++ {
size := len(commands)
if size > queue.ChunkSize {
size = queue.ChunkSize
}
queue.ChunksCh <- VKCommandsChunk{
AccessToken: accessToken,
Commands: commands[:size],
}
commands = commands[size:]
delivered += size
}
logger.Infof(
"delivered %d of %d commands in %d batches",
delivered,
total,
i,
)
return total == delivered
}