diff options
author | Valery Piashchynski <[email protected]> | 2021-06-25 11:47:24 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-06-25 11:47:24 +0300 |
commit | 09041fe7499be1929aaffaa113f93f6853564c09 (patch) | |
tree | a608df4250439cf9a1511f9e00ff7af6a033579d /plugins/kv/drivers/boltdb/driver.go | |
parent | 64d41f6d87bb8002e700cdcddb5a3a241e4a2a7d (diff) | |
parent | e9249c7896331bab97a18a7ee0db17803fdd31fb (diff) |
Merge remote-tracking branch 'origin/master' into feature/jobs_plugin
Diffstat (limited to 'plugins/kv/drivers/boltdb/driver.go')
-rw-r--r-- | plugins/kv/drivers/boltdb/driver.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/plugins/kv/drivers/boltdb/driver.go b/plugins/kv/drivers/boltdb/driver.go index e5aac290..0f737fbd 100644 --- a/plugins/kv/drivers/boltdb/driver.go +++ b/plugins/kv/drivers/boltdb/driver.go @@ -18,6 +18,7 @@ import ( ) type Driver struct { + clearMu sync.RWMutex // db instance DB *bolt.DB // name should be UTF-8 @@ -373,6 +374,35 @@ func (d *Driver) TTL(keys ...string) (map[string]string, error) { return m, nil } +func (d *Driver) Clear() error { + err := d.DB.Update(func(tx *bolt.Tx) error { + err := tx.DeleteBucket(d.bucket) + if err != nil { + d.log.Error("boltdb delete bucket", "error", err) + return err + } + + _, err = tx.CreateBucket(d.bucket) + if err != nil { + d.log.Error("boltdb create bucket", "error", err) + return err + } + + return nil + }) + + if err != nil { + d.log.Error("clear transaction failed", "error", err) + return err + } + + d.clearMu.Lock() + d.gc = sync.Map{} + d.clearMu.Unlock() + + return nil +} + // ========================= PRIVATE ================================= func (d *Driver) startGCLoop() { //nolint:gocognit @@ -382,6 +412,8 @@ func (d *Driver) startGCLoop() { //nolint:gocognit for { select { case <-t.C: + d.clearMu.RLock() + // calculate current time before loop started to be fair now := time.Now() d.gc.Range(func(key, value interface{}) bool { @@ -414,6 +446,8 @@ func (d *Driver) startGCLoop() { //nolint:gocognit } return true }) + + d.clearMu.RUnlock() case <-d.stop: err := d.DB.Close() if err != nil { |