diff --git a/accounting/snapshot.go b/accounting/snapshot.go index 73b9a34..4ba4e89 100644 --- a/accounting/snapshot.go +++ b/accounting/snapshot.go @@ -34,10 +34,13 @@ func (t *snapshotTask) Stop() { t.mx.Lock() if t.process { snapshotList := worker.takeSnapshot() - defer func() { - t.unload(snapshotList) - t.wg.Wait() - }() + if len(snapshotList) != 0 { + defer func() { + t.wg.Add(1) + t.unload(snapshotList) + t.wg.Wait() + }() + } t.process = false t.close <- true } @@ -53,14 +56,16 @@ func (t *snapshotTask) run(timeout time.Duration) { return case <-t.timeout: list := worker.takeSnapshot() - go t.unload(list) + if len(list) > 0 { + t.wg.Add(1) + go t.unload(list) + } t.timeout = time.After(timeout) } } } func (t *snapshotTask) unload(list []entity.Snapshot) { - t.wg.Add(1) if err := model.SnapshotRep.Update(list); err != nil { log.Error(log_code.ErrorSnapshotAccounting, err) } diff --git a/accounting/storing.go b/accounting/storing.go index 881cf02..1beb3eb 100644 --- a/accounting/storing.go +++ b/accounting/storing.go @@ -63,6 +63,7 @@ func (t *storingTask) Stop() { if t.counter != 0 { buffer := t.clearBuffer() defer func() { + t.wg.Add(1) t.unload(buffer) t.wg.Wait() }() @@ -81,12 +82,14 @@ func (t *storingTask) run(timeout time.Duration) { case <-t.chanClose: return case cache := <-t.chanCounter: + t.wg.Add(1) go t.unload(cache) t.chanTimeout = time.After(timeout) case <-t.chanTimeout: t.mx.Lock() if t.counter != 0 { buffer := t.clearBuffer() + t.wg.Add(1) go t.unload(buffer) } t.mx.Unlock() @@ -96,7 +99,6 @@ func (t *storingTask) run(timeout time.Duration) { } func (t *storingTask) unload(cache []entity.Request) { - t.wg.Add(1) if err := model.RequestsRep.Insert(cache); err != nil { log.Error(log_code.ErrorUnloadAccounting, err) }