EnsureRelay() returns an error.

This commit is contained in:
fiatjaf
2023-05-09 00:08:06 -03:00
parent 6c186812c9
commit 52a493fd96

23
pool.go
View File

@@ -2,6 +2,7 @@ package nostr
import ( import (
"context" "context"
"fmt"
"sync" "sync"
syncmap "github.com/SaveTheRbtz/generic-sync-map-go" syncmap "github.com/SaveTheRbtz/generic-sync-map-go"
@@ -26,7 +27,7 @@ func NewSimplePool(ctx context.Context) *SimplePool {
} }
} }
func (pool *SimplePool) EnsureRelay(url string) *Relay { func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
nm := NormalizeURL(url) nm := NormalizeURL(url)
pool.mutex.Lock() pool.mutex.Lock()
@@ -35,17 +36,17 @@ func (pool *SimplePool) EnsureRelay(url string) *Relay {
relay, ok := pool.Relays[nm] relay, ok := pool.Relays[nm]
if ok && relay.ConnectionContext.Err() == nil { if ok && relay.ConnectionContext.Err() == nil {
// already connected, unlock and return // already connected, unlock and return
return relay return relay, nil
} else { } else {
var err error var err error
// we use this ctx here so when the pool dies everything dies // we use this ctx here so when the pool dies everything dies
relay, err = RelayConnect(pool.Context, nm) relay, err = RelayConnect(pool.Context, nm)
if err != nil { if err != nil {
return nil return nil, fmt.Errorf("failed to connect: %w", err)
} }
pool.Relays[nm] = relay pool.Relays[nm] = relay
return relay return relay, nil
} }
} }
@@ -61,7 +62,12 @@ func (pool *SimplePool) SubMany(
for _, url := range urls { for _, url := range urls {
go func(nm string) { go func(nm string) {
sub, _ := pool.EnsureRelay(nm).Subscribe(ctx, filters) relay, err := pool.EnsureRelay(nm)
if err != nil {
return
}
sub, _ := relay.Subscribe(ctx, filters)
if sub == nil { if sub == nil {
return return
} }
@@ -100,7 +106,12 @@ func (pool *SimplePool) SubManyEose(
for _, url := range urls { for _, url := range urls {
go func(nm string) { go func(nm string) {
sub, _ := pool.EnsureRelay(nm).Subscribe(ctx, filters) relay, err := pool.EnsureRelay(nm)
if err != nil {
return
}
sub, _ := relay.Subscribe(ctx, filters)
if sub == nil { if sub == nil {
wg.Done() wg.Done()
return return