micro/internal/config/autosave.go

45 lines
551 B
Go
Raw Normal View History

2019-08-04 21:22:24 +00:00
package config
import (
2019-08-04 21:32:42 +00:00
"sync"
2019-08-04 21:22:24 +00:00
"time"
)
var Autosave chan bool
2019-08-04 21:32:42 +00:00
var autotime int
// lock for autosave
var autolock sync.Mutex
2019-08-04 21:22:24 +00:00
func init() {
Autosave = make(chan bool)
}
2019-08-04 21:32:42 +00:00
func SetAutoTime(a int) {
autolock.Lock()
autotime = a
autolock.Unlock()
}
func GetAutoTime() int {
autolock.Lock()
a := autotime
autolock.Unlock()
return a
}
2019-08-04 21:22:24 +00:00
func StartAutoSave() {
go func() {
for {
2020-10-09 03:33:34 +00:00
autolock.Lock()
a := autotime
autolock.Unlock()
if a < 1 {
2019-08-04 21:32:42 +00:00
break
}
2020-10-09 03:33:34 +00:00
time.Sleep(time.Duration(a) * time.Second)
2019-08-04 21:22:24 +00:00
Autosave <- true
}
}()
}