go-nostr/pool.go

144 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-05-05 19:05:11 -03:00
package nostr
import (
"context"
2023-05-09 00:08:06 -03:00
"fmt"
2023-05-05 19:05:11 -03:00
"sync"
"time"
2023-05-05 19:05:11 -03:00
2023-05-30 13:52:14 -03:00
"github.com/puzpuzpuz/xsync"
2023-05-05 19:05:11 -03:00
)
type SimplePool struct {
2023-05-06 14:32:39 -03:00
Relays map[string]*Relay
Context context.Context
2023-05-05 19:05:11 -03:00
2023-05-06 14:32:39 -03:00
mutex sync.Mutex
cancel context.CancelFunc
2023-05-05 19:05:11 -03:00
}
2023-05-06 14:32:39 -03:00
func NewSimplePool(ctx context.Context) *SimplePool {
ctx, cancel := context.WithCancel(ctx)
2023-05-05 19:05:11 -03:00
return &SimplePool{
Relays: make(map[string]*Relay),
2023-05-06 14:32:39 -03:00
Context: ctx,
cancel: cancel,
2023-05-05 19:05:11 -03:00
}
}
2023-05-09 00:08:06 -03:00
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
2023-05-05 19:05:11 -03:00
nm := NormalizeURL(url)
pool.mutex.Lock()
defer pool.mutex.Unlock()
relay, ok := pool.Relays[nm]
if ok && relay.IsConnected() {
2023-05-05 19:05:11 -03:00
// already connected, unlock and return
2023-05-09 00:08:06 -03:00
return relay, nil
2023-05-05 19:05:11 -03:00
} else {
var err error
2023-05-06 14:32:39 -03:00
// we use this ctx here so when the pool dies everything dies
ctx, cancel := context.WithTimeout(pool.Context, time.Second*15)
defer cancel()
2023-06-11 10:48:46 -03:00
if relay, err = RelayConnect(ctx, nm); err != nil {
2023-05-09 00:08:06 -03:00
return nil, fmt.Errorf("failed to connect: %w", err)
2023-05-05 19:05:11 -03:00
}
pool.Relays[nm] = relay
2023-05-09 00:08:06 -03:00
return relay, nil
2023-05-05 19:05:11 -03:00
}
}
// SubMany opens a subscription with the given filters to multiple relays
// the subscriptions only end when the context is canceled
2023-06-11 10:48:46 -03:00
func (pool *SimplePool) SubMany(ctx context.Context, urls []string, filters Filters) chan *Event {
2023-05-05 19:05:11 -03:00
uniqueEvents := make(chan *Event)
2023-05-30 13:52:14 -03:00
seenAlready := xsync.NewMapOf[bool]()
2023-05-05 19:05:11 -03:00
pending := xsync.Counter{}
initial := len(urls)
pending.Add(int64(initial))
2023-05-05 19:05:11 -03:00
for _, url := range urls {
go func(nm string) {
2023-05-09 00:08:06 -03:00
relay, err := pool.EnsureRelay(nm)
if err != nil {
return
}
sub, _ := relay.Subscribe(ctx, filters)
2023-05-05 19:05:11 -03:00
if sub == nil {
return
}
for evt := range sub.Events {
// dispatch unique events to client
2023-05-30 13:52:14 -03:00
if _, ok := seenAlready.LoadOrStore(evt.ID, true); !ok {
2023-05-05 19:05:11 -03:00
uniqueEvents <- evt
}
}
pending.Dec()
if pending.Value() == 0 {
close(uniqueEvents)
}
2023-05-05 19:05:11 -03:00
}(NormalizeURL(url))
}
return uniqueEvents
}
// SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE
2023-06-11 10:48:46 -03:00
func (pool *SimplePool) SubManyEose(ctx context.Context, urls []string, filters Filters) chan *Event {
2023-05-05 19:05:11 -03:00
ctx, cancel := context.WithCancel(ctx)
uniqueEvents := make(chan *Event)
2023-05-30 13:52:14 -03:00
seenAlready := xsync.NewMapOf[bool]()
2023-05-05 19:05:11 -03:00
wg := sync.WaitGroup{}
wg.Add(len(urls))
go func() {
// this will happen when all subscriptions get an eose (or when they die)
wg.Wait()
cancel()
close(uniqueEvents)
}()
for _, url := range urls {
go func(nm string) {
2023-05-09 00:08:06 -03:00
relay, err := pool.EnsureRelay(nm)
if err != nil {
return
}
sub, _ := relay.Subscribe(ctx, filters)
2023-05-05 19:05:11 -03:00
if sub == nil {
wg.Done()
return
}
defer wg.Done()
for {
select {
case <-sub.EndOfStoredEvents:
return
case evt, more := <-sub.Events:
if !more {
return
}
// dispatch unique events to client
2023-05-30 13:52:14 -03:00
if _, ok := seenAlready.LoadOrStore(evt.ID, true); !ok {
2023-05-05 19:05:11 -03:00
uniqueEvents <- evt
}
}
}
}(NormalizeURL(url))
}
return uniqueEvents
}