Repository navigation
Expand file tree
/
Copy pathpush.go
More file actions
115 lines (103 loc) · 3.6 KB
/
Copy pathpush.go
File metadata and controls
115 lines (103 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import "sync"
// PushConsumer is the EVENT-DRIVEN approach: it maintains a LOCAL materialized
// view of exactly the entitlements it cares about, built by consuming the owner's
// event log. Reads are local map lookups — no call to the owner, ever. This is the
// "staff engineer deletes the boundary" path: instead of making the per-request
// dependency more resilient, it removes the per-request dependency entirely.
//
// The owner can be slow, overloaded, or completely down and this consumer answers
// reads at full speed and full correctness, because correctness was moved to write
// time (when the fact was published) instead of read time (when the question is
// asked).
type PushConsumer struct {
mu sync.RWMutex
view map[string]map[string]bool // subject -> perm -> granted (local projection)
log *EventLog
cursor int // highest Seq applied; the "breadcrumb" for resume
done chan struct{}
wg sync.WaitGroup
}
func NewPushConsumer(log *EventLog) *PushConsumer {
return &PushConsumer{
view: map[string]map[string]bool{},
log: log,
cursor: -1,
done: make(chan struct{}),
}
}
// Start replays all history, then follows the log live. Crucially, the replay and
// the live subscription are stitched together without a gap: we subscribe first
// (capturing the high-water mark), replay up to it, then apply live events. An
// event that lands during replay is buffered on the channel and applied after,
// and apply() is idempotent on Seq so an overlap at the seam is harmless.
//
// This is also the recovery path: a consumer that boots fresh — even mid-outage,
// with the owner unreachable — rebuilds its entire view from the log alone. It
// never contacts the owner to catch up. (Projection logic and this replay path are
// what the tests cover.)
func (p *PushConsumer) Start() {
ch, highWater, cancel := p.log.Subscribe()
for _, e := range p.log.Read(-1) { // replay history...
if e.Seq > highWater {
break // anything past the mark arrives on the live channel
}
p.apply(e)
}
p.wg.Add(1)
go func() {
defer p.wg.Done()
defer cancel()
for {
select {
case e := <-ch:
p.apply(e)
case <-p.done:
return
}
}
}()
}
// Stop halts the follow goroutine.
func (p *PushConsumer) Stop() {
close(p.done)
p.wg.Wait()
}
// apply folds one event into the local view. Idempotent on Seq: applying the same
// or an older event again is a no-op, which is what makes the replay/live seam and
// any at-least-once delivery safe.
func (p *PushConsumer) apply(e Event) {
p.mu.Lock()
defer p.mu.Unlock()
if e.Seq <= p.cursor {
return
}
if p.view[e.Subject] == nil {
p.view[e.Subject] = map[string]bool{}
}
p.view[e.Subject][e.Perm] = e.Granted
p.cursor = e.Seq
}
// Allowed answers from the local view. No owner call, no error to return — the
// only way this is wrong is if the view hasn't yet applied a very recent event
// (bounded, eventual staleness measured in milliseconds, never an outage).
func (p *PushConsumer) Allowed(subject, perm string) bool {
p.mu.RLock()
defer p.mu.RUnlock()
return p.view[subject][perm]
}
// Cursor exposes the highest applied Seq (used by tests and the demo to show the
// view has caught up).
func (p *PushConsumer) Cursor() int {
p.mu.RLock()
defer p.mu.RUnlock()
return p.cursor
}
// rebuildSynchronously replays history into the view with no live follow. Used by
// the demo's "fresh consumer boots mid-outage" scenario to show a complete rebuild
// with zero owner contact. apply() idempotency makes it safe to call standalone.
func (p *PushConsumer) rebuildSynchronously() {
for _, e := range p.log.Read(-1) {
p.apply(e)
}
}