From da3df63eb19a0a267caf67e6fcdf2d5a8291b888 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Fri, 6 Oct 2023 17:30:22 +0100 Subject: [PATCH] chore: improve requesting contact/community info from mailserver (#4110) --- protocol/messenger_communities.go | 31 +++++++------------------------ protocol/messenger_contacts.go | 25 +++++++++---------------- 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 03b60ab9d..e6ac2a3d0 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -2443,11 +2443,12 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitF m.requestedCommunities[communityID] = nil } + defer m.forgetCommunityRequest(communityID) + to := uint32(m.transport.GetCurrentTime() / 1000) from := to - oneMonthInSeconds _, err = m.performMailserverRequest(func() (*MessengerResponse, error) { - batch := MailserverBatch{From: from, To: to, Topics: []types.TopicType{filter.ContentTopic}} m.logger.Info("Requesting historic") err := m.processMailserverBatch(batch) @@ -2461,43 +2462,25 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitF return nil, nil } - ctx := context.Background() - ctx, cancel := context.WithTimeout(ctx, 15*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() - var community *communities.Community - fetching := true - - for fetching { + for { select { case <-time.After(200 * time.Millisecond): //send signal to client that message status updated - community, err = m.communitiesManager.GetByIDString(communityID) + community, err := m.communitiesManager.GetByIDString(communityID) if err != nil { return nil, err } - if community != nil && community.Name() != "" && community.DescriptionText() != "" { - fetching = false + return community, nil } case <-ctx.Done(): - fetching = false + return nil, fmt.Errorf("failed to request community info for id '%s' from mailserver: %w", communityID, ctx.Err()) } } - - if community == nil { - return nil, nil - } - - //if there is no info helpful for client, we don't post it - if community.Name() == "" && community.DescriptionText() == "" { - return nil, nil - } - - m.forgetCommunityRequest(communityID) - - return community, nil } // RequestCommunityInfoFromMailserver installs filter for community and requests its details diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index 86caf36bc..4b738abc2 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -1221,32 +1221,25 @@ func (m *Messenger) RequestContactInfoFromMailserver(pubkey string, waitForRespo return nil, nil } - ctx := context.Background() - ctx, cancel := context.WithTimeout(ctx, 15*time.Second) - defer cancel() + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - var contact *Contact - fetching := true + defer func() { + cancel() + m.forgetContactInfoRequest(pubkey) + }() - for fetching { + for { select { case <-time.After(200 * time.Millisecond): - var ok bool - contact, ok = m.allContacts.Load(pubkey) - + contact, ok := m.allContacts.Load(pubkey) if ok && contact != nil && contact.DisplayName != "" { - fetching = false - m.logger.Info("contact info received", zap.String("pubkey", contact.ID)) + return contact, nil } case <-ctx.Done(): - fetching = false + return nil, fmt.Errorf("failed to request contact info from mailserver: %w", ctx.Err()) } } - - m.forgetContactInfoRequest(pubkey) - - return contact, nil } func (m *Messenger) requestContactInfoFromMailserver(pubkey string) error {