Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (82 loc) · 4.11 KB
/
Copy pathmain.go
File metadata and controls
96 lines (82 loc) · 4.11 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
package main
import (
"fmt"
"time"
)
// This program contrasts two ways a service can read data it does not own:
// pull — call the owner per request, with cache + TTL + retry
// push — consume the owner's event log into a local view, read locally
//
// It runs one storyline across four scenarios and prints what each approach
// answers, so the difference is visible rather than asserted.
func main() {
line := func() {
fmt.Println("────────────────────────────────────────────────────────")
}
report := func(scenario string) {
fmt.Printf("\n## %s\n", scenario)
}
log := NewEventLog()
owner := NewOwner(log)
// pull: 5s TTL cache, 2 retries. push: follows the log live.
pull := NewPullConsumer(owner, 5*time.Second, 2)
push := NewPushConsumer(log)
push.Start()
defer push.Stop()
subject, perm := "user:alice", "reports.export"
// Owner grants the permission. Both approaches should converge on "allowed".
owner.Grant(subject, perm)
time.Sleep(10 * time.Millisecond) // let the push view apply the live event
line()
fmt.Println("ENTITLEMENTS: pull (sync + cache) vs push (local view from event log)")
fmt.Printf("subject=%s perm=%s\n", subject, perm)
line()
// ---- Scenario 1: both healthy ----
report("1. Both healthy — both correct")
pg, _ := pull.Allowed(subject, perm)
fmt.Printf(" owner says: granted=true\n")
fmt.Printf(" pull answer: %v\n", pg)
fmt.Printf(" push answer: %v\n", push.Allowed(subject, perm))
fmt.Println(" → agree. Nothing interesting yet; this is the happy path both get right.")
// ---- Scenario 2: a permission changes; pull serves stale ----
report("2. Permission revoked — pull serves stale, push correct")
// Warm the pull cache with the current (granted) answer...
pull.Allowed(subject, perm)
// ...then the owner revokes. The pull cache entry is now wrong but unexpired.
owner.Revoke(subject, perm)
time.Sleep(10 * time.Millisecond) // push applies the revoke event
pg, _ = pull.Allowed(subject, perm)
fmt.Printf(" owner says: granted=false (just revoked)\n")
fmt.Printf(" pull answer: %v <- STALE within its %s TTL, and no error raised\n", pg, pull.ttl)
fmt.Printf(" push answer: %v <- correct; it applied the revoke fact\n", push.Allowed(subject, perm))
fmt.Println(" → the dangerous case: pull is confidently, silently wrong.")
// Re-grant and let both converge before the outage scenario.
owner.Grant(subject, perm)
time.Sleep(10 * time.Millisecond)
// ---- Scenario 3: owner outage on an uncached read ----
report("3. Owner outage, cold read — pull fails, push unaffected")
coldPerm := "billing.refund" // a perm the pull cache has never seen
owner.Grant(subject, coldPerm)
time.Sleep(10 * time.Millisecond) // push sees it via the log
owner.SetDown(true) // owner goes down
_, err := pull.Allowed(subject, coldPerm)
fmt.Printf(" owner: DOWN\n")
fmt.Printf(" pull answer: error = %v\n", err)
fmt.Printf(" push answer: %v <- no owner call on the read path\n", push.Allowed(subject, coldPerm))
fmt.Println(" → pull is blocked on someone else's uptime for data it needs now.")
// ---- Scenario 4: a fresh push consumer boots mid-outage ----
report("4. Fresh consumer boots during the outage — rebuilds from the log, zero owner contact")
fmt.Printf(" owner: STILL DOWN\n")
cold := NewPushConsumer(log)
cold.rebuildSynchronously() // replay history only; never calls the owner
fmt.Printf(" new consumer rebuilt view up to seq=%d with the owner unreachable\n", cold.Cursor())
fmt.Printf(" new consumer answer (%s): %v\n", perm, cold.Allowed(subject, perm))
fmt.Printf(" new consumer answer (%s): %v\n", coldPerm, cold.Allowed(subject, coldPerm))
fmt.Println(" → the log is the contract. A consumer can come up cold without the owner at all.")
owner.SetDown(false)
line()
fmt.Println("Takeaway: pull adds cache/TTL/retry to survive a boundary it keeps.")
fmt.Println("push deletes the read-time boundary: correctness moves to write time.")
fmt.Println("See the README for when pull is still the right call.")
line()
}