mirror of
https://github.com/xxjwxc/public.git
synced 2025-09-26 20:01:19 +08:00
add notify
This commit is contained in:
53
mynotify/mynotify.go
Normal file
53
mynotify/mynotify.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package mynotify
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type notify struct {
|
||||
tag int
|
||||
cc chan int // -2:超时退出 -1:已关闭 0:默认状态 1:正在运行
|
||||
tm time.Duration
|
||||
}
|
||||
|
||||
// New new signal
|
||||
func New(tm time.Duration) *notify {
|
||||
if tm == 0 { // 默认一个超时时间 一小时超时
|
||||
tm = 1 * time.Hour
|
||||
}
|
||||
return ¬ify{
|
||||
cc: make(chan int, 1),
|
||||
tm: tm,
|
||||
}
|
||||
}
|
||||
|
||||
// Signal 发送一个信号
|
||||
func (s *notify) Signal() {
|
||||
s.cc <- 1
|
||||
}
|
||||
|
||||
// 等等一个信息
|
||||
func (s *notify) Wait() bool {
|
||||
timeout := time.After(s.tm)
|
||||
select {
|
||||
case s.tag = <-s.cc:
|
||||
case <-timeout:
|
||||
s.tag = -2
|
||||
}
|
||||
|
||||
if s.tag == 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Stop 发送停止信号
|
||||
func (s *notify) Stop() {
|
||||
s.cc <- -1
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (s *notify) Close() {
|
||||
close(s.cc)
|
||||
}
|
Reference in New Issue
Block a user