oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "context"
5 "net/http"
6 "time"
7
8 "amadan.net/oficina/home/roster"
9 "amadan.net/rastrillo/rastrillo/sessions"
10)
11
12type teamStreamGuard struct {
13 store *Store
14 access *requestAccess
15 request *http.Request
16 docID int64
17 // deadline is the Member lease's, fixed when the stream opened; zero
18 // for a stream that rests on no lease (a guest's, or Unknown's, grant).
19 deadline time.Time
20 // role is the roster role the stream opened with. A fresh standing
21 // continues the stream only with the same role: a publication that
22 // changes it ends the stream, and the reconnect is decided afresh.
23 role string
24}
25
26// remaining is how long the stream may run before it must re-decide: the
27// opening lease's, never past the deadline it opened with. Used at open;
28// each delivery asks afresh (check).
29func (g *teamStreamGuard) remaining() time.Duration {
30 if g.deadline.IsZero() {
31 return defaultStreamWriteDeadline
32 }
33 return min(g.store.team.lease(g.access), time.Until(g.deadline))
34}
35
36// Lease contention (#175 finding 4). roster.Lease.Remaining and Valid use
37// TryLock and answer "expired" when another goroutine holds the roster
38// cache's mutex, which every request's admission takes; and
39// teammode.Access latches Unknown the first time it sees that. A stream
40// that kept its opening Access therefore died for good at the first lost
41// TryLock. teammode's own rule is that a stream asks for a new Access
42// before each delivery, so check does, and a zero lease or Unknown standing
43// is asked again a few times before it is believed: contention passes, an
44// expired lease does not.
45const (
46 leaseTries = 4
47 leaseRetryWait = 2 * time.Millisecond
48)
49
50// lease is a.Remaining(), or the tests' stand-in for a contended roster.
51func (t *teamRuntime) lease(a *requestAccess) time.Duration {
52 if t.leaseRemaining != nil {
53 return t.leaseRemaining(a)
54 }
55 return a.Remaining()
56}
57
58// freshLease asks the roster for id's standing now, again while it reads as
59// a Member with no lease left or as Unknown. It returns the last Access and
60// its lease (0 for a standing that rests on none).
61func (t *teamRuntime) freshLease(ctx context.Context, id requestIdentity) (*requestAccess, time.Duration) {
62 var a *requestAccess
63 for try := 0; try < leaseTries; try++ {
64 if try > 0 {
65 select {
66 case <-ctx.Done():
67 return a, 0
68 case <-time.After(leaseRetryWait):
69 }
70 }
71 a = t.resolveAccess(ctx, id)
72 t.freshLeases.Add(1)
73 switch a.state() {
74 case roster.Member:
75 if left := t.lease(a); left > 0 {
76 return a, left
77 }
78 case roster.NotMember:
79 return a, 0
80 }
81 }
82 return a, 0
83}
84
85// check re-decides the stream before a delivery: the session is still the
86// one it opened with, the person still the same, a fresh Access for them
87// (with its lease, for a stream that opened on one), and read permission
88// under that fresh standing. A stream never outlives the deadline it
89// opened with: reconnection obtains fresh admission.
90func (g *teamStreamGuard) check() (time.Duration, bool) {
91 t := g.store.team
92 if t == nil || t.home == nil || t.readSession == nil {
93 return 0, false
94 }
95 limit := defaultStreamWriteDeadline
96 if !g.deadline.IsZero() {
97 limit = min(limit, time.Until(g.deadline))
98 }
99 if limit <= 0 {
100 return 0, false
101 }
102 ctx, cancel := context.WithTimeout(g.request.Context(), limit)
103 defer cancel()
104 if _, present, ok := homeCookie(g.request, t.home.sessionCookie); !ok || !present {
105 return 0, false
106 }
107 session, ok := t.readSession(ctx, g.request)
108 if !ok || ctx.Err() != nil {
109 return 0, false
110 }
111 request := sessions.WithSession(g.request.WithContext(ctx), session)
112 id, e := t.home.resolver.resolve(request)
113 if e != nil || !sameActor(id.Actor, g.access.actor) || id.HomeSubject != g.access.Subject() {
114 return 0, false
115 }
116 // A stream that rests on no lease (a guest's grant, or one opened under
117 // Unknown) keeps the standing it opened with, as before: its access is
118 // the grant, and a roster check, which may wait on a refresh, would buy
119 // it nothing (#175 approval note 4). Only a stream opened on a Member
120 // lease asks afresh.
121 standing := g.access
122 remaining := defaultStreamWriteDeadline
123 if g.deadline.IsZero() && standing.state() == roster.Member {
124 // Never Member without a lease. A stream that opened on no lease
125 // and now reads as a Member ends; its reconnect is decided afresh
126 // with a lease. This does not rest on Access.Remaining having
127 // latched Unknown when it read 0.
128 return 0, false
129 }
130 if !g.deadline.IsZero() {
131 fresh, left := t.freshLease(ctx, id)
132 // Opened on a Member lease: it must still hold one, in the role it
133 // opened with.
134 if fresh.state() != roster.Member || left <= 0 || fresh.Role() != g.role {
135 return 0, false
136 }
137 standing = fresh
138 remaining = min(left, time.Until(g.deadline))
139 }
140 if e = g.store.checkPermission(withAccess(ctx, standing), g.store.db.WithContext(ctx), id.Actor, g.docID, permissionRead); e != nil {
141 return 0, false
142 }
143 if ctx.Err() != nil || remaining <= 0 {
144 return 0, false
145 }
146 return min(remaining, defaultStreamWriteDeadline), true
147}
148