First scratch commit: a full token map refuses, never forgets
Fable's final review of 58fd476, a Low. At 4,096 live tokens remember()
dropped the new one after making its page, so every later autosave with
that token made another page. Now a new token past the cap is refused
with 503 {refused: "busy"} before anything is made; the editor keeps the
text and retries, as it does offline.
Gate: TestAFullTokenMapRefusesANewPage, which answered 200 and made a
page on the old code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+58
−4
internal/mapa/firstscratch.go+13 −4internal/mapa/firstscratch_test.go+45 −0
diff --git a/internal/mapa/firstscratch.go b/internal/mapa/firstscratch.go| index 27339a9..2706642 100644 |
| --- a/internal/mapa/firstscratch.go |
| +++ b/internal/mapa/firstscratch.go |
| @@ -25,7 +25,9 @@ import ( |
| // firstHold is how long a token is remembered. |
| const firstHold = 10 * time.Minute |
| -// firstCap bounds how many tokens are remembered at once. |
| +// firstCap bounds how many tokens are remembered at once. Past it a new |
| +// token is refused, never forgotten: a token the server could not |
| +// remember would make a page on every autosave (review L). |
| const firstCap = 4096 |
| var firstToken = regexp.MustCompile(`^[A-Za-z0-9_-]{16,64}$`) |
| @@ -58,11 +60,13 @@ func (held *firsts) recall(key string, now time.Time) (firstMade, bool) { |
| } |
| func (held *firsts) remember(key string, made firstMade) { |
| - if len(held.made) < firstCap { |
| - held.made[key] = made |
| - } |
| + held.made[key] = made |
| } |
| +// full is whether no new token can be remembered. The caller holds the |
| +// mutex, after recall has forgotten the expired ones. |
| +func (held *firsts) full() bool { return len(held.made) >= firstCap } |
| + |
| // firstScratch is POST /scratch with a "first" token: the new page's |
| // first autosave. It answers JSON: {href, page, revision} when it made |
| // the page; when the page was already made, this is an autosave of a |
| @@ -83,6 +87,11 @@ func (app *app) firstScratch(writer http.ResponseWriter, |
| app.firstDraft(writer, request, viewer, made) |
| return |
| } |
| + if app.firsts.full() { |
| + // Nothing is made; the editor keeps the text and tries again. |
| + refuseJSON(writer, http.StatusServiceUnavailable, "busy") |
| + return |
| + } |
| page, err := app.store.CreateScratch(request.Context(), viewer.Address, |
| request.PostFormValue("body")) |
| if errors.Is(err, ErrEmpty) { |
diff --git a/internal/mapa/firstscratch_test.go b/internal/mapa/firstscratch_test.go| new file mode 100644 |
| index 0000000..85a1462 |
| --- /dev/null |
| +++ b/internal/mapa/firstscratch_test.go |
| @@ -0,0 +1,45 @@ |
| +package mapa |
| + |
| +import ( |
| + "io" |
| + "log/slog" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "strconv" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// The first scratch commit's token map fails closed (review L): when it |
| +// holds firstCap live tokens, a new token is refused and makes nothing, |
| +// where it once made a page and forgot it, so every later autosave with |
| +// that token made another. |
| +func TestAFullTokenMapRefusesANewPage(t *testing.T) { |
| + store := testStore(t) |
| + built := &app{store: store, allowUnauthenticated: true, |
| + testIdentity: "ana@example.com", |
| + logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| + built.firsts.made = map[string]firstMade{} |
| + for index := range firstCap { |
| + built.firsts.made["held"+strconv.Itoa(index)] = firstMade{ |
| + at: store.now()} |
| + } |
| + for range 2 { |
| + form := url.Values{"body": {"Hello."}} |
| + request := httptest.NewRequest(http.MethodPost, "/scratch", |
| + strings.NewReader(form.Encode())) |
| + request.Header.Set("Content-Type", |
| + "application/x-www-form-urlencoded") |
| + recorder := httptest.NewRecorder() |
| + built.firstScratch(recorder, request, "TOKENTOKENTOKEN0001") |
| + if recorder.Code != http.StatusServiceUnavailable { |
| + t.Fatalf("a full token map answered %d", recorder.Code) |
| + } |
| + } |
| + var pages int64 |
| + if err := store.database.Model(&Page{}).Count(&pages).Error; err != nil || |
| + pages != 0 { |
| + t.Fatalf("a full token map made %d pages (%v)", pages, err) |
| + } |
| +} |