-
Notifications
You must be signed in to change notification settings - Fork 4
improve-election-failover-on-process-termination #26
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,9 @@ import ( | |
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "os/signal" | ||
| "strconv" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
@@ -188,11 +190,22 @@ func (el *AwaitElection) Run() error { | |
|
|
||
| go elector.Run(ctx) | ||
|
|
||
| // Handle OS signals: cancel the context so ReleaseOnCancel triggers lease | ||
| // release before exit. Without this, SIGTERM terminates the process before | ||
| // cancel() is ever called, forcing followers to wait LeaseDuration to take over. | ||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT) | ||
| defer signal.Stop(sigCh) | ||
|
|
||
| // the different end conditions: | ||
| // 1. context was cancelled -> error | ||
| // 2. command executed -> either error or nil, depending on return value | ||
| // 3. status endpoint failed -> could not create status endpoint | ||
| // 1. OS signal received -> cancel context, release lease, then exit | ||
| // 2. context was cancelled -> error | ||
| // 3. command executed -> either error or nil, depending on return value | ||
| // 4. status endpoint failed -> could not create status endpoint | ||
| select { | ||
| case sig := <-sigCh: | ||
| log.Infof("received signal %v, releasing leader lease before exit", sig) | ||
| return fmt.Errorf("received signal: %v", sig) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regardless of whether we switch to |
||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case r := <-execResult: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we use
signal.NotifyContextto implement this more simply?