mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-18 10:06:27 +01:00
pool: support CountMany() using hyperloglog.
This commit is contained in:
34
pool.go
34
pool.go
@@ -10,6 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr/nip45/hyperloglog"
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
)
|
||||
|
||||
@@ -468,6 +469,39 @@ func (pool *SimplePool) subManyEose(
|
||||
return events
|
||||
}
|
||||
|
||||
// CountMany aggregates count results from multiple relays using HyperLogLog
|
||||
func (pool *SimplePool) CountMany(
|
||||
ctx context.Context,
|
||||
urls []string,
|
||||
filter Filter,
|
||||
opts []SubscriptionOption,
|
||||
) int {
|
||||
hll := hyperloglog.New()
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(urls))
|
||||
for _, url := range urls {
|
||||
go func(nm string) {
|
||||
defer wg.Done()
|
||||
relay, err := pool.EnsureRelay(url)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ce, err := relay.countInternal(ctx, Filters{filter}, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(ce.HyperLogLog) != 256 {
|
||||
return
|
||||
}
|
||||
hll.MergeRegisters(ce.HyperLogLog)
|
||||
}(NormalizeURL(url))
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
return int(hll.Count())
|
||||
}
|
||||
|
||||
// QuerySingle returns the first event returned by the first relay, cancels everything else.
|
||||
func (pool *SimplePool) QuerySingle(ctx context.Context, urls []string, filter Filter) *RelayEvent {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
Reference in New Issue
Block a user