mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-14 14:08:04 +08:00
26 lines
353 B
Go
26 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)
|
||
|
})
|
||
|
}
|
||
|
}
|