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 a helper method to set eviction function after construction #318

Conversation

dims
Copy link
Member

@dims dims commented Nov 2, 2024

We have a use case in kubernetes/kubernetes#128507 where we set OnEvicted later.

https://github.com/dims/utils/pull/new/add-a-helper-method-to-set-eviction-function-after-construction

What type of PR is this?

/kind feature

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Release note:

Add a new `SetEvictionFunc` in lru's Cache

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 2, 2024
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 2, 2024
@dims
Copy link
Member Author

dims commented Nov 2, 2024

/assign @aojea @liggitt

@@ -44,6 +44,11 @@ func NewWithEvictionFunc(size int, f EvictionFunc) *Cache {
return c
}

// SetEvictionFunc updates the eviction func
func (c *Cache) SetEvictionFunc(f EvictionFunc) {
c.cache.OnEvicted = f
Copy link
Member

Choose a reason for hiding this comment

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

should be thread safe and lock before setting the value?

same for NewWithEvictionFunc?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point! added it here. But we don't need it in NewWithEvictionFunc as we do not return the internal Cache we create for the user to use until the method exits, so setting c.cache.OnEvicted = f without the locks are fine.

Copy link
Member

Choose a reason for hiding this comment

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

good point! added it here

was that added in a follow-up? not seeing it here

Copy link
Member Author

Choose a reason for hiding this comment

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

@liggitt would you recommend both the lock/unlock in addition to the "return error if it is already set"? i can cut another PR if so

Copy link
Member

Choose a reason for hiding this comment

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

@liggitt would you recommend both the lock/unlock in addition to the "return error if it is already set"?

yeah... going from nil to set still requires a lock since the function can be read/used concurrently from other methods

Copy link
Member

Choose a reason for hiding this comment

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

(and disallowing resetting it once set still sgtm)

Copy link
Member Author

Choose a reason for hiding this comment

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

Follow up is here #319

@aojea
Copy link
Member

aojea commented Nov 3, 2024

It seems correct, but it will be nice if you can just add an unit test, looking at this https://github.com/kubernetes/utils/pull/275/files it seems you can just copy paste that but instead of using the constructor you use this new helper

Signed-off-by: Davanum Srinivas <davanum@gmail.com>
@dims dims force-pushed the add-a-helper-method-to-set-eviction-function-after-construction branch from 8c2389e to 7edc6f1 Compare November 3, 2024 21:59
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 3, 2024
@dims
Copy link
Member Author

dims commented Nov 3, 2024

It seems correct, but it will be nice if you can just add an unit test, looking at this https://github.com/kubernetes/utils/pull/275/files it seems you can just copy paste that but instead of using the constructor you use this new helper

indeed! i tightened the api a bit, by allowing the set function only once as well. added tests.

@aojea
Copy link
Member

aojea commented Nov 4, 2024

/lgtm
/approve

Thanks

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 4, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, dims

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

lru/lru.go Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot merged commit 3ea5e8c into kubernetes:master Nov 4, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants