This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Sleep before context cancellation #37
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
654eb6f
Add CANCELLATION_DELAY_SECONDS to README
f4d0d3d
Add context cancellation delay seconds
0d099ae
Fix test to wait for 6 seconds
00e959d
Reflect comment
300068e
Fix test to run with delay=0
8c89aa0
Reflect comments
a364339
Inherit environment variables to child
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ require ( | |
github.com/spf13/viper v1.3.2 | ||
golang.org/x/net v0.0.0-20190921015927-1a5e07d1ff72 | ||
) | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,18 @@ import ( | |
"errors" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cybozu-go/log" | ||
) | ||
|
||
var ( | ||
errSignaled = errors.New("signaled") | ||
|
||
cancellationDelaySecondsEnv = "CANCELLATION_DELAY_SECONDS" | ||
|
||
defaultCancellationDelaySeconds = 5 | ||
) | ||
|
||
// IsSignaled returns true if err returned by Wait indicates that | ||
|
@@ -28,6 +34,23 @@ func handleSignal(env *Environment) { | |
log.Warn("well: got signal", map[string]interface{}{ | ||
"signal": s.String(), | ||
}) | ||
delayStr := os.Getenv(cancellationDelaySecondsEnv) | ||
delay, err := strconv.Atoi(delayStr) | ||
if err != nil { | ||
log.Warn("well: set default cancellation delay seconds", map[string]interface{}{ | ||
"env": delayStr, | ||
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. should log.FnError: err |
||
"delay": defaultCancellationDelaySeconds, | ||
}) | ||
delay = defaultCancellationDelaySeconds | ||
} | ||
if delay < 0 { | ||
log.Warn("well: round up negative cancellation delay seconds to 0s", map[string]interface{}{ | ||
"env": delayStr, | ||
"delay": 0, | ||
}) | ||
delay = 0 | ||
} | ||
time.Sleep(time.Duration(delay) * time.Second) | ||
env.Cancel(errSignaled) | ||
}() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should be done only when
if len(delayStr) > 0
.