Skip to content

Commit

Permalink
Cache responses from purple pages when relays are not found
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 15, 2023
1 parent f735d3e commit 4a5020a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
41 changes: 26 additions & 15 deletions service/adapters/purple_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import (

var (
ErrRelayListNotFoundInPurplePages = errors.New("relay list not found in purple pages")
ErrPurplePagesTimeout = errors.New("purple pages lookup timed out")

errLookupFoundNoEvents = errors.New("lookup found no events")
)

var purplePagesAddress = domain.MustNewRelayAddress("wss://purplepag.es")

const purplePagesLookupTimeout = 10 * time.Second

const numLookups = 2

type PurplePages struct {
logger logging.Logger
metrics app.Metrics
Expand Down Expand Up @@ -79,26 +82,34 @@ func (p *PurplePages) GetRelays(ctx context.Context, publicKey domain.PublicKey)

results := internal.NewEmptySet[domain.RelayAddress]()

var compoundError error
errorsCounter := 0
var compoundError *multierror.Error

for i := 0; i < 2; i++ {
for i := 0; i < numLookups; i++ {
result := <-ch

if err := result.Err; err != nil {
if errors.Is(err, ErrPurplePagesTimeout) {
return nil, errors.Wrap(err, "one of the lookups timed out")
if !errors.Is(err, errLookupFoundNoEvents) {
return nil, errors.Wrap(err, "one of the lookups failed")
}

compoundError = multierror.Append(compoundError, err)
errorsCounter++
continue
}
results.PutMany(result.Addresses)
}

if errorsCounter == 2 {
return nil, compoundError
// if any of the lookups failed with err != errLookupFoundNoEvents return compoundErr
// if all of the lookups failed with err == errLookupFoundNoEvents return ErrRelayListNotFoundInPurplePages
if compoundError != nil {
errLookupFoundNoEventsCounter := 0
for _, componentErr := range compoundError.Errors {
if !errors.Is(componentErr, errLookupFoundNoEvents) {
return nil, errors.Wrap(compoundError, "some lookups failed")
}
errLookupFoundNoEventsCounter++
}

if errLookupFoundNoEventsCounter == numLookups {
return nil, ErrRelayListNotFoundInPurplePages
}
}

return results.List(), nil
Expand All @@ -117,7 +128,7 @@ func (p *PurplePages) getRelaysFromRelayMetadata(ctx context.Context, publicKey
nil,
) {
if eventOrEOSE.EOSE() {
return nil, ErrRelayListNotFoundInPurplePages
return nil, errLookupFoundNoEvents
}

event := eventOrEOSE.Event()
Expand All @@ -140,7 +151,7 @@ func (p *PurplePages) getRelaysFromRelayMetadata(ctx context.Context, publicKey
}
}

return nil, ErrPurplePagesTimeout
return nil, errors.New("timeout")
}

func (p *PurplePages) getRelaysFromContacts(ctx context.Context, publicKey domain.PublicKey) ([]domain.RelayAddress, error) {
Expand All @@ -156,7 +167,7 @@ func (p *PurplePages) getRelaysFromContacts(ctx context.Context, publicKey domai
nil,
) {
if eventOrEOSE.EOSE() {
return nil, ErrRelayListNotFoundInPurplePages
return nil, errLookupFoundNoEvents
}

event := eventOrEOSE.Event()
Expand All @@ -173,7 +184,7 @@ func (p *PurplePages) getRelaysFromContacts(ctx context.Context, publicKey domai
}
}

return nil, ErrPurplePagesTimeout
return nil, errors.New("timeout")
}

type relaysOrError struct {
Expand Down
22 changes: 14 additions & 8 deletions service/adapters/relay_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (p RelaySource) GetRelays(ctx context.Context, publicKey domain.PublicKey)
result.Put(relayAddress)
}

relayAddressesFromPurplePages, err := p.getRelaysFromPurplePages(ctx, publicKey)
relayAddressesFromPurplePages, err := p.getRelaysFromPurplePagesOrCache(ctx, publicKey)
if err != nil {
return nil, errors.Wrap(err, "error getting relays from purple pages")
}
Expand All @@ -52,7 +52,7 @@ func (p RelaySource) GetRelays(ctx context.Context, publicKey domain.PublicKey)
return result.List(), nil
}

func (p RelaySource) getRelaysFromPurplePages(ctx context.Context, publicKey domain.PublicKey) ([]domain.RelayAddress, error) {
func (p RelaySource) getRelaysFromPurplePagesOrCache(ctx context.Context, publicKey domain.PublicKey) ([]domain.RelayAddress, error) {
var previousEntries []domain.RelayAddress

entry, ok := p.cache.Get(publicKey)
Expand All @@ -63,18 +63,24 @@ func (p RelaySource) getRelaysFromPurplePages(ctx context.Context, publicKey dom
}
}

relayAddressesFromPurplePages, err := p.purplePages.GetRelays(ctx, publicKey)
relayAddressesFromPurplePages, err := p.getRelaysFromPurplePages(ctx, publicKey)
if err != nil {
if errors.Is(err, ErrRelayListNotFoundInPurplePages) ||
errors.Is(err, ErrPurplePagesTimeout) {
p.logger.Debug().WithError(err).Message("known error from purple pages")
return previousEntries, nil
}
return nil, errors.Wrap(err, "error querying purple pages")
}

p.cache.Set(publicKey, relayAddressesFromPurplePages)
return relayAddressesFromPurplePages, nil
}

func (p RelaySource) getRelaysFromPurplePages(ctx context.Context, publicKey domain.PublicKey) ([]domain.RelayAddress, error) {
relayAddressesFromPurplePages, err := p.purplePages.GetRelays(ctx, publicKey)
if err != nil {
if errors.Is(err, ErrRelayListNotFoundInPurplePages) {
p.logger.Debug().WithError(err).Message("relay list not found in purple pages")
return nil, nil
}
return nil, errors.Wrap(err, "error querying purple pages")
}
return relayAddressesFromPurplePages, nil
}

Expand Down

0 comments on commit 4a5020a

Please sign in to comment.