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

feat: introduce a ttl cache for resources #482

Merged
merged 5 commits into from
May 29, 2024
Merged

feat: introduce a ttl cache for resources #482

merged 5 commits into from
May 29, 2024

Conversation

atzoum
Copy link
Collaborator

@atzoum atzoum commented May 24, 2024

Description

Introducing a new specialty cache for resources which need to be closed/cleaned-up after their expiration.

This new cache keeps track of the resources' usage and makes sure that expired resources are not cleaned up while they are still in use and cleaned up only when they are no longer needed.

Internally it uses a cachettl.Cache which has been extended with an OnEvicted callback.

Resources with any of following methods can be managed by the cache and cleaned up appropriately:

  • Cleanup()
  • Close()
  • Close() error
  • Stop()
  • Stop() error

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

cachettl/cachettl.go Outdated Show resolved Hide resolved
cachettl/cachettl.go Outdated Show resolved Hide resolved
resourcettl/cache.go Outdated Show resolved Hide resolved
r := c.resources[instanceID]
delete(c.resources, instanceID)
delete(c.checkouts, instanceID)
go c.cleanup(r)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the lack of synchronization create issues here? Wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using another go-routine so that the main go-routine is not blocked. At this point no one should be using the resource, thus doing async cleanup should be safe

resourcettl/cache.go Outdated Show resolved Hide resolved
@atzoum atzoum merged commit 2d5c6b2 into main May 29, 2024
10 checks passed
@atzoum atzoum deleted the feat.resourceCache branch May 29, 2024 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants