From 99ba8dd7d491699a6ae75e62f3480facea44dc0c Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Wed, 16 Sep 2020 13:09:04 +0100 Subject: [PATCH] store/file: don't keep connection to boltdb open (#2006) --- blob.go | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/blob.go b/blob.go index 9f2ee51..eb5c34c 100644 --- a/blob.go +++ b/blob.go @@ -9,7 +9,6 @@ import ( "time" "github.com/micro/go-micro/v3/store" - "github.com/pkg/errors" bolt "go.etcd.io/bbolt" ) @@ -18,19 +17,14 @@ func NewBlobStore() (store.BlobStore, error) { // ensure the parent directory exists os.MkdirAll(DefaultDir, 0700) - // open the connection to the database - dbPath := filepath.Join(DefaultDir, "micro.db") - db, err := bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second}) - if err != nil { - return nil, errors.Wrap(err, "Error connecting to database") - } - - // return the blob store - return &blobStore{db}, nil + return &blobStore{}, nil } -type blobStore struct { - db *bolt.DB +type blobStore struct{} + +func (b *blobStore) db() (*bolt.DB, error) { + dbPath := filepath.Join(DefaultDir, "blob.db") + return bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second}) } func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error) { @@ -48,6 +42,13 @@ func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error options.Namespace = "micro" } + // open a connection to the database + db, err := b.db() + if err != nil { + return nil, err + } + defer db.Close() + // execute the transaction var value []byte readValue := func(tx *bolt.Tx) error { @@ -65,7 +66,7 @@ func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error return nil } - if err := b.db.View(readValue); err != nil { + if err := db.View(readValue); err != nil { return nil, err } @@ -88,8 +89,15 @@ func (b *blobStore) Write(key string, blob io.Reader, opts ...store.BlobOption) options.Namespace = "micro" } + // open a connection to the database + db, err := b.db() + if err != nil { + return err + } + defer db.Close() + // execute the transaction - return b.db.Update(func(tx *bolt.Tx) error { + return db.Update(func(tx *bolt.Tx) error { // create the bucket bucket, err := tx.CreateBucketIfNotExists([]byte(options.Namespace)) if err != nil { @@ -121,8 +129,15 @@ func (b *blobStore) Delete(key string, opts ...store.BlobOption) error { options.Namespace = "micro" } + // open a connection to the database + db, err := b.db() + if err != nil { + return err + } + defer db.Close() + // execute the transaction - return b.db.Update(func(tx *bolt.Tx) error { + return db.Update(func(tx *bolt.Tx) error { // check for the namespaces bucket bucket := tx.Bucket([]byte(options.Namespace)) if bucket == nil {