Created
December 12, 2020 10:31
-
-
Save owulveryck/f5478b614c0ee02881b946ab4d74d2e8 to your computer and use it in GitHub Desktop.
Detect if the os has been in sleep mode by looking at the clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func hasSlept(ctx context.Context, healthCheck time.Duration, sleptTime time.Duration) <-chan struct{} { | |
signalC := make(chan struct{}) | |
go func(chan<- struct{}) { | |
tick := time.NewTicker(healthCheck) | |
last := time.Now() | |
for { | |
select { | |
case <-ctx.Done(): | |
return | |
case <-tick.C: | |
if time.Now().Round(0).Sub(last) > sleptTime { | |
signalC <- struct{}{} | |
} | |
last = time.Now() | |
} | |
} | |
}(signalC) | |
return signalC | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
time.Now().Round(0).Sub(last)
is the important line here:From the doc: