Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions storage/indexdb/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pebble
import (
"context"
"errors"
"sync"
"time"

"github.com/cockroachdb/pebble/v2"
Expand All @@ -22,6 +23,7 @@ type PebbleDB struct {
db *pebble.DB
writeMode *pebble.WriteOptions
skipErrRecord bool
closed sync.Once
}

func init() {
Expand Down Expand Up @@ -112,9 +114,13 @@ func (p *PebbleDB) Expired(ctx context.Context, f storage.IterateFunc) error {

// Close implements storage.IndexDB.
func (p *PebbleDB) Close() error {
// force flush data to disk
_ = p.db.Flush()
return p.db.Close()
var err error
p.closed.Do(func() {
_ = p.db.Flush()
// force flush data to disk
err = p.db.Close()
})
return err
Comment on lines 116 to +123
}

// GC implements storage.IndexDB.
Expand Down Expand Up @@ -178,5 +184,6 @@ func New(path string, option storage.Option) (storage.IndexDB, error) {
db: pdb,
writeMode: writeMode, // 是否异步写操作
skipErrRecord: true,
closed: sync.Once{},
}, nil
}
13 changes: 10 additions & 3 deletions storage/sharedkv/nonekv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"errors"
"sync"

"github.com/cockroachdb/pebble/v2"
"github.com/omalloc/tavern/api/defined/v1/storage"
Expand All @@ -12,11 +13,16 @@ import (
var _ storage.SharedKV = (*noneSharedKV)(nil)

type noneSharedKV struct {
db *pebble.DB
db *pebble.DB
closed sync.Once
}

func (r *noneSharedKV) Close() error {
return r.db.Close()
var err error
r.closed.Do(func() {
err = r.db.Close()
})
return err
Comment on lines 20 to +25
}

func (r *noneSharedKV) Get(_ context.Context, key []byte) ([]byte, error) {
Expand Down Expand Up @@ -189,7 +195,8 @@ func newNoneKV(storePath string, opts *pebble.Options) (storage.SharedKV, error)
}

r := &noneSharedKV{
db: db,
db: db,
closed: sync.Once{},
}
return r, nil
}
Loading