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

Add waited return var to Lock #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yonderblue
Copy link

In cases where you might have a GetThing() that concurrently does:

Check cache for k
If hit, return the val
If miss,
     do expensive work
     set in cache
     return the cached val

Using this pkg to put a Lock around the miss part can make sure we don't repeat expensive work from concurrent calls both seeing a miss. With the Lock though you'd want to check after the lock is entered if we are already cached since we could have just waited on a concurrent caller that set the cache.

So it would be:

Check cache for k
If hit, return the val
If miss,
     Lock(k)
     defer Lock(k)
     Check cache for k
        if hit, return the val
     do expensive work
     set in cache
     return the cached val

The added waited return var can allow a skipping of the extra check if waited=false.

@yonderblue
Copy link
Author

I should note that I see the argument that doing the second cache check should always be fast enough not to warrant the skip. So feel free to close :)

@im7mortal
Copy link
Owner

@yonderblue Thank you for PR

I saw immediately your PR but I am still analyzing it.

I can immediately say that it's not possible to change function signature as then it will break the standard Locker interface
But I will think if it worth to wrap in other way

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.

2 participants