refactor(sdk): Improve StreamRegistry
cache invalidation
#2874
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.
Summary
Now there are two separate cache invalidations in
StreamRegistry
: one for metadata and another for permissions.Changes
Previously the
invalidateStreamCache()
invalidated both caches. Now there are separateinvalidatePermissionCaches()
andinvalidateMetadataCache()
methods.Also changed log level from
debug
totrace
.Open questions (maybe for a follow-up PR)
If there are multiple instances of
Stream
classes for samestreamId
, andsetMetadata()
is called for some of the instances, the metadata is not updated to other instances. It would be possible to makeStream
object stateless by removing the metadata field. That way the metadata info would always be only in one place: inStreamRegistry
, which already caches the info. But in that solution, theStream#getMetadata()
should be an async function.StreamRegistry
could emit e.g.metadataUpdated
event and Stream instances would update their internal metadata field. But that could cause garbage collector problems:StreamrClientEventEmitter
needs to store references to allStream
instances and therefore the garbage collector can't clean obsoleteStream
instances from memory.Future improvements
isStreamPublisher
andisStreamSubscriber
caches could be positive caches, i.e. cache the value only if result istrue
? We invalidate cache manually when we find out that an operation fails because of missing permission (seeMessageFactory#69
andmessagePipeline#60
). Those are the only places outside ofStreamRegistry
manual where cache invalidation is done. Therefore we could encapsulate the caching fully intoStreamRegistry
and also simplify cache key handing by doing this refactoring.invalidatePermissionCaches
should invalidate also thehasPublicSubscribePermission
cache? (TODO comment inStreamRegistry
)