interrupt.go 3.8 KB

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