代码拉取完成,页面将自动刷新
package interrupthandler
import (
"os"
"os/signal"
"sync"
"syscall"
)
type InterruptHandler struct {
interruptCount int
lock *sync.Mutex
C chan bool
}
func NewInterruptHandler() *InterruptHandler {
h := &InterruptHandler{
lock: &sync.Mutex{},
C: make(chan bool, 0),
}
go h.handleInterrupt()
SwallowSigQuit()
return h
}
func (h *InterruptHandler) WasInterrupted() bool {
h.lock.Lock()
defer h.lock.Unlock()
return h.interruptCount > 0
}
func (h *InterruptHandler) handleInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
signal.Stop(c)
h.lock.Lock()
h.interruptCount++
if h.interruptCount == 1 {
close(h.C)
} else if h.interruptCount > 5 {
os.Exit(1)
}
h.lock.Unlock()
go h.handleInterrupt()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。