fix FetchSpecificEvent() behavior for the faster no-relays case.

This commit is contained in:
fiatjaf
2024-10-14 14:46:59 -03:00
parent 9e744e1eb0
commit 7ede46661c

View File

@@ -74,30 +74,36 @@ func (sys *System) FetchSpecificEvent(
} }
var result *nostr.Event var result *nostr.Event
fetchProfileOnce := sync.Once{}
attempts:
for _, attempt := range []struct { for _, attempt := range []struct {
label string label string
relays []string relays []string
nonUnique bool slowWithRelays bool
}{ }{
{ {
label: "fetch-" + prefix, label: "fetch-" + prefix,
relays: relays, relays: relays,
// set this to true if the caller wants relays, so we won't return immediately // set this to true if the caller wants relays, so we won't return immediately
// but will instead wait a little while to see if more relays respond // but will instead wait a little while to see if more relays respond
nonUnique: withRelays, slowWithRelays: withRelays,
}, },
{ {
label: "fetchf-" + prefix, label: "fetchf-" + prefix,
relays: fallback, relays: fallback,
nonUnique: false, slowWithRelays: false,
}, },
} { } {
// actually fetch the event here // actually fetch the event here
countdown := 6.0 countdown := 6.0
subManyCtx := ctx subManyCtx := ctx
subMany := sys.Pool.SubManyEose
if attempt.slowWithRelays {
subMany = sys.Pool.SubManyEoseNonUnique
}
if attempt.nonUnique { if attempt.slowWithRelays {
// keep track of where we have actually found the event so we can show that // keep track of where we have actually found the event so we can show that
var cancel context.CancelFunc var cancel context.CancelFunc
subManyCtx, cancel = context.WithTimeout(ctx, time.Second*6) subManyCtx, cancel = context.WithTimeout(ctx, time.Second*6)
@@ -115,9 +121,7 @@ func (sys *System) FetchSpecificEvent(
}() }()
} }
fetchProfileOnce := sync.Once{} for ie := range subMany(
for ie := range sys.Pool.SubManyEoseNonUnique(
subManyCtx, subManyCtx,
attempt.relays, attempt.relays,
nostr.Filters{filter}, nostr.Filters{filter},
@@ -131,6 +135,11 @@ func (sys *System) FetchSpecificEvent(
if result == nil || ie.CreatedAt > result.CreatedAt { if result == nil || ie.CreatedAt > result.CreatedAt {
result = ie.Event result = ie.Event
} }
if !attempt.slowWithRelays {
break attempts
}
countdown = min(countdown-0.5, 1) countdown = min(countdown-0.5, 1)
} }
} }