oficina / mapa Public

First scratch commit: the token cap is per person

Fable's re-review of 14ab6a6, L4. The 4,096-token cap was one map for
everyone, so one person's tabs could use up everyone's first scratch
commits. Tokens are now held per person, 256 each, still failing
closed (503) past that person's cap.

Gate: TestAFullTokenMapRefusesANewPage now also requires Bo's first
commit to succeed while Ana's map is full; with the cap counted over
everyone (the old rule) it fails: Ana's full map refused Bo.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 6b18c3027fe82ceacb9cf6c569f0ef83837d32e3 parent 9db58d6
2 files changed, +54 −28
  • internal/mapa/firstscratch.go +35 −21
  • internal/mapa/firstscratch_test.go +19 −7
diff --git a/internal/mapa/firstscratch.go b/internal/mapa/firstscratch.go
index 2706642..f80744f 100644
--- a/internal/mapa/firstscratch.go
+++ b/internal/mapa/firstscratch.go
@@ -25,17 +25,20 @@ import (
// firstHold is how long a token is remembered.
const firstHold = 10 * time.Minute
-// 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
+// firstCap bounds how many tokens one person has remembered at once
+// (review round 4: per person, so no one can use up anyone else's).
+// Past it that person's new token is refused, never forgotten: a token
+// the server could not remember would make a page on every autosave.
+// A tab draws one token per New scratch page.
+const firstCap = 256
var firstToken = regexp.MustCompile(`^[A-Za-z0-9_-]{16,64}$`)
-// firsts remembers which scratch page each first-commit token made.
+// firsts remembers which scratch page each first-commit token made, by
+// person and then token.
type firsts struct {
mutex sync.Mutex
- made map[string]firstMade
+ made map[string]map[string]firstMade
}
type firstMade struct {
@@ -46,26 +49,37 @@ type firstMade struct {
// recall is the page a token made, forgetting tokens past firstHold.
// The caller holds the mutex.
-func (held *firsts) recall(key string, now time.Time) (firstMade, bool) {
+func (held *firsts) recall(person, token string,
+ now time.Time) (firstMade, bool) {
if held.made == nil {
- held.made = map[string]firstMade{}
+ held.made = map[string]map[string]firstMade{}
}
- for other, made := range held.made {
- if now.Sub(made.at) > firstHold {
- delete(held.made, other)
+ for who, tokens := range held.made {
+ for other, made := range tokens {
+ if now.Sub(made.at) > firstHold {
+ delete(tokens, other)
+ }
+ }
+ if len(tokens) == 0 {
+ delete(held.made, who)
}
}
- made, ok := held.made[key]
+ made, ok := held.made[person][token]
return made, ok
}
-func (held *firsts) remember(key string, made firstMade) {
- held.made[key] = made
+func (held *firsts) remember(person, token string, made firstMade) {
+ if held.made[person] == nil {
+ held.made[person] = map[string]firstMade{}
+ }
+ held.made[person][token] = 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 }
+// full is whether the person can have no new token remembered. The
+// caller holds the mutex, after recall has forgotten the expired ones.
+func (held *firsts) full(person string) bool {
+ return len(held.made[person]) >= firstCap
+}
// firstScratch is POST /scratch with a "first" token: the new page's
// first autosave. It answers JSON: {href, page, revision} when it made
@@ -79,15 +93,15 @@ func (app *app) firstScratch(writer http.ResponseWriter,
notFound(writer, request)
return
}
- key := viewer.Address + "\x00" + token
+ person := viewer.Address
app.firsts.mutex.Lock()
defer app.firsts.mutex.Unlock()
- made, ok := app.firsts.recall(key, app.store.now())
+ made, ok := app.firsts.recall(person, token, app.store.now())
if ok {
app.firstDraft(writer, request, viewer, made)
return
}
- if app.firsts.full() {
+ if app.firsts.full(person) {
// Nothing is made; the editor keeps the text and tries again.
refuseJSON(writer, http.StatusServiceUnavailable, "busy")
return
@@ -106,7 +120,7 @@ func (app *app) firstScratch(writer http.ResponseWriter,
}
made = firstMade{pageID: page.ID, revision: page.Revision,
public: publicID(page.Nonce), at: app.store.now()}
- app.firsts.remember(key, made)
+ app.firsts.remember(person, token, made)
answer(writer, map[string]any{"href": PagePath(page),
"page": made.public, "revision": idText(page.Revision)})
}
diff --git a/internal/mapa/firstscratch_test.go b/internal/mapa/firstscratch_test.go
index 85a1462..356cc31 100644
--- a/internal/mapa/firstscratch_test.go
+++ b/internal/mapa/firstscratch_test.go
@@ -11,19 +11,21 @@ import (
"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.
+// The first scratch commit's token map fails closed (review L): when a
+// person holds firstCap live tokens, their 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. The cap is per person (review
+// round 4): Bo's first scratch commit still works while Ana's is full.
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{}
+ built.firsts.made = map[string]map[string]firstMade{
+ "ana@example.com": {}}
for index := range firstCap {
- built.firsts.made["held"+strconv.Itoa(index)] = firstMade{
- at: store.now()}
+ built.firsts.made["ana@example.com"]["held"+strconv.Itoa(index)] =
+ firstMade{at: store.now()}
}
for range 2 {
form := url.Values{"body": {"Hello."}}
@@ -42,4 +44,14 @@ func TestAFullTokenMapRefusesANewPage(t *testing.T) {
pages != 0 {
t.Fatalf("a full token map made %d pages (%v)", pages, err)
}
+ built.testIdentity = "bo@example.com"
+ form := url.Values{"body": {"Bo's."}}
+ 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, "TOKENTOKENTOKEN0002")
+ if recorder.Code != http.StatusOK {
+ t.Fatalf("Ana's full token map refused Bo: %d", recorder.Code)
+ }
}