GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cache/singleflight.go at main · ubgo/cache Expand file tree/ Copy pathsingleflight.goMore file actions 92 lines (84 loc) · 3.75 KB / Copy pathsingleflight.goFile metadata and controls92 lines (84 loc) · 3.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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 // singleflight.go — per-cache call deduplication backing Remember (package cache, github.com/ubgo/cache). // // Package role: cache is the root bytes-level cache contract of the // ubgo/cache family; see doc.go for the package overview. // // This file: a zero-dependency reimplementation of the singleflight idea // scoped to what Remember needs — flightGroup.Do runs fn once for a key // while concurrent callers block and share the result; flightReg maps a // Cache instance to its group. The WHY: dedup is per-Cache (not global) // because two independent stores may legitimately load the same logical key // at once. Concurrency invariant: g.mu guards the map, but fn runs OUTSIDE // the lock so a slow loader never blocks unrelated keys; followers read // val/err only after the leader's wg.Done() (written-before-read, no race). // // AI-context: flightReg grows one entry per distinct Cache ever passed and // is intentionally never pruned — bounded in practice (apps create few // caches). Used only via remember.go's loaderFlight/flightRefresh. package cache import "sync" // flightReg maps a Cache instance to its single-flight group so concurrent // Remember calls for the same key on the same cache load exactly once. Keyed // by interface identity; adapters are pointer-backed so this is stable. // // Why keyed by Cache (not a global): two independent caches may legitimately // load the same logical key concurrently — they are different stores, so // deduping across them would be wrong. Per-instance groups keep dedup scoped // to one backing store. The map grows by one entry per distinct Cache value // ever passed to Remember and is never pruned; this is intentional and bounded // in practice because applications create a small, fixed set of caches. var ( flightRegMu sync.Mutex flightReg map[Cache]*flightGroup ) // flightGroup deduplicates concurrent calls for the same key: only the first // caller runs fn, the rest block and share its result. Zero-dependency // reimplementation of the golang.org/x/sync/singleflight idea, scoped to what // Remember needs. type flightGroup struct { mu sync.Mutex m map[string]*flightCall } type flightCall struct { wg sync.WaitGroup val any err error } // Do runs fn for key, or shares an in-flight call's result. The bool reports // whether the result was shared (true) rather than computed by this caller. // // Concurrency contract: the map is guarded by g.mu, but fn runs OUTSIDE the // lock so a slow loader never blocks unrelated keys. Followers find the // in-flight *flightCall under the lock, release it, then block on the // WaitGroup — they read c.val/c.err only after the leader's wg.Done(), so the // fields are written-before-read with no data race. The leader deletes the // entry after completing, so a fresh call for the same key after this one // returns starts a new flight (no result caching here — that is Remember's // job via the cache itself). func (g *flightGroup) Do(key string, fn func() (any, error)) (v any, shared bool, err error) { g.mu.Lock() if g.m == nil { g.m = make(map[string]*flightCall) } if c, ok := g.m[key]; ok { // A leader is already running fn for this key. Drop the lock and wait // for it; share its result instead of running fn a second time. g.mu.Unlock() c.wg.Wait() return c.val, true, c.err } // We are the leader for this key. Register before unlocking so any // follower that arrives while fn runs finds us. c := &flightCall{} c.wg.Add(1) g.m[key] = c g.mu.Unlock() c.val, c.err = fn() c.wg.Done() // releases every follower currently blocked in Wait() // Deregister so the next call for this key starts fresh. Done under the // lock to stay consistent with the registration path above. g.mu.Lock() delete(g.m, key) g.mu.Unlock() return c.val, false, c.err } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |