blob: 418632b7dec38524d4e153d673e39c9adf565fbf [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001package desc
2
3import (
4 "sync"
5
6 "google.golang.org/protobuf/reflect/protoreflect"
7)
8
9type descriptorCache interface {
10 get(protoreflect.Descriptor) Descriptor
11 put(protoreflect.Descriptor, Descriptor)
12}
13
14type lockingCache struct {
15 cacheMu sync.RWMutex
16 cache mapCache
17}
18
19func (c *lockingCache) get(d protoreflect.Descriptor) Descriptor {
20 c.cacheMu.RLock()
21 defer c.cacheMu.RUnlock()
22 return c.cache.get(d)
23}
24
25func (c *lockingCache) put(key protoreflect.Descriptor, val Descriptor) {
26 c.cacheMu.Lock()
27 defer c.cacheMu.Unlock()
28 c.cache.put(key, val)
29}
30
31func (c *lockingCache) withLock(fn func(descriptorCache)) {
32 c.cacheMu.Lock()
33 defer c.cacheMu.Unlock()
34 // Pass the underlying mapCache. We don't want fn to use
35 // c.get or c.put sine we already have the lock. So those
36 // methods would try to re-acquire and then deadlock!
37 fn(c.cache)
38}
39
40type mapCache map[protoreflect.Descriptor]Descriptor
41
42func (c mapCache) get(d protoreflect.Descriptor) Descriptor {
43 return c[d]
44}
45
46func (c mapCache) put(key protoreflect.Descriptor, val Descriptor) {
47 c[key] = val
48}