-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
239 lines (215 loc) · 5.63 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
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
package main
import (
"fmt"
"net"
)
// ConnectStatus is the result of a connection check
type ConnectStatus int
var (
ConnectStatusPending ConnectStatus // address has not been checked yet
ConnectStatusSuccess ConnectStatus = 1 // connection to the address was successful
ConnectStatusFailure ConnectStatus = 2 // connection to the address has failed
)
func (s ConnectStatus) String() string {
switch s {
case ConnectStatusPending:
return "pending"
case ConnectStatusSuccess:
return "success"
case ConnectStatusFailure:
return "failure"
default:
return "unknown"
}
}
// Cache is responsible for storing connection tests.
type Cache struct {
games map[int]CacheItem
updateRequestChan chan cacheReq
checkResultChan chan cacheCheckMsg
requestGamesChan chan chan map[int]CacheItem
GameUpdates *Notifier // notifies about updated cache items (CacheUpdate)
}
// NewCache creates a new cache.
func NewCache() *Cache {
c := &Cache{
games: make(map[int]CacheItem),
updateRequestChan: make(chan cacheReq),
checkResultChan: make(chan cacheCheckMsg),
requestGamesChan: make(chan chan map[int]CacheItem),
GameUpdates: NewNotifier(),
}
go c.run()
return c
}
// UpdateAllGames inserts and updates the given games, deleting all others from
// the cache.
func (c *Cache) UpdateAllGames(games []LeagueGame) {
c.updateRequestChan <- cacheReq{
reqType: reqUpdateAll,
payload: games,
}
}
// UpdateGame inserts or updates a single game.
func (c *Cache) UpdateGame(game LeagueGame) {
c.updateRequestChan <- cacheReq{
reqType: reqUpdateSingle,
id: game.ID,
payload: game,
}
}
// UpdateAddrs updates a game's addresses.
func (c *Cache) UpdateAddrs(id int, addrs []net.Addr) {
c.updateRequestChan <- cacheReq{
reqType: reqUpdateAddrs,
id: id,
payload: addrs,
}
}
// DeleteGame removes a game from the cache.
func (c *Cache) DeleteGame(id int) {
c.updateRequestChan <- cacheReq{
reqType: reqDelete,
id: id,
}
}
// Get retrieves a copy of the currently-cached games.
func (c *Cache) Get() map[int]CacheItem {
res := make(chan map[int]CacheItem)
c.requestGamesChan <- res
return <-res
}
// internal (run): copyState copies the cache state.
func (c *Cache) copyState() map[int]CacheItem {
games := make(map[int]CacheItem)
for id, game := range c.games {
games[id] = game.Clone()
}
return games
}
// internal (run): notifyGameUpdate notifies listeners about an updated game.
func (c *Cache) notifyGameUpdate(id int) {
if g, ok := c.games[id]; ok {
g2 := g.Clone()
c.GameUpdates.Notify(&CacheUpdate{ID: id, G: &g2})
} else {
// game deleted
c.GameUpdates.Notify(&CacheUpdate{ID: id, G: nil})
}
}
// run starts the cache main loop.
func (c *Cache) run() {
updateGame := func(game *LeagueGame) {
g, ok := c.games[game.ID]
if !ok {
g = CacheItem{Addrs: make(map[string]CacheItemAddr)}
}
g.Game = *game
c.games[game.ID] = g
}
for {
select {
case req := <-c.updateRequestChan:
switch req.reqType {
case reqUpdateAll:
games := req.payload.([]LeagueGame)
seen := make(map[int]bool)
for _, game := range games {
updateGame(&game)
seen[game.ID] = true
c.notifyGameUpdate(game.ID)
}
// delete games that weren't updated
for id := range c.games {
if !seen[id] {
delete(c.games, id)
c.notifyGameUpdate(id)
}
}
case reqUpdateSingle:
game := req.payload.(LeagueGame)
updateGame(&game)
c.notifyGameUpdate(game.ID)
case reqUpdateAddrs:
// drop request for unknown games
if game, ok := c.games[req.id]; ok {
addrs := req.payload.([]net.Addr)
for _, addr := range addrs {
if !shouldSkipAddr(addr) {
if _, ok := game.Addrs[cacheAddrKey(addr)]; !ok {
// item is not in cache, check it now
game.Addrs[cacheAddrKey(addr)] = CacheItemAddr{Addr: addr, Status: ConnectStatusPending}
go c.check(cacheCheckMsg{id: req.id, addr: addr})
}
}
}
}
case reqDelete:
delete(c.games, req.id)
c.notifyGameUpdate(req.id)
}
case res := <-c.checkResultChan:
if game, ok := c.games[res.id]; ok {
key := cacheAddrKey(res.addr)
a := game.Addrs[key]
a.Status = res.status
game.Addrs[key] = a
c.notifyGameUpdate(res.id)
}
case resChan := <-c.requestGamesChan:
resChan <- c.copyState()
}
}
}
type cacheCheckMsg struct {
id int // game id
addr net.Addr // address to check
status ConnectStatus // reply: status
}
// check tries to connect to the given address. Should be run from a goroutine.
func (c *Cache) check(req cacheCheckMsg) {
req.status = ConnectStatusFailure
if tryConnect(req.addr) {
req.status = ConnectStatusSuccess
}
c.checkResultChan <- req
}
type cacheReqType int
const (
reqUpdateAll cacheReqType = iota
reqUpdateSingle
reqUpdateAddrs
reqDelete
)
type cacheReq struct {
reqType cacheReqType
id int
payload interface{}
}
// CacheItem is a game with associated addresses.
type CacheItem struct {
Game LeagueGame // includes ID
Addrs map[string]CacheItemAddr // indexed by cacheAddrKey
}
// Clone creates a deep copy of the cache item.
func (g *CacheItem) Clone() CacheItem {
g2 := *g
g2.Addrs = make(map[string]CacheItemAddr)
for key, addr := range g.Addrs {
g2.Addrs[key] = addr
}
return g2
}
func cacheAddrKey(a net.Addr) string {
return fmt.Sprintf("%s:%s", a.Network(), a.String())
}
// CacheItemAddr is a single address that has been checked.
type CacheItemAddr struct {
Addr net.Addr
Status ConnectStatus
}
// CacheUpdate is the broadcasted via Cache.GameUpdates
type CacheUpdate struct {
ID int
G *CacheItem // might be nil for deleted games
}