From 9c2b7d305954a6042e272b7ef15c07b0d5510572 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 26 Nov 2020 14:50:54 +0100 Subject: [PATCH] Add timeout and retrying to opening bbolt dbs --- database/storage/bbolt/bbolt.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/database/storage/bbolt/bbolt.go b/database/storage/bbolt/bbolt.go index 5291e0c..214605c 100644 --- a/database/storage/bbolt/bbolt.go +++ b/database/storage/bbolt/bbolt.go @@ -32,8 +32,18 @@ func init() { // NewBBolt opens/creates a bbolt database. func NewBBolt(name, location string) (storage.Interface, error) { + // Create options for bbolt database. + dbFile := filepath.Join(location, "db.bbolt") + dbOptions := &bbolt.Options{ + Timeout: 1 * time.Second, + } - db, err := bbolt.Open(filepath.Join(location, "db.bbolt"), 0600, nil) + // Open/Create database, retry if there is a timeout. + db, err := bbolt.Open(dbFile, 0600, dbOptions) + for i := 0; i < 5 && err != nil; i++ { + // Try again if there is an error. + db, err = bbolt.Open(dbFile, 0600, dbOptions) + } if err != nil { return nil, err }