2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-30 00:33:57 +00:00

introduce RWLock for caching obj.Stat()

This commit is contained in:
trbs 2017-03-14 23:05:51 +01:00
parent b523eef294
commit f1ba45723c

View File

@ -7,6 +7,7 @@ import (
"path" "path"
"restic" "restic"
"strings" "strings"
"sync"
"restic/backend" "restic/backend"
"restic/errors" "restic/errors"
@ -24,8 +25,8 @@ type s3 struct {
connChan chan struct{} connChan chan struct{}
bucketname string bucketname string
prefix string prefix string
cacheObjName string cacheMutex sync.RWMutex
cacheSize int64 cacheObjSize map[string]int64
} }
// Open opens the S3 backend at bucket and region. The bucket is created if it // Open opens the S3 backend at bucket and region. The bucket is created if it
@ -38,7 +39,12 @@ func Open(cfg Config) (restic.Backend, error) {
return nil, errors.Wrap(err, "minio.New") return nil, errors.Wrap(err, "minio.New")
} }
be := &s3{client: client, bucketname: cfg.Bucket, prefix: cfg.Prefix} be := &s3{
client: client,
bucketname: cfg.Bucket,
prefix: cfg.Prefix,
cacheObjSize: make(map[string]int64),
}
tr := &http.Transport{MaxIdleConnsPerHost: connLimit} tr := &http.Transport{MaxIdleConnsPerHost: connLimit}
client.SetCustomTransport(tr) client.SetCustomTransport(tr)
@ -189,17 +195,20 @@ func (be *s3) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, er
}() }()
// otherwise use a buffer with ReadAt // otherwise use a buffer with ReadAt
if be.cacheObjName == objName { be.cacheMutex.RLock()
size = be.cacheSize size, cacheHit := be.cacheObjSize[objName]
} else { be.cacheMutex.RUnlock()
if !cacheHit {
info, err := obj.Stat() info, err := obj.Stat()
if err != nil { if err != nil {
_ = obj.Close() _ = obj.Close()
return nil, errors.Wrap(err, "obj.Stat") return nil, errors.Wrap(err, "obj.Stat")
} }
size = info.Size size = info.Size
be.cacheObjName = objName be.cacheMutex.Lock()
be.cacheSize = size be.cacheObjSize[objName] = size
be.cacheMutex.Unlock()
} }
if offset > size { if offset > size {