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

Clarify is_fresh() with regard to concurrent insert_with() #217

Closed
niklasf opened this issue Feb 2, 2023 · 3 comments · Fixed by #218
Closed

Clarify is_fresh() with regard to concurrent insert_with() #217

niklasf opened this issue Feb 2, 2023 · 3 comments · Fixed by #218
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested
Milestone

Comments

@niklasf
Copy link

niklasf commented Feb 2, 2023

Hi, great work on the upcoming entry API!

I am wondering about the interaction of is_fresh() and the coalescing of concurrent calls in insert_with():

This method guarantees that concurrent calls on the same not-existing entry are coalesced into one evaluation of the init future. Only one of the calls evaluates its future, and other calls wait for that future to resolve.

That is, will one of the concurrent calls return an Entry that is_fresh(), will all be considered fresh, or is there no such guarantee?

@tatsuya6502 tatsuya6502 self-assigned this Feb 3, 2023
@tatsuya6502 tatsuya6502 added the question Further information is requested label Feb 3, 2023
@tatsuya6502 tatsuya6502 added the documentation Improvements or additions to documentation label Feb 3, 2023
@tatsuya6502 tatsuya6502 added this to the v0.10.0 milestone Feb 3, 2023
@tatsuya6502
Copy link
Member

Hi. Thank you for trying the new feature and asking the question.

Yes, there is a guarantee; is_fresh() must return true only when the init future of the or_insert_with call is resolved.

For example, when two concurrent or_insert_with calls (say call-A and call-B) on the same non-existing key were coalesced into one and call-A's init future was resolved, is_fresh of returned entries should return the following values:

  • is_fresh of the entry returned from call-A should return true.
  • is_fresh of the entry returned from call-B should return false.

This behavior was already implemented in the master branch via PR #193, but I realized test cases were missing. So I opened a new PR #218 to update the test cases to verify the behavior and also update the documentation to make the guarantee a bit more clear.

This method guarantees that concurrent calls on the same not-existing entry are coalesced into one evaluation of the init future. Only one of the calls evaluates its future (thus returned entry's is_fresh method returns true), and other calls wait for that future to resolve (and their is_fresh return false).

@niklasf
Copy link
Author

niklasf commented Feb 4, 2023

Thanks!

@tatsuya6502
Copy link
Member

@niklasf — FYI, I just published Moka v0.10.0 with the entry API to crates.io.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants