fix(nrfcache): stop stalling cache hits behind an in-flight NRF query - #171
Merged
gab-arrobo merged 4 commits intoAug 7, 2026
Merged
Conversation
handleLookup held the cache write lock across the NRF round trip. A sync.RWMutex queues new readers behind a waiting writer, so an in-flight discovery blocked every concurrent cache *hit* for that NF type until the network call returned. For an NF type that misses persistently the stall is continuous, and it serialises discovery core-wide: on SD-Core this capped registration at roughly one attach per discovery round trip. Serialise discovery on a dedicated discoveryMutex instead, and take the cache lock only to read the entry and to store the result. A burst of concurrent misses still collapses into a single NRF query, because the goroutine that wins discoveryMutex populates the cache and the rest find the entry on the re-check, so the existing single-callback behaviour is unchanged. Cache hits no longer wait on the network. Signed-off-by: Ben Grewell <bgrewell@gmail.com>
gab-arrobo
requested changes
Aug 6, 2026
Co-authored-by: Gabriel Arrobo <gabriel.arrobo@intel.com> Signed-off-by: Ben Grewell <BGrewell@gmail.com>
Co-authored-by: Gabriel Arrobo <gabriel.arrobo@intel.com> Signed-off-by: Ben Grewell <BGrewell@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (2)
nrfcache/nrfcache.go:187
Close()/purge()clear the cache and closec.done, buthandleLookupwill still write newly discovered instances intoc.cacheafter the NRF round-trip completes. With the discovery now running outsidec.mutex, this can happen after shutdown has begun/finished. Consider checkingc.donewhile holding the write lock and bailing out (or skipping cache population) if the cache is closed.
c.mutex.Lock()
for i := range searchResult.NfInstances {
c.set(&searchResult.NfInstances[i], ttl)
}
c.mutex.Unlock()
nrfcache/nrfcache.go:166
- After moving the NRF round-trip outside
c.mutex,handleLookupcan now continue issuing an NRF query even ifClose()/purge()has already closedc.done. That means work can proceed on a cache that is being torn down, and it can also change the semantics of shutdown (Close can return while a discovery is still in-flight). Consider short-circuiting oncec.doneis closed before starting the discovery flow.
This issue also appears on line 183 of the same file.
// discoveryMutex serializes NRF round-trips without blocking concurrent cache hits.
c.discoveryMutex.Lock()
defer c.discoveryMutex.Unlock()
// Re-check in case another goroutine already populated the entry.
c.mutex.RLock()
nfInstances = c.get(param)
c.mutex.RUnlock()
Contributor
|
@bgrewell, |
gab-arrobo
approved these changes
Aug 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

handleLookuptakes the cache's exclusive write lock and holds it across the NRF network round trip.sync.RWMutexqueues new readers behind a waiting writer, so one in-flight discovery blocks every concurrent cache hit for that NF type until the network call returns — the cache stops being a cache exactly when it is under load.Observed on a live Aether SD-Core deployment under registration load:
The discovery round trip now runs outside the cache's own lock. A dedicated
discoveryMutexpreserves the existing single-NRF-callback behaviour thatTestCacheConcurrencypins — the point was never to allow N concurrent identical NRF queries, only to stop that serialization from blocking unrelated readers.Scoping honestly: with the matcher fixes in #170 making misses rare, this one's steady-state effect is within run-to-run noise. It matters on cold start, on TTL expiry, and whenever a miss storm coincides with load — which is when the system can least afford to serialize.
go test ./nrfcache/passes, includingTestCacheConcurrency. Independent of #170; applies cleanly either way.