-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.go
More file actions
78 lines (67 loc) · 1.75 KB
/
queue.go
File metadata and controls
78 lines (67 loc) · 1.75 KB
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
package main
import (
"log"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/redcon"
)
var syncMirror []chan bool
var contMirror []chan bool
var doneMirror []chan bool
var pauseMain []chan bool
var contMain []chan bool
type mirrorCmd struct {
rdb redis.Conn
cmdArgs []interface{}
}
type mainCmd struct {
conn redcon.Conn
cmdArgs []interface{}
}
var N = 10
func initQueues() {
for i := 0; i < N; i++ {
syncMirror = append(syncMirror, make(chan bool))
contMirror = append(contMirror, make(chan bool))
doneMirror = append(doneMirror, make(chan bool))
pauseMain = append(pauseMain, make(chan bool))
contMain = append(contMain, make(chan bool))
go mainDo(i)
go mirrorDo(i)
}
}
// This function starts as a goroutine. A concurrent process.
// mirrorDo loops forever and run the queued commands against the mirror redis
func mirrorDo(i int) {
for {
select {
case next := <-mirrorQueue:
_, err := next.rdb.Do(next.cmdArgs[0].(string), next.cmdArgs[1:]...)
if err != nil {
log.Println("Mirror command failed:", err.Error())
}
case <-syncMirror[i]:
doneMirror[i] <- true
<-contMirror[i]
}
}
}
func mainDo(i int) {
for {
select {
case next := <-mainQueue:
res, err := pickMain(next.conn).Do(next.cmdArgs[0].(string), next.cmdArgs[1:]...)
if err == nil {
// This will queue the mirror commands until the channel reaches its capacity.
// If channel is full, commands will be ignored and we get log and error
select {
case mirrorQueue <- mirrorCmd{rdb: pickMirror(next.conn), cmdArgs: next.cmdArgs}:
default:
log.Println("Queue full, skipping", next.cmdArgs)
}
}
next.conn.Context().(RedisSettings).replyChan <- reply{res: res, err: err}
case <-pauseMain[i]:
<-contMain[i]
}
}
}