Skip to content

Commit

Permalink
Merge pull request #38 from bitvora/dev-profileRefresh
Browse files Browse the repository at this point in the history
broadcast events, process in goroutine
  • Loading branch information
barrydeen authored Sep 13, 2024
2 parents d5ee8a3 + 3c6f93c commit 47f1b94
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var booted bool
var oneHopNetwork []string
var trustNetworkMap map[string]bool
var pubkeyFollowerCount = make(map[string]int)
var trustedNotes uint64
var untrustedNotes uint64

func main() {
nostr.InfoLogger = log.New(io.Discard, "", 0)
Expand Down Expand Up @@ -78,17 +80,16 @@ func main() {

relay.RejectEvent = append(relay.RejectEvent,
policies.RejectEventsWithBase64Media,
policies.EventIPRateLimiter(5, time.Minute*2, 30),
policies.EventIPRateLimiter(5, time.Minute*1, 30),
)

relay.RejectFilter = append(relay.RejectFilter,
policies.NoEmptyFilters,
policies.NoComplexFilters,
// policies.FilterIPRateLimiter(50, time.Minute, 250),
)

relay.RejectConnection = append(relay.RejectConnection,
policies.ConnectionRateLimiter(10, time.Minute*1, 30),
policies.ConnectionRateLimiter(10, time.Minute*2, 30),
)

relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
Expand Down Expand Up @@ -349,8 +350,6 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
}}

log.Println("πŸ“¦ archiving trusted notes...")
var trustedNotes uint64
var untrustedNotes uint64

eventChan := pool.SubMany(ctx, seedRelays, filters)

Expand All @@ -359,31 +358,43 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
case <-timeout:
log.Println("⏰ Archive process terminated due to timeout")
log.Println("πŸ“¦ archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
trustedNotes = 0
untrustedNotes = 0
return

case <-ctx.Done():
log.Println("⏰ Archive process terminated due to context cancellation")
log.Println("πŸ“¦ archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
trustedNotes = 0
untrustedNotes = 0
return

case ev, ok := <-eventChan:
if !ok {
log.Println("πŸ“¦ subscription channel closed")
log.Println("πŸ“¦ archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
trustedNotes = 0
untrustedNotes = 0
return
}

if trustNetworkMap[ev.Event.PubKey] {
if len(ev.Event.Tags) > 3000 {
continue
}
go processEvent(ctx, ev.Event, relay)
}
}
}

relay.AddEvent(ctx, ev.Event)
trustedNotes++
//log.Println("πŸ“¦ archived note: ", ev.Event.ID)
} else {
untrustedNotes++
}
func processEvent(ctx context.Context, ev *nostr.Event, relay *khatru.Relay) {
if trustNetworkMap[ev.PubKey] {
if len(ev.Tags) > 3000 {
return
}

relay.AddEvent(ctx, ev)
relay.BroadcastEvent(ev)
trustedNotes++
log.Println("πŸ“¦ archived note: ", ev.ID)
} else {
log.Println("πŸ“¦ discarded note: ", ev.ID)
untrustedNotes++
}
}

0 comments on commit 47f1b94

Please sign in to comment.