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

Cleanly shutdown cleanup go-routine #337

Merged
merged 3 commits into from
Oct 12, 2022

Conversation

alok87
Copy link
Contributor

@alok87 alok87 commented Oct 4, 2022

Cleanly shutdown go routine. Big cache was made when go did not have ctx support

Fixes #336

Cleanly shutdown go routine. Big cache was made when go did not have ctx support

Fixes allegro#336
@@ -41,11 +42,11 @@ const (
)

// NewBigCache initialize new instance of BigCache
func NewBigCache(config Config) (*BigCache, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should keep the old constructor for backward compatibility but mark it deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

name suggestion for the new function NewBigCacheWithContext()?

and what should we pass in the old function TODO() or Background() is fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

or just New() as the package name is bigcache

bigcache.New()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's go with WithContext suffix as this is how std lib added context https://godocs.io/net/http#NewRequestWithContext

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both background and todo returns the same empty context

var (
	background = new(emptyCtx)
	todo       = new(emptyCtx)
)

https://go.dev/src/context/context.go

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not about implementation, it's about readability 😉

Copy link
Contributor Author

@alok87 alok87 Oct 4, 2022

Choose a reason for hiding this comment

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

Saw the comment in WithContext now, went ahead with New(), looks more redable to me.

Std lib follows both, see this https://pkg.go.dev/errors#New , yes but for context addition they added WithContext

Let me know if it requires a change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, New is ok for me too. @janisz ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Me too

@@ -16,7 +17,7 @@ func BenchmarkWriteToCacheWith1Shard(b *testing.B) {

func BenchmarkWriteToLimitedCacheWithSmallInitSizeAnd1Shard(b *testing.B) {
m := blob('a', 1024)
cache, _ := NewBigCache(Config{
cache, _ := NewBigCache(context.TODO(), Config{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think in tests its fine to use context.Background()

@codecov-commenter
Copy link

codecov-commenter commented Oct 4, 2022

Codecov Report

Merging #337 (3a23231) into master (c9426ba) will decrease coverage by 0.85%.
The diff coverage is 42.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #337      +/-   ##
==========================================
- Coverage   90.17%   89.32%   -0.86%     
==========================================
  Files          15       15              
  Lines         784      787       +3     
==========================================
- Hits          707      703       -4     
- Misses         65       71       +6     
- Partials       12       13       +1     
Impacted Files Coverage Δ
server/server.go 30.30% <0.00%> (ø)
bigcache.go 94.44% <50.00%> (-2.31%) ⬇️
shard.go 90.49% <0.00%> (-1.53%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c9426ba...3a23231. Read the comment docs.

alok87 added a commit to alok87/bigcache that referenced this pull request Oct 4, 2022
@alok87 alok87 changed the title Shutdown cleanup go routine with context cancellation Cleanly shutdown cleanup go-routine Oct 4, 2022
alok87 added a commit to alok87/bigcache that referenced this pull request Oct 4, 2022
@alok87 alok87 force-pushed the ctx-support-cleanup-routine-fix-336 branch from b8dce9e to 0e0633c Compare October 4, 2022 14:40
bigcache.go Outdated Show resolved Hide resolved
alok87 added a commit to alok87/bigcache that referenced this pull request Oct 4, 2022
@alok87 alok87 force-pushed the ctx-support-cleanup-routine-fix-336 branch from 0e0633c to 830f26e Compare October 4, 2022 14:46
@alok87 alok87 force-pushed the ctx-support-cleanup-routine-fix-336 branch from 830f26e to 383a05c Compare October 4, 2022 14:47
@alok87
Copy link
Contributor Author

alok87 commented Oct 6, 2022

wanted to use the latest version? can we merge this? @cristaloleg @janisz

@janisz janisz enabled auto-merge (squash) October 12, 2022 13:12
@janisz janisz merged commit 8b4452b into allegro:master Oct 12, 2022
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.

Shutting down cleanup routine with ctx
4 participants