mirror of
https://github.com/MetaCubeX/Clash.Meta.git
synced 2025-04-18 00:10:54 +00:00
25 lines
353 B
Go
25 lines
353 B
Go
package util
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
func NewDeadlineWatcher(ddl time.Duration, timeOut func()) (done func()) {
|
|
t := time.NewTimer(ddl)
|
|
closeCh := make(chan struct{})
|
|
go func() {
|
|
defer t.Stop()
|
|
select {
|
|
case <-closeCh:
|
|
case <-t.C:
|
|
timeOut()
|
|
}
|
|
}()
|
|
var once sync.Once
|
|
return func() {
|
|
once.Do(func() {
|
|
close(closeCh)
|
|
})
|
|
}
|
|
}
|