fix OK handling.

This commit is contained in:
fiatjaf 2023-12-09 13:21:35 -03:00
parent 1bccebbf2a
commit 3e1749a01d
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 8 additions and 3 deletions

View File

@ -256,13 +256,13 @@ func (pool *SimplePool) subManyEose(ctx context.Context, urls []string, filters
case reason := <-sub.ClosedReason:
if strings.HasPrefix(reason, "auth-required:") && pool.authHandler != nil && !hasAuthed {
// relay is requesting auth. if we can we will perform auth and try again
if err := relay.Auth(ctx, pool.authHandler); err == nil {
err := relay.Auth(ctx, pool.authHandler)
if err == nil {
hasAuthed = true // so we don't keep doing AUTH again and again
goto subscribe
}
} else {
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
}
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
return
case evt, more := <-sub.Events:
if !more {

View File

@ -319,7 +319,9 @@ func (r *Relay) publish(ctx context.Context, id string, env Envelope) error {
}
// listen for an OK callback
gotOk := false
r.okCallbacks.Store(id, func(ok bool, reason string) {
gotOk = true
if !ok {
err = fmt.Errorf("msg: %s", reason)
}
@ -338,6 +340,9 @@ func (r *Relay) publish(ctx context.Context, id string, env Envelope) error {
select {
case <-ctx.Done():
// this will be called when we get an OK or when the context has been canceled
if gotOk {
return err
}
return ctx.Err()
case <-r.connectionContext.Done():
// this is caused when we lose connectivity