mirror of
https://github.com/bitvora/wot-relay.git
synced 2025-06-23 16:05:35 +00:00
fix race conditions
This commit is contained in:
parent
61554badfc
commit
e2b9fbc323
57
main.go
57
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,28 +206,43 @@ func appendPubkey(pubkey string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func archiveTrustedNotes(relay *khatru.Relay) {
|
func archiveTrustedNotes(relay *khatru.Relay) {
|
||||||
ctx := context.Background()
|
// Create a ticker to restart the function every minute
|
||||||
filters := []nostr.Filter{{
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
Kinds: []int{
|
defer ticker.Stop()
|
||||||
nostr.KindArticle,
|
|
||||||
nostr.KindDeletion,
|
|
||||||
nostr.KindContactList,
|
|
||||||
nostr.KindEncryptedDirectMessage,
|
|
||||||
nostr.KindMuteList,
|
|
||||||
nostr.KindReaction,
|
|
||||||
nostr.KindRelayListMetadata,
|
|
||||||
nostr.KindRepost,
|
|
||||||
nostr.KindZapRequest,
|
|
||||||
nostr.KindZap,
|
|
||||||
nostr.KindTextNote,
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
|
|
||||||
for ev := range pool.SubMany(ctx, relays, filters) {
|
for range ticker.C {
|
||||||
for _, trustedPubkey := range trustNetwork {
|
// Copy the current state of trustNetwork at the start of the function
|
||||||
if ev.Event.PubKey == trustedPubkey {
|
mu.Lock() // Lock while copying the trustNetwork to avoid partial reads
|
||||||
relay.AddEvent(ctx, ev.Event)
|
localTrustNetwork := make([]string, len(trustNetwork))
|
||||||
|
copy(localTrustNetwork, trustNetwork)
|
||||||
|
mu.Unlock() // Unlock immediately after copying
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
filters := []nostr.Filter{{
|
||||||
|
Kinds: []int{
|
||||||
|
nostr.KindArticle,
|
||||||
|
nostr.KindDeletion,
|
||||||
|
nostr.KindContactList,
|
||||||
|
nostr.KindEncryptedDirectMessage,
|
||||||
|
nostr.KindMuteList,
|
||||||
|
nostr.KindReaction,
|
||||||
|
nostr.KindRelayListMetadata,
|
||||||
|
nostr.KindRepost,
|
||||||
|
nostr.KindZapRequest,
|
||||||
|
nostr.KindZap,
|
||||||
|
nostr.KindTextNote,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
// Use the local copy of trustNetwork in the event loop
|
||||||
|
for ev := range pool.SubMany(ctx, relays, filters) {
|
||||||
|
for _, trustedPubkey := range localTrustNetwork {
|
||||||
|
if ev.Event.PubKey == trustedPubkey {
|
||||||
|
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