@@ -2,18 +2,46 @@ package wot
22
33import (
44 "context"
5+ "sync/atomic"
56 "time"
67
8+ "fiatjaf.com/nostr"
79 "github.com/fiatjaf/pyramid/global"
810)
911
10- // Current is the latest computed aggregated web-of-trust filter.
11- // It is safe to read at any time, even before it has been computed
12- // (it will just return an empty filter that contains nothing).
13- var Current XorFilter
12+ // current holds the latest computed aggregated web-of-trust filter.
13+ // it is stored behind an atomic pointer so it can be read from request
14+ // goroutines while the background goroutine replaces it, without racing
15+ // on the multi-word XorFilter struct.
16+ var current atomic.Pointer [XorFilter ]
1417
15- // Computed is true after the first successful WoT computation.
16- var Computed bool
18+ // computed is set to true after the first successful WoT computation.
19+ var computed atomic.Bool
20+
21+ // Contains reports whether the given pubkey is in the latest aggregated WoT.
22+ // It is safe to call at any time, even before the first computation
23+ // (it will just return false).
24+ func Contains (pubkey nostr.PubKey ) bool {
25+ f := current .Load ()
26+ if f == nil {
27+ return false
28+ }
29+ return f .Contains (pubkey )
30+ }
31+
32+ // IsComputed reports whether the WoT has been computed at least once.
33+ func IsComputed () bool {
34+ return computed .Load ()
35+ }
36+
37+ // Count returns the number of pubkeys in the latest aggregated WoT.
38+ func Count () int {
39+ f := current .Load ()
40+ if f == nil {
41+ return 0
42+ }
43+ return f .Items
44+ }
1745
1846// StartBackgroundComputation begins a periodic background loop that
1947// recomputes the aggregated WoT every 48 hours. It sleeps 2 minutes
@@ -28,8 +56,8 @@ func StartBackgroundComputation() {
2856 global .Log .Error ().Err (err ).Msg ("failed to compute aggregated WoT" )
2957 time .Sleep (3 * time .Hour )
3058 } else {
31- Current = wot
32- Computed = true
59+ current . Store ( & wot )
60+ computed . Store ( true )
3361 global .Log .Info ().Int ("entries" , wot .Items ).Msg ("computed aggregated WoT" )
3462 time .Sleep (48 * time .Hour )
3563 }
0 commit comments