Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rare nil dereferences in lru (ele.Value.(*cacheEntry)) #529

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *lru) Flight(key, cmd string, ttl time.Duration, now time.Time) (v Redis

c.mu.RLock()
if kc, ok = c.store[key]; ok {
if ele, ok = kc.cache[cmd]; ok {
if ele = kc.cache[cmd]; ele != nil {
e = ele.Value.(*cacheEntry)
v = e.val
back = c.list.Back()
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *lru) Flight(key, cmd string, ttl time.Duration, now time.Time) (v Redis
kc = &keyCache{cache: make(map[string]*list.Element, 1), key: key}
c.store[key] = kc
}
if ele, ok = kc.cache[cmd]; ok {
if ele = kc.cache[cmd]; ele != nil {
if e = ele.Value.(*cacheEntry); e.val.typ == 0 || e.val.relativePTTL(now) > 0 {
atomic.AddUint32(&kc.hits, 1)
v = e.val
Expand All @@ -123,8 +123,7 @@ func (c *lru) Flight(key, cmd string, ttl time.Duration, now time.Time) (v Redis
}
atomic.AddUint32(&kc.miss, 1)
v.setExpireAt(now.Add(ttl).UnixMilli())
c.list.PushBack(&cacheEntry{cmd: cmd, kc: kc, val: v, ch: make(chan struct{})})
kc.cache[cmd] = c.list.Back()
kc.cache[cmd] = c.list.PushBack(&cacheEntry{cmd: cmd, kc: kc, val: v, ch: make(chan struct{})})
ret:
c.mu.Unlock()
return v, ce
Expand All @@ -137,7 +136,7 @@ func (c *lru) Flights(now time.Time, multi []CacheableTTL, results []RedisResult
for i, ct := range multi {
key, cmd := cmds.CacheKey(ct.Cmd)
if kc, ok := c.store[key]; ok {
if ele, ok := kc.cache[cmd]; ok {
if ele := kc.cache[cmd]; ele != nil {
e := ele.Value.(*cacheEntry)
v := e.val
if v.typ == 0 {
Expand Down Expand Up @@ -191,7 +190,7 @@ func (c *lru) Flights(now time.Time, multi []CacheableTTL, results []RedisResult
kc = &keyCache{cache: make(map[string]*list.Element, 1), key: key}
c.store[key] = kc
}
if ele, ok := kc.cache[cmd]; ok {
if ele := kc.cache[cmd]; ele != nil {
e := ele.Value.(*cacheEntry)
v := e.val
if v.typ == 0 {
Expand All @@ -211,8 +210,7 @@ func (c *lru) Flights(now time.Time, multi []CacheableTTL, results []RedisResult
atomic.AddUint32(&kc.miss, 1)
v := RedisMessage{}
v.setExpireAt(now.Add(multi[i].TTL).UnixMilli())
c.list.PushBack(&cacheEntry{cmd: cmd, kc: kc, val: v, ch: make(chan struct{})})
kc.cache[cmd] = c.list.Back()
kc.cache[cmd] = c.list.PushBack(&cacheEntry{cmd: cmd, kc: kc, val: v, ch: make(chan struct{})})
missed[j] = i
j++
}
Expand All @@ -224,7 +222,7 @@ func (c *lru) Update(key, cmd string, value RedisMessage) (pxat int64) {
var ch chan struct{}
c.mu.Lock()
if kc, ok := c.store[key]; ok {
if ele, ok := kc.cache[cmd]; ok {
if ele := kc.cache[cmd]; ele != nil {
if e := ele.Value.(*cacheEntry); e.val.typ == 0 {
pxat = value.getExpireAt()
cpttl := e.val.getExpireAt()
Expand Down Expand Up @@ -264,7 +262,7 @@ func (c *lru) Cancel(key, cmd string, err error) {
var ch chan struct{}
c.mu.Lock()
if kc, ok := c.store[key]; ok {
if ele, ok := kc.cache[cmd]; ok {
if ele := kc.cache[cmd]; ele != nil {
if e := ele.Value.(*cacheEntry); e.val.typ == 0 {
e.err = err
ch = e.ch
Expand Down Expand Up @@ -296,13 +294,17 @@ func (c *lru) GetTTL(key, cmd string) (ttl time.Duration) {
func (c *lru) purge(key string, kc *keyCache) {
if kc != nil {
for cmd, ele := range kc.cache {
if e := ele.Value.(*cacheEntry); e.val.typ != 0 { // do not delete pending entries
if delete(kc.cache, cmd); len(kc.cache) == 0 {
delete(c.store, key)
if ele != nil {
e := ele.Value.(*cacheEntry)
if e.val.typ == 0 { // do not delete pending entries
continue
}
c.list.Remove(ele)
c.size -= e.size
}
if delete(kc.cache, cmd); len(kc.cache) == 0 {
delete(c.store, key)
}
}
}
}
Expand All @@ -325,9 +327,11 @@ func (c *lru) Close(err error) {
c.mu.Lock()
for _, kc := range c.store {
for _, ele := range kc.cache {
if e := ele.Value.(*cacheEntry); e.val.typ == 0 {
e.err = err
close(e.ch)
if ele != nil {
if e := ele.Value.(*cacheEntry); e.val.typ == 0 {
e.err = err
close(e.ch)
}
}
}
}
Expand Down
Loading