mirror of
https://github.com/status-im/status-go.git
synced 2025-01-12 07:35:02 +00:00
38308d48f2
* feat_: log error and stacktrace when panic in goroutine * test_: add test TestSafeGo * chore_: rename logAndCall to call * chore_: rename SafeGo to Go * chore_: make lint-fix * chore_: use t.Cleanup * chore_: Revert "chore_: use t.Cleanup" This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819. * chore_: Revert "chore_: make lint-fix" This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab. * chore_: Revert "chore_: rename SafeGo to Go" This reverts commit a6d73d6df583f313032d79aac62f66328039cb55. * chore_: Revert "chore_: rename logAndCall to call" This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc. * chore_: Revert "test_: add test TestSafeGo" This reverts commit a1fa91839f3960398980c6bf456e6462ec944819. * chore_: Revert "feat_: log error and stacktrace when panic in goroutine" This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a. * feat_: log error and stacktrace when panic in goroutine * chore_: make lint-fix * chore_: rename logAndCall to call * chore_: renaming LogOnPanic * chore_: update rest goroutine function calls * chore_: make lint-fix
88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package mailserver
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/status-im/status-go/common"
|
|
)
|
|
|
|
const (
|
|
dbCleanerBatchSize = 1000
|
|
dbCleanerPeriod = time.Hour
|
|
)
|
|
|
|
// dbCleaner removes old messages from a db.
|
|
type dbCleaner struct {
|
|
sync.RWMutex
|
|
|
|
db DB
|
|
batchSize int
|
|
retention time.Duration
|
|
|
|
period time.Duration
|
|
cancel chan struct{}
|
|
}
|
|
|
|
// newDBCleaner returns a new cleaner for db.
|
|
func newDBCleaner(db DB, retention time.Duration) *dbCleaner {
|
|
return &dbCleaner{
|
|
db: db,
|
|
retention: retention,
|
|
|
|
batchSize: dbCleanerBatchSize,
|
|
period: dbCleanerPeriod,
|
|
}
|
|
}
|
|
|
|
// Start starts a loop that cleans up old messages.
|
|
func (c *dbCleaner) Start() {
|
|
log.Info("Starting cleaning envelopes", "period", c.period, "retention", c.retention)
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
c.Lock()
|
|
c.cancel = cancel
|
|
c.Unlock()
|
|
|
|
go c.schedule(c.period, cancel)
|
|
}
|
|
|
|
// Stops stops the cleaning loop.
|
|
func (c *dbCleaner) Stop() {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
|
|
if c.cancel == nil {
|
|
return
|
|
}
|
|
close(c.cancel)
|
|
c.cancel = nil
|
|
}
|
|
|
|
func (c *dbCleaner) schedule(period time.Duration, cancel <-chan struct{}) {
|
|
defer common.LogOnPanic()
|
|
t := time.NewTicker(period)
|
|
defer t.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-t.C:
|
|
count, err := c.PruneEntriesOlderThan(time.Now().Add(-c.retention))
|
|
if err != nil {
|
|
log.Error("failed to prune data", "err", err)
|
|
}
|
|
log.Info("Prunned some some messages successfully", "count", count)
|
|
case <-cancel:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// PruneEntriesOlderThan removes messages sent between lower and upper timestamps
|
|
// and returns how many have been removed.
|
|
func (c *dbCleaner) PruneEntriesOlderThan(t time.Time) (int, error) {
|
|
return c.db.Prune(t, c.batchSize)
|
|
}
|