feat: add RetrieveCb for inspecting and cloning values under read lock by AdamMagued · Pull Request #165 · orcaman/concurrent-map · GitHub
Skip to content

feat: add RetrieveCb for inspecting and cloning values under read lock - #165

Open
AdamMagued wants to merge 1 commit into
orcaman:masterfrom
AdamMagued:add-retrieve-callback
Open

AdamMagued wants to merge 1 commit into
orcaman:masterfrom
AdamMagued:add-retrieve-callback

Conversation

@AdamMagued

Copy link
Copy Markdown

Add RetrieveCb method to ConcurrentMap to allow inspecting or deep copying values (especially pointer and reference types) safely while holding the shard's read lock (RLock).

Motivation

When storing pointer values in ConcurrentMap, retrieving a value via Get() returns the raw pointer. If another goroutine concurrently modifies the contents of that pointer or performs in-place mutation, data races can occur. RetrieveCb provides a callback executed while holding RLock on the shard, matching the pattern of Upsert and RemoveCb:

m.RetrieveCb("key", func(key string, val *MyStruct, exists bool) {
    if exists {
        // safely read or clone under RLock
    }
})
  • Added RetrieveCb[K any, V any] type and method on ConcurrentMap.
  • Added unit tests in concurrent_map_test.go.
  • All tests pass 100%.

Fixes #161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add callback when retrieving value especially for pointer value

1 participant