Live push 1b: a stream on no lease asks the roster nothing per delivery (#175 approval, note 4)
The fresh-lease check ran for every team stream, so one resting on no lease (a guest's grant, or opened under Unknown) asked the roster up to four times per delivery, each ask able to wait on an in-flight refresh, for a standing its grant does not need. Such a stream now keeps the standing it opened with, as before the fresh-lease change; only a stream opened on a Member lease asks afresh. That one's asks are still up to four per delivery, bounded by the delivery's deadline; recorded in the plan. TestTeamGuestStreamAsksTheRosterNothing: a removed member's stream on a document granted to them gets three frames with no roster ask; before, one ask per delivery.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+41
−2
internal/docs/push_test.go+28 −0internal/docs/team_config.go+3 −0internal/docs/team_stream.go+10 −2
diff --git a/internal/docs/push_test.go b/internal/docs/push_test.go| index eb0aaed..06caf95 100644 |
| --- a/internal/docs/push_test.go |
| +++ b/internal/docs/push_test.go |
| @@ -1434,3 +1434,31 @@ func TestLogEpochTwoConcurrentStarts(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// #175 approval note 4: a stream that rests on no lease (a guest's grant) |
| +// asks the roster nothing per delivery: its standing is the one it opened |
| +// with, as before, and a roster check that may wait on a refresh is not |
| +// spent on it. |
| +func TestTeamGuestStreamAsksTheRosterNothing(t *testing.T) { |
| + f, tf := teamPushFixture(t) |
| + tf.clock.step(roster.RetryAfter) |
| + tf.directory.change([]roster.MemberEntry{{Subject: teamSubject('b'), Role: "member"}, {Subject: teamSubject('c'), Role: "member"}}, false) |
| + if err := tf.app.store.team.cache.Refresh(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.doc = tf.shared // Bob's, granted to alice, who is no longer a member |
| + s := f.open(f.cookies, "?push=1&since="+strconv.FormatInt(f.currentSeq(), 10), nil) |
| + s.hello(t) |
| + before := f.app.store.team.freshLeases.Load() |
| + for k := 1; k <= 3; k++ { |
| + if code, _ := f.postHost(f.update(k, fmt.Sprint("guest", k)), ""); code != http.StatusNoContent { |
| + t.Fatalf("guest POST %d", code) |
| + } |
| + if fr := s.next(t); fr.event != "push" { |
| + t.Fatalf("%+v, want push", fr) |
| + } |
| + } |
| + if n := f.app.store.team.freshLeases.Load() - before; n != 0 { |
| + t.Fatalf("a guest's stream asked the roster %d times for three deliveries", n) |
| + } |
| +} |
diff --git a/internal/docs/team_config.go b/internal/docs/team_config.go| index c7f6ae3..e0e5f77 100644 |
| --- a/internal/docs/team_config.go |
| +++ b/internal/docs/team_config.go |
| @@ -6,6 +6,7 @@ import ( |
| "database/sql" |
| "errors" |
| "net/http" |
| + "sync/atomic" |
| "time" |
| "amadan.net/oficina/home/handoff/seal" |
| @@ -76,6 +77,8 @@ type teamRuntime struct { |
| // leaseRemaining stands in for Access.Remaining in tests (a contended |
| // roster answers 0; team_stream.go). nil in production. |
| leaseRemaining func(*requestAccess) time.Duration |
| + // freshLeases counts freshLease's roster asks (tests). |
| + freshLeases atomic.Int64 |
| } |
| func newTeamRuntime(ctx context.Context, settings *teamSettings, db *sql.DB, home *homeSettings) (*teamRuntime, error) { |
diff --git a/internal/docs/team_stream.go b/internal/docs/team_stream.go| index d36ed12..6b6f7a1 100644 |
| --- a/internal/docs/team_stream.go |
| +++ b/internal/docs/team_stream.go |
| @@ -69,6 +69,7 @@ func (t *teamRuntime) freshLease(ctx context.Context, id requestIdentity) (*requ |
| } |
| } |
| a = t.resolveAccess(ctx, id) |
| + t.freshLeases.Add(1) |
| switch a.state() { |
| case roster.Member: |
| if left := t.lease(a); left > 0 { |
| @@ -112,17 +113,24 @@ func (g *teamStreamGuard) check() (time.Duration, bool) { |
| if e != nil || !sameActor(id.Actor, g.access.actor) || id.HomeSubject != g.access.Subject() { |
| return 0, false |
| } |
| - fresh, left := t.freshLease(ctx, id) |
| + // A stream that rests on no lease (a guest's grant, or one opened under |
| + // Unknown) keeps the standing it opened with, as before: its access is |
| + // the grant, and a roster check, which may wait on a refresh, would buy |
| + // it nothing (#175 approval note 4). Only a stream opened on a Member |
| + // lease asks afresh. |
| + standing := g.access |
| remaining := defaultStreamWriteDeadline |
| if !g.deadline.IsZero() { |
| + fresh, left := t.freshLease(ctx, id) |
| // Opened on a Member lease: it must still hold one, in the role it |
| // opened with. |
| if fresh.state() != roster.Member || left <= 0 || fresh.Role() != g.role { |
| return 0, false |
| } |
| + standing = fresh |
| remaining = min(left, time.Until(g.deadline)) |
| } |
| - if e = g.store.checkPermission(withAccess(ctx, fresh), g.store.db.WithContext(ctx), id.Actor, g.docID, permissionRead); e != nil { |
| + if e = g.store.checkPermission(withAccess(ctx, standing), g.store.db.WithContext(ctx), id.Actor, g.docID, permissionRead); e != nil { |
| return 0, false |
| } |
| if ctx.Err() != nil || remaining <= 0 { |