mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-28 21:02:28 +02:00
pool.SubManyNotifyEOSE()
This commit is contained in:
39
pool.go
39
pool.go
@@ -234,6 +234,28 @@ func (pool *SimplePool) SubMany(
|
|||||||
urls []string,
|
urls []string,
|
||||||
filters Filters,
|
filters Filters,
|
||||||
opts ...SubscriptionOption,
|
opts ...SubscriptionOption,
|
||||||
|
) chan RelayEvent {
|
||||||
|
return pool.subMany(ctx, urls, filters, nil, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubManyNotifyEOSE is like SubMany, but takes a channel that is closed when
|
||||||
|
// all subscriptions have received an EOSE
|
||||||
|
func (pool *SimplePool) SubManyNotifyEOSE(
|
||||||
|
ctx context.Context,
|
||||||
|
urls []string,
|
||||||
|
filters Filters,
|
||||||
|
eoseChan chan struct{},
|
||||||
|
opts ...SubscriptionOption,
|
||||||
|
) chan RelayEvent {
|
||||||
|
return pool.subMany(ctx, urls, filters, eoseChan, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pool *SimplePool) subMany(
|
||||||
|
ctx context.Context,
|
||||||
|
urls []string,
|
||||||
|
filters Filters,
|
||||||
|
eoseChan chan struct{},
|
||||||
|
opts ...SubscriptionOption,
|
||||||
) chan RelayEvent {
|
) chan RelayEvent {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
_ = cancel // do this so `go vet` will stop complaining
|
_ = cancel // do this so `go vet` will stop complaining
|
||||||
@@ -242,6 +264,14 @@ func (pool *SimplePool) SubMany(
|
|||||||
ticker := time.NewTicker(seenAlreadyDropTick)
|
ticker := time.NewTicker(seenAlreadyDropTick)
|
||||||
|
|
||||||
eose := false
|
eose := false
|
||||||
|
eoseWg := sync.WaitGroup{}
|
||||||
|
eoseWg.Add(len(urls))
|
||||||
|
if eoseChan != nil {
|
||||||
|
go func() {
|
||||||
|
eoseWg.Wait()
|
||||||
|
close(eoseChan)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
pending := xsync.NewCounter()
|
pending := xsync.NewCounter()
|
||||||
pending.Add(int64(len(urls)))
|
pending.Add(int64(len(urls)))
|
||||||
@@ -250,6 +280,7 @@ func (pool *SimplePool) SubMany(
|
|||||||
urls[i] = url
|
urls[i] = url
|
||||||
if idx := slices.Index(urls, url); idx != i {
|
if idx := slices.Index(urls, url); idx != i {
|
||||||
// skip duplicate relays in the list
|
// skip duplicate relays in the list
|
||||||
|
eoseWg.Done()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +336,12 @@ func (pool *SimplePool) SubMany(
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-sub.EndOfStoredEvents
|
<-sub.EndOfStoredEvents
|
||||||
|
|
||||||
|
// guard here otherwise a resubscription will trigger a duplicate call to eoseWg.Done()
|
||||||
|
if !eose {
|
||||||
eose = true
|
eose = true
|
||||||
|
eoseWg.Done()
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// reset interval when we get a good subscription
|
// reset interval when we get a good subscription
|
||||||
@@ -359,6 +395,9 @@ func (pool *SimplePool) SubMany(
|
|||||||
} else {
|
} else {
|
||||||
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
|
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eoseWg.Done()
|
||||||
|
|
||||||
return
|
return
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user