Skip to content

Commit

Permalink
fix FetchSpecificEvent() behavior for the faster no-relays case.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 14, 2024
1 parent 9e744e1 commit 7ede466
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions sdk/specific_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,36 @@ func (sys *System) FetchSpecificEvent(
}

var result *nostr.Event
fetchProfileOnce := sync.Once{}

attempts:
for _, attempt := range []struct {
label string
relays []string
nonUnique bool
label string
relays []string
slowWithRelays bool
}{
{
label: "fetch-" + prefix,
relays: relays,
// 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
nonUnique: withRelays,
slowWithRelays: withRelays,
},
{
label: "fetchf-" + prefix,
relays: fallback,
nonUnique: false,
label: "fetchf-" + prefix,
relays: fallback,
slowWithRelays: false,
},
} {
// actually fetch the event here
countdown := 6.0
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
var cancel context.CancelFunc
subManyCtx, cancel = context.WithTimeout(ctx, time.Second*6)
Expand All @@ -115,9 +121,7 @@ func (sys *System) FetchSpecificEvent(
}()
}

fetchProfileOnce := sync.Once{}

for ie := range sys.Pool.SubManyEoseNonUnique(
for ie := range subMany(
subManyCtx,
attempt.relays,
nostr.Filters{filter},
Expand All @@ -131,6 +135,11 @@ func (sys *System) FetchSpecificEvent(
if result == nil || ie.CreatedAt > result.CreatedAt {
result = ie.Event
}

if !attempt.slowWithRelays {
break attempts
}

countdown = min(countdown-0.5, 1)
}
}
Expand Down

0 comments on commit 7ede466

Please sign in to comment.