mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-14 02:40:47 +02:00
pool.PublishMany() runs in parallel.
This commit is contained in:
parent
b48d2249b9
commit
1ccd9ba417
43
pool.go
43
pool.go
@ -217,17 +217,46 @@ type PublishResult struct {
|
|||||||
func (pool *SimplePool) PublishMany(ctx context.Context, urls []string, evt Event) chan PublishResult {
|
func (pool *SimplePool) PublishMany(ctx context.Context, urls []string, evt Event) chan PublishResult {
|
||||||
ch := make(chan PublishResult, len(urls))
|
ch := make(chan PublishResult, len(urls))
|
||||||
|
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(urls))
|
||||||
go func() {
|
go func() {
|
||||||
for _, url := range urls {
|
for _, url := range urls {
|
||||||
relay, err := pool.EnsureRelay(url)
|
go func() {
|
||||||
if err != nil {
|
defer wg.Done()
|
||||||
ch <- PublishResult{err, url, nil}
|
|
||||||
} else {
|
relay, err := pool.EnsureRelay(url)
|
||||||
err = relay.Publish(ctx, evt)
|
if err != nil {
|
||||||
ch <- PublishResult{err, url, relay}
|
ch <- PublishResult{err, url, nil}
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := relay.Publish(ctx, evt); err == nil {
|
||||||
|
// success with no auth required
|
||||||
|
ch <- PublishResult{nil, url, relay}
|
||||||
|
} else if strings.HasPrefix(err.Error(), "msg: auth-required:") && pool.authHandler != nil {
|
||||||
|
// try to authenticate if we can
|
||||||
|
if authErr := relay.Auth(ctx, func(event *Event) error {
|
||||||
|
return pool.authHandler(ctx, RelayEvent{Event: event, Relay: relay})
|
||||||
|
}); authErr == nil {
|
||||||
|
if err := relay.Publish(ctx, evt); err == nil {
|
||||||
|
// success after auth
|
||||||
|
ch <- PublishResult{nil, url, relay}
|
||||||
|
} else {
|
||||||
|
// failure after auth
|
||||||
|
ch <- PublishResult{err, url, relay}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// failure to auth
|
||||||
|
ch <- PublishResult{fmt.Errorf("failed to auth: %w", authErr), url, relay}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// direct failure
|
||||||
|
ch <- PublishResult{err, url, relay}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
close(ch)
|
close(ch)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user