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

refactor(sdk): Improve StreamRegistry cache invalidation #2874

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

teogeb
Copy link
Contributor

@teogeb teogeb commented Nov 12, 2024

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 separate invalidatePermissionCaches() and invalidateMetadataCache() methods.

Also changed log level from debug to trace.

Open questions (maybe for a follow-up PR)

If there are multiple instances of Stream classes for same streamId, and setMetadata() is called for some of the instances, the metadata is not updated to other instances. It would be possible to make Stream object stateless by removing the metadata field. That way the metadata info would always be only in one place: in StreamRegistry, which already caches the info. But in that solution, the Stream#getMetadata() should be an async function.

  • Alternatively 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 all Stream instances and therefore the garbage collector can't clean obsolete Stream instances from memory.

Future improvements

  • Maybe isStreamPublisher and isStreamSubscriber caches could be positive caches, i.e. cache the value only if result is true? We invalidate cache manually when we find out that an operation fails because of missing permission (see MessageFactory#69 and messagePipeline#60). Those are the only places outside of StreamRegistry manual where cache invalidation is done. Therefore we could encapsulate the caching fully into StreamRegistry and also simplify cache key handing by doing this refactoring.
  • The invalidatePermissionCaches should invalidate also the hasPublicSubscribePermission cache? (TODO comment in StreamRegistry)

@github-actions github-actions bot added the sdk label Nov 12, 2024
@teogeb teogeb force-pushed the sdk-invalidatePermissionCaches branch from fe68650 to 9c24cec Compare November 12, 2024 20:47
@teogeb teogeb requested a review from harbu November 12, 2024 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant