lib/kv: add read-only flag in DB
This commit is contained in:
parent
612c717ea0
commit
359989a5f7
3 changed files with 35 additions and 0 deletions
|
@ -33,6 +33,7 @@ type DB struct {
|
|||
refs int
|
||||
bolt *bbolt.DB
|
||||
mu sync.Mutex
|
||||
readOnly bool
|
||||
canWrite bool
|
||||
queue chan *request
|
||||
lockTime time.Duration
|
||||
|
@ -138,6 +139,10 @@ func (db *DB) open(ctx context.Context, forWrite bool) (err error) {
|
|||
}
|
||||
_ = db.close()
|
||||
|
||||
if db.readOnly && forWrite {
|
||||
return ErrReadOnly
|
||||
}
|
||||
|
||||
db.canWrite = forWrite
|
||||
if !forWrite {
|
||||
// mitigate https://github.com/etcd-io/bbolt/issues/98
|
||||
|
@ -228,6 +233,11 @@ func (db *DB) Do(write bool, op Op) error {
|
|||
return r.err
|
||||
}
|
||||
|
||||
// ReadOnly setter the db.readOnly
|
||||
func (db *DB) ReadOnly(b bool) {
|
||||
db.readOnly = b
|
||||
}
|
||||
|
||||
// request encapsulates a synchronous operation and its results
|
||||
type request struct {
|
||||
op Op
|
||||
|
|
|
@ -66,3 +66,27 @@ func TestKvExit(t *testing.T) {
|
|||
Exit()
|
||||
assert.Equal(t, 0, len(dbMap))
|
||||
}
|
||||
|
||||
func TestDbReadOnly(t *testing.T) {
|
||||
require.Equal(t, 0, len(dbMap), "no databases can be started initially")
|
||||
ctx := context.Background()
|
||||
|
||||
db, err := Start(ctx, "test", nil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, db)
|
||||
|
||||
assert.False(t, db.readOnly)
|
||||
|
||||
// set db in read only
|
||||
db.ReadOnly(true)
|
||||
|
||||
assert.True(t, db.readOnly)
|
||||
|
||||
// write op is not allowed in read only mode, should throw error
|
||||
err = db.Do(true, nil)
|
||||
assert.ErrorIs(t, err, ErrReadOnly)
|
||||
|
||||
db.ReadOnly(false)
|
||||
err = db.Do(true, &opStop{})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ var (
|
|||
ErrEmpty = errors.New("database empty")
|
||||
ErrInactive = errors.New("database stopped")
|
||||
ErrUnsupported = errors.New("unsupported on this OS")
|
||||
ErrReadOnly = errors.New("database in read-only mode")
|
||||
)
|
||||
|
||||
// Op represents a database operation
|
||||
|
|
Loading…
Add table
Reference in a new issue