load loads key either by invoking the getter locally or by sending it to another machine.
(ctx context.Context, key string, dest Sink)
| 239 | |
| 240 | // load loads key either by invoking the getter locally or by sending it to another machine. |
| 241 | func (g *Group) load(ctx context.Context, key string, dest Sink) (value ByteView, destPopulated bool, err error) { |
| 242 | g.Stats.Loads.Add(1) |
| 243 | viewi, err := g.loadGroup.Do(key, func() (interface{}, error) { |
| 244 | // Check the cache again because singleflight can only dedup calls |
| 245 | // that overlap concurrently. It's possible for 2 concurrent |
| 246 | // requests to miss the cache, resulting in 2 load() calls. An |
| 247 | // unfortunate goroutine scheduling would result in this callback |
| 248 | // being run twice, serially. If we don't check the cache again, |
| 249 | // cache.nbytes would be incremented below even though there will |
| 250 | // be only one entry for this key. |
| 251 | // |
| 252 | // Consider the following serialized event ordering for two |
| 253 | // goroutines in which this callback gets called twice for the |
| 254 | // same key: |
| 255 | // 1: Get("key") |
| 256 | // 2: Get("key") |
| 257 | // 1: lookupCache("key") |
| 258 | // 2: lookupCache("key") |
| 259 | // 1: load("key") |
| 260 | // 2: load("key") |
| 261 | // 1: loadGroup.Do("key", fn) |
| 262 | // 1: fn() |
| 263 | // 2: loadGroup.Do("key", fn) |
| 264 | // 2: fn() |
| 265 | if value, cacheHit := g.lookupCache(key); cacheHit { |
| 266 | g.Stats.CacheHits.Add(1) |
| 267 | return value, nil |
| 268 | } |
| 269 | g.Stats.LoadsDeduped.Add(1) |
| 270 | var value ByteView |
| 271 | var err error |
| 272 | if peer, ok := g.peers.PickPeer(key); ok { |
| 273 | value, err = g.getFromPeer(ctx, peer, key) |
| 274 | if err == nil { |
| 275 | g.Stats.PeerLoads.Add(1) |
| 276 | return value, nil |
| 277 | } |
| 278 | g.Stats.PeerErrors.Add(1) |
| 279 | // TODO(bradfitz): log the peer's error? keep |
| 280 | // log of the past few for /groupcachez? It's |
| 281 | // probably boring (normal task movement), so not |
| 282 | // worth logging I imagine. |
| 283 | } |
| 284 | value, err = g.getLocally(ctx, key, dest) |
| 285 | if err != nil { |
| 286 | g.Stats.LocalLoadErrs.Add(1) |
| 287 | return nil, err |
| 288 | } |
| 289 | g.Stats.LocalLoads.Add(1) |
| 290 | destPopulated = true // only one caller of load gets this return value |
| 291 | g.populateCache(key, value, &g.mainCache) |
| 292 | return value, nil |
| 293 | }) |
| 294 | if err == nil { |
| 295 | value = viewi.(ByteView) |
| 296 | } |
| 297 | return |
| 298 | } |
no test coverage detected