An R2 user on X: the sync bucket grew by 18 GB in under a month. The
backup and usage files are small and rewritten in place; what grew was
the request archive, every call written whole (bodies up to 32 MiB) as
archive/<date>/<id>.json and never removed. The archive on this computer
is capped at a GiB; the one in the bucket had no cap.
Now the bucket's archive is held to RequestArchiveMB (a GiB unless set,
carried by sync), down to a tenth under it past the cap, oldest first:
- only keys of ArchiveName's own shape under <prefix>/magpie/archive/
are counted or removed; the backup, usage, other prefixes and anything
else in the bucket are never touched (a test seeds seven such keys);
- uploads are counted as they go, and the bucket is listed again only
after a tenth of the cap more is uploaded: one LIST for ~1000 calls,
never one per call;
- the serving magpie lists once two minutes after it starts, archive on
or off, so a bucket grown before this update is brought under the cap
without the user doing anything (auto-cleanup chosen over a button:
the objects are magpie's own, already capped locally, and a removal
is logged);
- the Usage page's switch tooltip says the size ("…in <bucket>, up to
1.1 GB"), in every language; the core script budget is kept, not
raised: the two tooltips share one options object and the switch's
aria-checked is set from a boolean.
The Nov 3 rise in Class B reads has no code cause found: archive reads
are click-driven only, and the backup's conditional GET runs every 3
minutes per computer online.
Red on the old logic (bucketAdded and KeepBucketArchiveBounded made
no-ops by hand, on mica): TestRequestArchiveKeptUnderItsSize "after 200
calls the archive holds 248 calls, 7629310 bytes, over its 1048576 (48
from 2020 left); 0 LISTs, 0 DELETEs"; TestBucketArchiveBroughtUnderIts
SizeAtStart keeps 30 calls / 3000 bytes; TestBucketArchiveListedPer
TenthUploaded "0 LISTs". With the fix: "200 calls: 44 kept, 986389
bytes; 4461157 bytes uploaded, 39 LISTs, 203 DELETEs".
Run at this tree (rebased on c6a67dd41):
- mote: gofmt -l internal clean; go test -timeout 45m -count=1
./internal/davsync ./internal/gateway ./internal/settings
./internal/settingsapi ./internal/cli: all ok (gateway 897s).
- mica: go test ./internal/gui ok (TestCoreScriptBudget included);
GOOS=windows go vet of the six touched packages ok; GOOS=linux go vet
of davsync, gateway, settings, settingsapi ok (gui and cli need
WebKitGTK's cgo, not vetted for Linux).
- mica GUI, Chromium and WebKit, every language: request-archive,
request-archive-usage, i18n-keys, i18n-en-no-cjk 22/22 at this tree;
with call-headers, context-text, routing-bodies, routing-side-calls,
usage-ledger-search and web-page 114/114 before the tooltip's last
wording change.
Not tried: a real R2 or S3 bucket (fake S3 only), and the reporter's
own bucket contents.
160 lines
4.4 KiB
Go
160 lines
4.4 KiB
Go
package davsync
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// Bucket is the S3 sync's bucket for files other than the backup — the
|
|
// gateway's request archive — each at <prefix>/magpie/<name>, beside the
|
|
// backup, written as it is: no passphrase seals them, so they can be read
|
|
// in the bucket too.
|
|
type Bucket struct{ s *s3 }
|
|
|
|
// ErrNoObject is a name with nothing at it.
|
|
var ErrNoObject = errors.New("not in the bucket")
|
|
|
|
// S3Bucket is the bucket sync keeps the backup in; false when sync is off
|
|
// or to a WebDAV folder.
|
|
func S3Bucket() (*Bucket, bool) {
|
|
c, ok := Load()
|
|
if !ok || !c.S3() {
|
|
return nil, false
|
|
}
|
|
s, err := newS3(c)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return &Bucket{s}, true
|
|
}
|
|
|
|
// at is the client for name rather than the backup.
|
|
func (b *Bucket) at(name string) *s3 {
|
|
s := *b.s
|
|
s.key = strings.TrimSuffix(s.key, file) + strings.TrimLeft(name, "/")
|
|
return &s
|
|
}
|
|
|
|
// Where names the bucket, its prefix and its server, as messages do.
|
|
func (b *Bucket) Where() string { return b.at("").where() }
|
|
|
|
// Put writes data at name, over what is there.
|
|
func (b *Bucket) Put(ctx context.Context, name string, data []byte) error {
|
|
s := b.at(name)
|
|
res, err := s.send(ctx, http.MethodPut, data, map[string]string{"Content-Type": "application/json"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode >= 200 && res.StatusCode < 300 {
|
|
io.Copy(io.Discard, io.LimitReader(res.Body, 64<<10))
|
|
return nil
|
|
}
|
|
return s.explain("write", readError(res))
|
|
}
|
|
|
|
// Get reads what is at name, its first 16 MiB; ErrNoObject when there is
|
|
// nothing.
|
|
func (b *Bucket) Get(ctx context.Context, name string) ([]byte, error) {
|
|
body, _, err := b.Open(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer body.Close()
|
|
return io.ReadAll(io.LimitReader(body, 16<<20))
|
|
}
|
|
|
|
// Open is what is at name as it is read, however large, and its size (-1
|
|
// when the server doesn't say); ErrNoObject when there is nothing. The
|
|
// caller closes it.
|
|
func (b *Bucket) Open(ctx context.Context, name string) (io.ReadCloser, int64, error) {
|
|
s := b.at(name)
|
|
res, err := s.send(ctx, http.MethodGet, nil, nil)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if res.StatusCode != http.StatusOK {
|
|
defer res.Body.Close()
|
|
e := readError(res)
|
|
if e.Status == http.StatusNotFound && e.Code != "NoSuchBucket" {
|
|
return nil, 0, ErrNoObject
|
|
}
|
|
return nil, 0, s.explain("read", e)
|
|
}
|
|
return res.Body, res.ContentLength, nil
|
|
}
|
|
|
|
// Each calls f with every object under prefix, a name as Put takes it
|
|
// ("archive/"), its name as Put takes it and its size, a page of a
|
|
// thousand at a time.
|
|
func (b *Bucket) Each(ctx context.Context, prefix string, f func(name string, size int64)) error {
|
|
s := b.at(prefix)
|
|
base := strings.TrimSuffix(b.s.key, file)
|
|
token := ""
|
|
seen := map[string]bool{}
|
|
for {
|
|
q := url.Values{"list-type": {"2"}, "prefix": {s.key}}
|
|
if token != "" {
|
|
q.Set("continuation-token", token)
|
|
}
|
|
u := s.bucketURL()
|
|
u.RawQuery = strings.ReplaceAll(q.Encode(), "+", "%20")
|
|
res, err := s.sendTo(ctx, http.MethodGet, u, nil, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if res.StatusCode != http.StatusOK {
|
|
e := readError(res)
|
|
res.Body.Close()
|
|
return s.explain("list", e)
|
|
}
|
|
var l struct {
|
|
Contents []struct {
|
|
Key string `xml:"Key"`
|
|
Size int64 `xml:"Size"`
|
|
} `xml:"Contents"`
|
|
Truncated bool `xml:"IsTruncated"`
|
|
Next string `xml:"NextContinuationToken"`
|
|
}
|
|
err = xml.NewDecoder(io.LimitReader(res.Body, 16<<20)).Decode(&l)
|
|
res.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, c := range l.Contents {
|
|
if name, ok := strings.CutPrefix(c.Key, base); ok && strings.HasPrefix(c.Key, s.key) {
|
|
f(name, c.Size)
|
|
}
|
|
}
|
|
if !l.Truncated {
|
|
return nil
|
|
}
|
|
if l.Next == "" || seen[l.Next] {
|
|
return fmt.Errorf("listing %s: incomplete listing (missing or repeated continuation token)", s.where())
|
|
}
|
|
seen[l.Next] = true
|
|
token = l.Next
|
|
}
|
|
}
|
|
|
|
// Delete removes what is at name; nothing there is no error.
|
|
func (b *Bucket) Delete(ctx context.Context, name string) error {
|
|
s := b.at(name)
|
|
res, err := s.send(ctx, http.MethodDelete, nil, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode >= 200 && res.StatusCode < 300 || res.StatusCode == http.StatusNotFound {
|
|
io.Copy(io.Discard, io.LimitReader(res.Body, 64<<10))
|
|
return nil
|
|
}
|
|
return s.explain("remove", readError(res))
|
|
}
|