-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gracefully shutdown reconcilers and catalogd FBC
Signed-off-by: Joe Lanford <[email protected]>
- Loading branch information
1 parent
9b08aea
commit 8c84738
Showing
7 changed files
with
92 additions
and
9 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package context | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type delayContext struct { | ||
valuesContext context.Context | ||
cancellationContext context.Context | ||
} | ||
|
||
func (d *delayContext) Deadline() (time.Time, bool) { | ||
return d.cancellationContext.Deadline() | ||
} | ||
|
||
func (d *delayContext) Done() <-chan struct{} { | ||
return d.cancellationContext.Done() | ||
} | ||
|
||
func (d *delayContext) Err() error { | ||
return d.cancellationContext.Err() | ||
} | ||
|
||
func (d *delayContext) Value(key interface{}) interface{} { | ||
return d.valuesContext.Value(key) | ||
} | ||
|
||
func WithDelay(ctx context.Context, delay time.Duration) (context.Context, context.CancelFunc) { | ||
delayedCtx, delayedCancel := context.WithCancel(context.Background()) | ||
done := make(chan struct{}) | ||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
// If there was no delay, cancel the delayed context immediately | ||
// after the parent context is canceled, then return | ||
if delay <= 0 { | ||
delayedCancel() | ||
return | ||
} | ||
|
||
// Otherwise, start a timer for our delayed cancellation | ||
timer := time.NewTimer(delay) | ||
select { | ||
case <-timer.C: | ||
// if the timer expires first, cancel the delayed context. | ||
// timer expiration here means that the function using this | ||
// context was still unable to complete even after the delay | ||
delayedCancel() | ||
case <-done: | ||
// if done is closed first, cleanup the timer to avoid a | ||
// resource leak. This case means that the function using the | ||
// context was able to complete within the allotted delay | ||
// duration. | ||
timer.Stop() | ||
} | ||
case <-done: | ||
// In this case, the original parent context was never cancelled | ||
// so we never even started our delayed context cancellation timer. | ||
// Just return to avoid leaking the goroutine. | ||
} | ||
}() | ||
|
||
cancelFunc := func() { | ||
close(done) // signal the goroutine to exit | ||
delayedCancel() // cancel the graceful context immediately without waiting for the timer to expire. | ||
} | ||
|
||
return &delayContext{ | ||
valuesContext: ctx, | ||
cancellationContext: delayedCtx, | ||
}, cancelFunc | ||
} |