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

main: signal related func better with context #2306

Closed
wants to merge 1 commit into from

Conversation

00arthur00
Copy link
Contributor

change signature of func interrupt and reload with context.

Signed-off-by: arthur yang <[email protected]>
Copy link
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.
However, I don't see any tangible reasons for this change. It doesn't improve anything. And there are controversial opinions on using context for just sake of cancellation.
https://dave.cheney.net/2017/08/20/context-isnt-for-cancellation

@00arthur00
Copy link
Contributor Author

However, I don't see any tangible reasons for this change. It doesn't improve anything. And there are controversial opinions on using context for just sake of cancellation.

@kakkoyun

Under the cmd/thanos, other go files use context.WithCancel to perform the cancel semantic(the main file as well). The purpose of the PR is to keep same style of such semantic.

If you doesn't agree, I'm glad to close the PR.

@kakkoyun
Copy link
Member

Hey @00arthur00, I have checked the usages of context patterns under thanos/cmd, I could only find one which is used solely for cancellation.

thanos/cmd/thanos/rule.go

Lines 493 to 519 in 601d4cf

ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
// Initialize rules.
if err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics); err != nil {
level.Error(logger).Log("msg", "initialize rules failed", "err", err)
return err
}
for {
select {
case <-reloadSignal:
if err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics); err != nil {
level.Error(logger).Log("msg", "reload rules by sighup failed", "err", err)
}
case reloadMsg := <-reloadWebhandler:
err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics)
if err != nil {
level.Error(logger).Log("msg", "reload rules by webhandler failed", "err", err)
}
reloadMsg <- err
case <-ctx.Done():
return ctx.Err()
}
}
}, func(error) {
cancel()
})
}

All the others are passed along to the other APIs that requires context.Context.

Maybe we should fix that exceptional case, to be consistent.
So I'm closing this, for now, thank you for your contribution anyway!
Maybe you would want to fix that case 🤗

@kakkoyun kakkoyun closed this Mar 30, 2020
@00arthur00
Copy link
Contributor Author

Hey @00arthur00, I have checked the usages of context patterns under thanos/cmd, I could only find one which is used solely for cancellation.

thanos/cmd/thanos/rule.go

Lines 493 to 519 in 601d4cf

ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
// Initialize rules.
if err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics); err != nil {
level.Error(logger).Log("msg", "initialize rules failed", "err", err)
return err
}
for {
select {
case <-reloadSignal:
if err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics); err != nil {
level.Error(logger).Log("msg", "reload rules by sighup failed", "err", err)
}
case reloadMsg := <-reloadWebhandler:
err := reloadRules(logger, ruleFiles, ruleMgr, evalInterval, metrics)
if err != nil {
level.Error(logger).Log("msg", "reload rules by webhandler failed", "err", err)
}
reloadMsg <- err
case <-ctx.Done():
return ctx.Err()
}
}
}, func(error) {
cancel()
})
}

All the others are passed along to the other APIs that requires context.Context.
Maybe we should fix that exceptional case, to be consistent.
So I'm closing this, for now, thank you for your contribution anyway!
Maybe you would want to fix that case

@kakkoyun Please see the comment.
The orignal one is close(ch), but bwplotka comment to use context.WithCancel.

Please see:
#1848 (comment)

@bwplotka
Copy link
Member

bwplotka commented Mar 30, 2020 via email

@kakkoyun
Copy link
Member

I also don't have strong opinions on it as well. I find channels simpler.
I'm happy to reopen the PR if you all think context is way to go for the project.

@00arthur00
Copy link
Contributor Author

I also don't have strong opinions on it as well. I find channels simpler.
I'm happy to reopen the PR if you all think context is way to go for the project.

@kakkoyun
I agree with bwplotka. Though not a big deal. Please help to reopen. I think we'd better keep consistent.

@00arthur00
Copy link
Contributor Author

There's no button for me to reopen the PR.

@kakkoyun kakkoyun reopened this Apr 1, 2020
@squat
Copy link
Member

squat commented Apr 1, 2020

doesnt matter too much one way or the other. I like to use chans if the use-case is simple enough and allows for it, e.g. here. Context is nice for when we have to propagate and build more context that can be cancelled by a parent.

@bwplotka
Copy link
Member

bwplotka commented Apr 1, 2020

Let's keep it simple then.

Thanks @00arthur00 anyway for this! I think building context for propagation cases, sounds like a good rule.

@bwplotka bwplotka closed this Apr 1, 2020
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.

4 participants