forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retryable_blobstore.go
74 lines (58 loc) · 1.87 KB
/
retryable_blobstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package blobstore
import (
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type retryableBlobstore struct {
blobstore DigestBlobstore
maxTries int
logTag string
logger boshlog.Logger
}
func NewRetryableBlobstore(blobstore DigestBlobstore, maxTries int, logger boshlog.Logger) DigestBlobstore {
return retryableBlobstore{
blobstore: blobstore,
maxTries: maxTries,
logTag: "retryableBlobstore",
logger: logger,
}
}
func (b retryableBlobstore) Get(blobID string, fingerprint boshcrypto.Digest) (string, error) {
var fileName string
var lastErr error
for i := 0; i < b.maxTries; i++ {
fileName, lastErr = b.blobstore.Get(blobID, fingerprint)
if lastErr == nil {
return fileName, nil
}
b.logger.Info(b.logTag,
"Failed to get blob with error '%s', attempt %d out of %d", lastErr.Error(), i, b.maxTries)
}
return "", bosherr.WrapError(lastErr, "Getting blob from inner blobstore")
}
func (b retryableBlobstore) CleanUp(fileName string) error {
return b.blobstore.CleanUp(fileName)
}
func (b retryableBlobstore) Delete(blobID string) error {
return b.blobstore.Delete(blobID)
}
func (b retryableBlobstore) Create(fileName string) (string, boshcrypto.MultipleDigest, error) {
var lastErr error
for i := 0; i < b.maxTries; i++ {
blobID, digest, thisErr := b.blobstore.Create(fileName)
if thisErr == nil {
return blobID, digest, nil
}
lastErr = thisErr
b.logger.Info(b.logTag,
"Failed to create blob with error %s, attempt %d out of %d", lastErr.Error(), i, b.maxTries)
}
return "", boshcrypto.MultipleDigest{}, bosherr.WrapError(lastErr, "Creating blob in inner blobstore")
}
func (b retryableBlobstore) Validate() error {
if b.maxTries < 1 {
return bosherr.Error("Max tries must be > 0")
}
return b.blobstore.Validate()
}