Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c7f663

Browse files
authored
store/file: don't keep connection to boltdb open (micro#2006)
1 parent 8fdc8f0 commit 3c7f663

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

store/file/blob.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/micro/go-micro/v3/store"
12-
"github.com/pkg/errors"
1312
bolt "go.etcd.io/bbolt"
1413
)
1514

@@ -18,19 +17,14 @@ func NewBlobStore() (store.BlobStore, error) {
1817
// ensure the parent directory exists
1918
os.MkdirAll(DefaultDir, 0700)
2019

21-
// open the connection to the database
22-
dbPath := filepath.Join(DefaultDir, "micro.db")
23-
db, err := bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
24-
if err != nil {
25-
return nil, errors.Wrap(err, "Error connecting to database")
26-
}
27-
28-
// return the blob store
29-
return &blobStore{db}, nil
20+
return &blobStore{}, nil
3021
}
3122

32-
type blobStore struct {
33-
db *bolt.DB
23+
type blobStore struct{}
24+
25+
func (b *blobStore) db() (*bolt.DB, error) {
26+
dbPath := filepath.Join(DefaultDir, "blob.db")
27+
return bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
3428
}
3529

3630
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
4842
options.Namespace = "micro"
4943
}
5044

45+
// open a connection to the database
46+
db, err := b.db()
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer db.Close()
51+
5152
// execute the transaction
5253
var value []byte
5354
readValue := func(tx *bolt.Tx) error {
@@ -65,7 +66,7 @@ func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error
6566

6667
return nil
6768
}
68-
if err := b.db.View(readValue); err != nil {
69+
if err := db.View(readValue); err != nil {
6970
return nil, err
7071
}
7172

@@ -88,8 +89,15 @@ func (b *blobStore) Write(key string, blob io.Reader, opts ...store.BlobOption)
8889
options.Namespace = "micro"
8990
}
9091

92+
// open a connection to the database
93+
db, err := b.db()
94+
if err != nil {
95+
return err
96+
}
97+
defer db.Close()
98+
9199
// execute the transaction
92-
return b.db.Update(func(tx *bolt.Tx) error {
100+
return db.Update(func(tx *bolt.Tx) error {
93101
// create the bucket
94102
bucket, err := tx.CreateBucketIfNotExists([]byte(options.Namespace))
95103
if err != nil {
@@ -121,8 +129,15 @@ func (b *blobStore) Delete(key string, opts ...store.BlobOption) error {
121129
options.Namespace = "micro"
122130
}
123131

132+
// open a connection to the database
133+
db, err := b.db()
134+
if err != nil {
135+
return err
136+
}
137+
defer db.Close()
138+
124139
// execute the transaction
125-
return b.db.Update(func(tx *bolt.Tx) error {
140+
return db.Update(func(tx *bolt.Tx) error {
126141
// check for the namespaces bucket
127142
bucket := tx.Bucket([]byte(options.Namespace))
128143
if bucket == nil {

0 commit comments

Comments
 (0)