mirror of
https://github.com/bitvora/wot-relay.git
synced 2025-06-06 18:31:05 +00:00
fix race conditions
This commit is contained in:
parent
61554badfc
commit
e2b9fbc323
19
main.go
19
main.go
@ -117,7 +117,7 @@ func refreshTrustNetwork(relay *khatru.Relay) []string {
|
|||||||
ticker := time.NewTicker(10 * time.Minute)
|
ticker := time.NewTicker(10 * time.Minute)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
|
|
||||||
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
filters := []nostr.Filter{{
|
filters := []nostr.Filter{{
|
||||||
@ -206,6 +206,17 @@ func appendPubkey(pubkey string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func archiveTrustedNotes(relay *khatru.Relay) {
|
func archiveTrustedNotes(relay *khatru.Relay) {
|
||||||
|
// Create a ticker to restart the function every minute
|
||||||
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
// Copy the current state of trustNetwork at the start of the function
|
||||||
|
mu.Lock() // Lock while copying the trustNetwork to avoid partial reads
|
||||||
|
localTrustNetwork := make([]string, len(trustNetwork))
|
||||||
|
copy(localTrustNetwork, trustNetwork)
|
||||||
|
mu.Unlock() // Unlock immediately after copying
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
filters := []nostr.Filter{{
|
filters := []nostr.Filter{{
|
||||||
Kinds: []int{
|
Kinds: []int{
|
||||||
@ -223,11 +234,15 @@ func archiveTrustedNotes(relay *khatru.Relay) {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
// Use the local copy of trustNetwork in the event loop
|
||||||
for ev := range pool.SubMany(ctx, relays, filters) {
|
for ev := range pool.SubMany(ctx, relays, filters) {
|
||||||
for _, trustedPubkey := range trustNetwork {
|
for _, trustedPubkey := range localTrustNetwork {
|
||||||
if ev.Event.PubKey == trustedPubkey {
|
if ev.Event.PubKey == trustedPubkey {
|
||||||
relay.AddEvent(ctx, ev.Event)
|
relay.AddEvent(ctx, ev.Event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("archiveTrustedNotes: finished one cycle, will restart in 1 minute")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user