interrupt.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package interrupt
  14. import (
  15. "os"
  16. "os/signal"
  17. "sync"
  18. "syscall"
  19. )
  20. // terminationSignals are signals that cause the program to exit in the
  21. // supported platforms (linux, darwin, windows).
  22. var terminationSignals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}
  23. // Handler guarantees execution of notifications after a critical section (the function passed
  24. // to a Run method), even in the presence of process termination. It guarantees exactly once
  25. // invocation of the provided notify functions.
  26. type Handler struct {
  27. notify []func()
  28. final func(os.Signal)
  29. once sync.Once
  30. }
  31. // Chain creates a new handler that invokes all notify functions when the critical section exits
  32. // and then invokes the optional handler's notifications. This allows critical sections to be
  33. // nested without losing exactly once invocations. Notify functions can invoke any cleanup needed
  34. // but should not exit (which is the responsibility of the parent handler).
  35. func Chain(handler *Handler, notify ...func()) *Handler {
  36. if handler == nil {
  37. return New(nil, notify...)
  38. }
  39. return New(handler.Signal, append(notify, handler.Close)...)
  40. }
  41. // New creates a new handler that guarantees all notify functions are run after the critical
  42. // section exits (or is interrupted by the OS), then invokes the final handler. If no final
  43. // handler is specified, the default final is `os.Exit(1)`. A handler can only be used for
  44. // one critical section.
  45. func New(final func(os.Signal), notify ...func()) *Handler {
  46. return &Handler{
  47. final: final,
  48. notify: notify,
  49. }
  50. }
  51. // Close executes all the notification handlers if they have not yet been executed.
  52. func (h *Handler) Close() {
  53. h.once.Do(func() {
  54. for _, fn := range h.notify {
  55. fn()
  56. }
  57. })
  58. }
  59. // Signal is called when an os.Signal is received, and guarantees that all notifications
  60. // are executed, then the final handler is executed. This function should only be called once
  61. // per Handler instance.
  62. func (h *Handler) Signal(s os.Signal) {
  63. h.once.Do(func() {
  64. for _, fn := range h.notify {
  65. fn()
  66. }
  67. if h.final == nil {
  68. os.Exit(1)
  69. }
  70. h.final(s)
  71. })
  72. }
  73. // Run ensures that any notifications are invoked after the provided fn exits (even if the
  74. // process is interrupted by an OS termination signal). Notifications are only invoked once
  75. // per Handler instance, so calling Run more than once will not behave as the user expects.
  76. func (h *Handler) Run(fn func() error) error {
  77. ch := make(chan os.Signal, 1)
  78. signal.Notify(ch, terminationSignals...)
  79. defer func() {
  80. signal.Stop(ch)
  81. close(ch)
  82. }()
  83. go func() {
  84. sig, ok := <-ch
  85. if !ok {
  86. return
  87. }
  88. h.Signal(sig)
  89. }()
  90. defer h.Close()
  91. return fn()
  92. }