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

cache: don't let RowByModel() recursively lock the RowCache #270

Merged
merged 1 commit into from
Dec 16, 2021
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
18 changes: 12 additions & 6 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,22 @@ type RowCache struct {
mutex sync.RWMutex
}

// Row returns one model from the cache by UUID
func (r *RowCache) Row(uuid string) model.Model {
r.mutex.RLock()
defer r.mutex.RUnlock()
// rowByUUID returns one model from the cache by UUID. Caller must hold the row
// cache lock.
func (r *RowCache) rowByUUID(uuid string) model.Model {
if row, ok := r.cache[uuid]; ok {
return model.Clone(row)
}
return nil
}

// Row returns one model from the cache by UUID
func (r *RowCache) Row(uuid string) model.Model {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.rowByUUID(uuid)
}

// RowByModel searches the cache using a the indexes for a provided model
func (r *RowCache) RowByModel(m model.Model) model.Model {
r.mutex.RLock()
Expand All @@ -119,15 +125,15 @@ func (r *RowCache) RowByModel(m model.Model) model.Model {
return nil
}
if uuid.(string) != "" {
return r.Row(uuid.(string))
return r.rowByUUID(uuid.(string))
}
for index := range r.indexes {
val, err := valueFromIndex(info, index)
if err != nil {
continue
}
if uuid, ok := r.indexes[index][val]; ok {
return r.Row(uuid)
return r.rowByUUID(uuid)
}
}
return nil
Expand Down