Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve s3: add --metadata-max-objects to limit the metadata kept in memory

Nick Craig-Wood77ed639805ed
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index dbb30265f..a7310c5b5 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -6,6 +6,7 @@ import (
"encoding/hex"
"io"
"maps"
+ "math"
"os"
"path"
"strings"
@@ -14,6 +15,7 @@ import (
"time"
"github.com/google/uuid"
+ lru "github.com/hashicorp/golang-lru/v2"
"github.com/ncw/swift/v2"
"github.com/rclone/gofakes3"
"github.com/rclone/rclone/fs"
@@ -40,7 +42,7 @@ const putObjectPrefix = tempObjectPrefix + "put_"
// PutStream, instead of being buffered in memory by gofakes3.
type s3Backend struct {
s *Server
- meta *sync.Map
+ meta *lru.Cache[string, map[string]string] // object metadata by path
// multipartUploads tracks in-flight streaming multipart uploads,
// keyed by gofakes3.UploadID.
@@ -64,11 +66,25 @@ type s3Backend struct {
reaperStop sync.Once
}
+// newMetadataStore makes a store for the metadata of up to maxObjects
+// objects, forgetting that of the least recently used after that. If
+// maxObjects <= 0 there is no limit.
+func newMetadataStore(maxObjects int) *lru.Cache[string, map[string]string] {
+ if maxObjects <= 0 {
+ maxObjects = math.MaxInt
+ }
+ meta, err := lru.New[string, map[string]string](maxObjects)
+ if err != nil {
+ panic(err) // only fails if maxObjects <= 0
+ }
+ return meta
+}
+
// newBackend creates a new SimpleBucketBackend.
func newBackend(s *Server) *s3Backend {
return &s3Backend{
s: s,
- meta: new(sync.Map),
+ meta: newMetadataStore(s.opt.MetadataMaxObjects),
budget: newBufferBudget(int64(s.opt.MultipartStreamingBufferTotal)),
reaperQuit: make(chan struct{}),
}
@@ -187,8 +203,7 @@ func (b *s3Backend) HeadObject(ctx context.Context, bucketName, objectName strin
"Content-Type": mimeType,
}
- if val, ok := b.meta.Load(fp); ok {
- metaMap := val.(map[string]string)
+ if metaMap, ok := b.meta.Get(fp); ok {
maps.Copy(meta, metaMap)
}
@@ -268,8 +283,7 @@ func (b *s3Backend) GetObject(ctx context.Context, bucketName, objectName string
"Content-Type": mimeType,
}
- if val, ok := b.meta.Load(fp); ok {
- metaMap := val.(map[string]string)
+ if metaMap, ok := b.meta.Get(fp); ok {
maps.Copy(meta, metaMap)
}
@@ -288,7 +302,7 @@ func (b *s3Backend) GetObject(ctx context.Context, bucketName, objectName string
func (b *s3Backend) storeModtime(fp string, meta map[string]string, val string) {
meta["X-Amz-Meta-Mtime"] = val
meta["mtime"] = val
- b.meta.Store(fp, meta)
+ b.meta.Add(fp, meta)
}
// TouchObject creates or updates meta on specified object.
@@ -314,7 +328,7 @@ func (b *s3Backend) TouchObject(ctx context.Context, fp string, meta map[string]
return result, err
}
- b.meta.Store(fp, meta)
+ b.meta.Add(fp, meta)
if val, ok := meta["X-Amz-Meta-Mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
@@ -435,7 +449,7 @@ func (b *s3Backend) PutObject(
return result, err
}
- b.meta.Store(fp, meta)
+ b.meta.Add(fp, meta)
if val, ok := meta["X-Amz-Meta-Mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
@@ -503,7 +517,7 @@ func (b *s3Backend) deleteObject(ctx context.Context, bucketName, objectName str
if err := _vfs.Remove(fp); err != nil && !os.IsNotExist(err) {
return err
}
- b.meta.Delete(fp)
+ b.meta.Remove(fp)
// FIXME: unsafe operation
rmdirRecursive(fp, _vfs)
@@ -578,7 +592,7 @@ func (b *s3Backend) CopyObject(ctx context.Context, srcBucket, srcKey, dstBucket
return result, gofakes3.KeyNotFound(srcKey)
}
if srcBucket == dstBucket && srcKey == dstKey {
- b.meta.Store(fp, meta)
+ b.meta.Add(fp, meta)
val, ok := meta["X-Amz-Meta-Mtime"]
if !ok {
diff --git a/cmd/serve/s3/multipart.go b/cmd/serve/s3/multipart.go
index 3dac0c3a7..47f863e67 100644
--- a/cmd/serve/s3/multipart.go
+++ b/cmd/serve/s3/multipart.go
@@ -868,7 +868,7 @@ func (b *s3Backend) CompleteMultipartUpload(ctx context.Context, bucketName, obj
}
b.deleteUpload(uploadID)
- b.meta.Store(up.fp, up.meta)
+ b.meta.Add(up.fp, up.meta)
if val, ok := up.meta["X-Amz-Meta-Mtime"]; ok {
if ti, err := swift.FloatStringToTime(val); err == nil {
b.storeModtime(up.fp, up.meta, val)
diff --git a/cmd/serve/s3/put_test.go b/cmd/serve/s3/put_test.go
index 6dbddfa57..604647782 100644
--- a/cmd/serve/s3/put_test.go
+++ b/cmd/serve/s3/put_test.go
@@ -254,3 +254,24 @@ func TestDeleteObjectForgetsMetadata(t *testing.T) {
require.NoError(t, err)
assert.NotContains(t, obj.Metadata, "X-Amz-Meta-Colour", "metadata of the deleted object reappeared")
}
+
+// TestMetadataMaxObjects checks that the metadata of no more than
+// --metadata-max-objects objects is kept, forgetting that of the least
+// recently used.
+func TestMetadataMaxObjects(t *testing.T) {
+ b, _, bucket := newPutTestBackend(t, "", nil)
+ b.meta = newMetadataStore(2)
+ ctx := context.Background()
+
+ for _, key := range []string{"a", "b", "c"} {
+ meta := map[string]string{"X-Amz-Meta-Key": key}
+ _, err := b.PutObject(ctx, bucket, key, meta, bytes.NewReader([]byte(key)), 1)
+ require.NoError(t, err)
+ }
+
+ for key, want := range map[string]string{"a": "", "b": "b", "c": "c"} {
+ obj, err := b.HeadObject(ctx, bucket, key)
+ require.NoError(t, err)
+ assert.Equal(t, want, obj.Metadata["X-Amz-Meta-Key"], key)
+ }
+}
diff --git a/cmd/serve/s3/s3.go b/cmd/serve/s3/s3.go
index b8586cd31..02dd77dfd 100644
--- a/cmd/serve/s3/s3.go
+++ b/cmd/serve/s3/s3.go
@@ -53,6 +53,10 @@ var OptionsInfo = fs.Options{{
Name: "multipart_max_uploads",
Default: 1000,
Help: "Maximum number of streamed multipart uploads in progress at once, 0 for unlimited",
+}, {
+ Name: "metadata_max_objects",
+ Default: 100000,
+ Help: "Maximum number of objects whose metadata is kept in memory, 0 for unlimited",
}, {
Name: "multipart_expiry",
Default: fs.Duration(24 * time.Hour),
@@ -72,6 +76,7 @@ type Options struct {
MultipartStreamingBufferLimit fs.SizeSuffix `config:"multipart_streaming_buffer_limit"`
MultipartStreamingBufferTotal fs.SizeSuffix `config:"multipart_streaming_buffer_total"`
MultipartMaxUploads int `config:"multipart_max_uploads"`
+ MetadataMaxObjects int `config:"metadata_max_objects"`
MultipartExpiry fs.Duration `config:"multipart_expiry"`
Auth httplib.AuthConfig
HTTP httplib.Config
diff --git a/cmd/serve/s3/serve_s3.md b/cmd/serve/s3/serve_s3.md
index 3ab888ed9..5d6298413 100644
--- a/cmd/serve/s3/serve_s3.md
+++ b/cmd/serve/s3/serve_s3.md
@@ -378,7 +378,10 @@ can take some time.
Versioning is not currently supported.
Metadata will only be saved in memory other than the rclone `mtime`
-metadata which will be set as the modification time of the file.
+metadata which will be set as the modification time of the file. The
+metadata of at most `--metadata-max-objects` objects (default `100000`,
+`0` for no limit) is kept; after that the metadata of the least
+recently used object is forgotten.
### Object names
diff --git a/go.mod b/go.mod
index 2d99c8f9c..560743e4e 100644
--- a/go.mod
+++ b/go.mod
@@ -47,6 +47,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/uuid v1.6.0
github.com/hanwen/go-fuse/v2 v2.10.1
+ github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/internxt/rclone-adapter v0.0.0-20260904121407-404a75f3d031
github.com/jcmturner/gokrb5/v8 v8.4.4
github.com/jlaffaye/ftp v0.2.1-0.20251026020404-6602e981a1bb
@@ -191,7 +192,6 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
- github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect