go-nostr/pool.go

142 lines
2.8 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"
syncmap "github.com/SaveTheRbtz/generic-sync-map-go"
)
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.connectionContext.Err() == nil {
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
relay, err = RelayConnect(pool.Context, nm)
2023-05-05 19:05:11 -03:00
if 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
func (pool *SimplePool) SubMany(
ctx context.Context,
urls []string,
filters Filters,
) chan *Event {
uniqueEvents := make(chan *Event)
seenAlready := syncmap.MapOf[string, struct{}]{}
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
if _, ok := seenAlready.LoadOrStore(evt.ID, struct{}{}); !ok {
uniqueEvents <- evt
}
}
}(NormalizeURL(url))
}
return uniqueEvents
}
// SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE
func (pool *SimplePool) SubManyEose(
ctx context.Context,
urls []string,
filters Filters,
) chan *Event {
ctx, cancel := context.WithCancel(ctx)
uniqueEvents := make(chan *Event)
seenAlready := syncmap.MapOf[string, struct{}]{}
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
if _, ok := seenAlready.LoadOrStore(evt.ID, struct{}{}); !ok {
uniqueEvents <- evt
}
}
}
}(NormalizeURL(url))
}
return uniqueEvents
}