Remove five second ticker and replace with sleep
This prevents the process from waking up every five seconds even when there are no requests
This commit is contained in:
parent
99994c6722
commit
0354436ab6
@ -34,29 +34,32 @@ func NewGList(chatID int, items ...string) *GList {
|
|||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
|
|
||||||
var reqs chan *GList
|
var PersistReqC chan<- *GList
|
||||||
|
|
||||||
func DoPersist() {
|
func startPersistenceGoR() {
|
||||||
reqs = make(chan *GList, 50)
|
reqs := make(chan *GList, 50)
|
||||||
|
PersistReqC = reqs
|
||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(5 * time.Second) {
|
|
||||||
lists := map[*GList]struct{}{}
|
lists := map[*GList]struct{}{}
|
||||||
loop:
|
for g := range reqs {
|
||||||
for {
|
lists[g] = struct{}{}
|
||||||
select {
|
time.Sleep(5 * time.Second)
|
||||||
case g := <-reqs:
|
for len(reqs) > 0 {
|
||||||
|
g := <-reqs
|
||||||
lists[g] = struct{}{}
|
lists[g] = struct{}{}
|
||||||
default:
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for g := range lists {
|
for g := range lists {
|
||||||
g.persist()
|
g.persist()
|
||||||
}
|
}
|
||||||
|
lists = make(map[*GList]struct{}, len(lists))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
startPersistenceGoR()
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GList) persist() {
|
func (g *GList) persist() {
|
||||||
g.Mutex.Lock()
|
g.Mutex.Lock()
|
||||||
defer g.Mutex.Unlock()
|
defer g.Mutex.Unlock()
|
||||||
@ -80,14 +83,14 @@ outer:
|
|||||||
}
|
}
|
||||||
g.Items = append(g.Items, Entry{text, false})
|
g.Items = append(g.Items, Entry{text, false})
|
||||||
}
|
}
|
||||||
reqs <- g
|
PersistReqC <- g
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GList) Toggle(text string) error {
|
func (g *GList) Toggle(text string) error {
|
||||||
for i, item := range g.Items {
|
for i, item := range g.Items {
|
||||||
if item.Text == text {
|
if item.Text == text {
|
||||||
g.Items[i].Checked = !g.Items[i].Checked
|
g.Items[i].Checked = !g.Items[i].Checked
|
||||||
reqs <- g
|
PersistReqC <- g
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +105,7 @@ func (g *GList) ClearChecked() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.Items = remaining
|
g.Items = remaining
|
||||||
reqs <- g
|
PersistReqC <- g
|
||||||
}
|
}
|
||||||
|
|
||||||
type button struct {
|
type button struct {
|
||||||
|
@ -23,7 +23,8 @@ func TestGList(t *testing.T) {
|
|||||||
|
|
||||||
func TestSplit(t *testing.T) {
|
func TestSplit(t *testing.T) {
|
||||||
g := GList{}
|
g := GList{}
|
||||||
reqs = make(chan *GList, 50)
|
//This resets the channel, so test does not try to persist
|
||||||
|
PersistReqC = make(chan *GList, 50)
|
||||||
g.Add("foo")
|
g.Add("foo")
|
||||||
g.Add("bar\nfoo\nblah")
|
g.Add("bar\nfoo\nblah")
|
||||||
g.Add("foo")
|
g.Add("foo")
|
||||||
|
Loading…
Reference in New Issue
Block a user