This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
cache.go
172 lines (155 loc) · 3.67 KB
/
cache.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
package main
import (
"hash/fnv"
"log"
"math"
"strconv"
"time"
"github.com/dustin/gojson"
"github.com/dustin/gomemcached"
"github.com/dustin/gomemcached/client"
)
var cacheInput chan *processIn
var cacheInputSet chan *processOut
func cacheReceiveLoop(client *memcached.Client, out chan *processOut) {
for {
res, err := memcached.UnwrapMemcachedError(client.Receive())
if err != nil {
log.Printf("Error receiving from memcache: %v", err)
return
}
po := processOut{cacheOpaque: res.Opaque}
if res.Opcode == gomemcached.GET && res.Status == gomemcached.SUCCESS {
rv := map[string][]interface{}{}
err = json.Unmarshal(res.Body, &rv)
if err == nil {
po.value = rv["v"]
} else {
log.Printf("Decoding error: %v\n%s", err, res.Body)
po.err = err
}
} else {
po.err = res
}
out <- &po
}
}
func cacheOpener(ch chan<- *memcached.Client) {
log.Printf("Connecting to memcached")
client, err := memcached.Connect("tcp", *cacheAddr)
if err != nil {
log.Printf("Failed to connect to memcached: %v", err)
}
ch <- client
}
func cacheProcessor(ch <-chan *processIn, chset <-chan *processOut) {
omap := map[uint32]*processIn{}
opaque := uint32(100)
out := make(chan *processOut)
connch := make(chan *memcached.Client)
go cacheOpener(connch)
var client *memcached.Client
for {
select {
case client = <-connch:
if client == nil {
time.AfterFunc(time.Second, func() {
cacheOpener(connch)
})
} else {
go cacheReceiveLoop(client, out)
}
case pi := <-ch:
switch {
case client == nil:
// No connection, pass through
processorInput <- pi
case time.Now().Before(pi.before):
newOpaque := opaque
opaque++
if opaque == math.MaxUint32 {
opaque = 100
}
omap[newOpaque] = pi
pi.cacheKey = cacheKey(pi)
req := &gomemcached.MCRequest{
Opcode: gomemcached.GET,
Opaque: newOpaque,
Key: []byte(pi.cacheKey),
}
err := client.Transmit(req)
if err != nil {
log.Printf("Error transmitting!: %v", err)
client.Close()
client = nil
go cacheOpener(connch)
for _, v := range omap {
processorInput <- v
}
omap = map[uint32]*processIn{}
}
default:
// Too old.
pi.out <- &processOut{"", pi.key, nil, errTimeout, 0}
}
case po := <-out:
pi, ok := omap[po.cacheOpaque]
if ok {
delete(omap, po.cacheOpaque)
po.key = pi.key
if po.err == nil {
po.cacheKey = pi.cacheKey
pi.out <- po
} else {
processorInput <- pi
}
} else {
if po.cacheOpaque > 10 {
log.Printf("Unknown opaque: %v", po.cacheOpaque)
}
}
case po := <-chset:
if client != nil {
bytes, err := json.Marshal(po)
if err == nil {
req := &gomemcached.MCRequest{
Opcode: gomemcached.SETQ,
Key: []byte(po.cacheKey),
Cas: 0,
Opaque: 1,
Extras: []byte{0, 0, 0, 0, 0, 0, 0, 0},
Body: bytes}
err = client.Transmit(req)
if err != nil {
log.Printf("Error transmitting!: %v", err)
client.Close()
client = nil
go cacheOpener(connch)
for _, v := range omap {
processorInput <- v
}
omap = map[uint32]*processIn{}
}
} else {
log.Fatalf("Error marshaling %v: %v", po, err)
}
} // has client, will cache
}
}
}
func cacheKey(p *processIn) string {
h := fnv.New64()
for _, i := range p.infos {
i.WriteIDTo(h)
}
for i := range p.ptrs {
h.Write([]byte(p.ptrs[i]))
h.Write([]byte(p.reds[i]))
}
for i := range p.filters {
h.Write([]byte(p.filters[i]))
h.Write([]byte(p.filtervals[i]))
}
return p.dbname + "#" + strconv.FormatInt(p.key, 10) +
"#" + strconv.FormatUint(h.Sum64(), 10)
}