fix_: simplify `performStorenodeTask`

This commit is contained in:
Richard Ramos 2024-10-21 17:13:30 -04:00
parent c207997071
commit 62d4ce9b7e
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
5 changed files with 25 additions and 31 deletions

View File

@ -90,35 +90,25 @@ func (m *Messenger) scheduleSyncChat(chat *Chat) (bool, error) {
} }
func (m *Messenger) performStorenodeTask(task func() (*MessengerResponse, error), opts ...history.StorenodeTaskOption) (*MessengerResponse, error) { func (m *Messenger) performStorenodeTask(task func() (*MessengerResponse, error), opts ...history.StorenodeTaskOption) (*MessengerResponse, error) {
responseCh := make(chan *MessengerResponse) responseCh := make(chan *MessengerResponse, 1)
errCh := make(chan error) err := m.transport.PerformStorenodeTask(func() error {
r, err := task()
go func() {
defer gocommon.LogOnPanic()
err := m.transport.PerformStorenodeTask(func() error {
r, err := task()
if err != nil {
return err
}
select {
case <-m.ctx.Done():
return m.ctx.Err()
case responseCh <- r:
default:
//
}
return nil
}, opts...)
if err != nil { if err != nil {
errCh <- err return err
} }
}()
select {
case <-m.ctx.Done():
return m.ctx.Err()
case responseCh <- r:
return nil
}
}, opts...)
if err != nil {
return nil, err
}
select { select {
case err := <-errCh:
return nil, err
case r := <-responseCh: case r := <-responseCh:
if r != nil { if r != nil {
return r, nil return r, nil
@ -784,7 +774,7 @@ func (m *Messenger) SyncChatFromSyncedFrom(chatID string) (uint32, error) {
chat.SyncedFrom = uint32(batch.From.Unix()) chat.SyncedFrom = uint32(batch.From.Unix())
} }
m.logger.Debug("setting sync timestamps", zap.Int64("from", int64(batch.From.Unix())), zap.Int64("to", int64(chat.SyncedTo)), zap.String("chatID", chatID)) m.logger.Debug("setting sync timestamps", zap.Int64("from", batch.From.Unix()), zap.Int64("to", int64(chat.SyncedTo)), zap.String("chatID", chatID))
err = m.persistence.SetSyncTimestamps(uint32(batch.From.Unix()), chat.SyncedTo, chat.ID) err = m.persistence.SetSyncTimestamps(uint32(batch.From.Unix()), chat.SyncedTo, chat.ID)
from = uint32(batch.From.Unix()) from = uint32(batch.From.Unix())

View File

@ -156,6 +156,8 @@ func (m *Messenger) Storenodes() ([]peer.ID, error) {
} }
func (m *Messenger) checkForStorenodeCycleSignals() { func (m *Messenger) checkForStorenodeCycleSignals() {
defer gocommon.LogOnPanic()
if m.transport.WakuVersion() != 2 { if m.transport.WakuVersion() != 2 {
return return
} }

View File

@ -149,7 +149,7 @@ func (s *MessengerStoreNodeCommunitySuite) newMessenger(name string, storenodeAd
} }
func (s *MessengerStoreNodeCommunitySuite) createCommunityWithChat(m *Messenger) (*communities.Community, *Chat) { func (s *MessengerStoreNodeCommunitySuite) createCommunityWithChat(m *Messenger) (*communities.Community, *Chat) {
WaitForAvailableStoreNode(&s.Suite, m, 500*time.Millisecond) WaitForAvailableStoreNode(&s.Suite, m, context.TODO())
storeNodeSubscription := s.setupStoreNodeEnvelopesWatcher(nil) storeNodeSubscription := s.setupStoreNodeEnvelopesWatcher(nil)
@ -197,7 +197,7 @@ func (s *MessengerStoreNodeCommunitySuite) fetchCommunity(m *Messenger, communit
WithWaitForResponseOption(true), WithWaitForResponseOption(true),
} }
fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(communityShard, options) fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityShard, options)
s.Require().NoError(err) s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity) s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity)

View File

@ -290,7 +290,7 @@ func (s *MessengerStoreNodeRequestSuite) fetchCommunity(m *Messenger, communityS
WithWaitForResponseOption(true), WithWaitForResponseOption(true),
} }
fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(communityShard, options) fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityShard, options)
s.Require().NoError(err) s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity) s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity)
@ -808,7 +808,7 @@ func (s *MessengerStoreNodeRequestSuite) TestRequestCommunityEnvelopesOrder() {
} }
// Fetch the community // Fetch the community
fetchedCommunity, _, err := s.bob.storeNodeRequestsManager.FetchCommunity(community.CommunityShard(), options) fetchedCommunity, _, err := s.bob.storeNodeRequestsManager.FetchCommunity(context.TODO(), community.CommunityShard(), options)
s.Require().NoError(err) s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, community) s.requireCommunitiesEqual(fetchedCommunity, community)
@ -1162,7 +1162,7 @@ func (s *MessengerStoreNodeRequestSuite) TestFetchRealCommunity() {
} }
storeNodeRequestOptions = append(storeNodeRequestOptions, exampleToRun.CustomOptions...) storeNodeRequestOptions = append(storeNodeRequestOptions, exampleToRun.CustomOptions...)
fetchedCommunity, stats, err := user.storeNodeRequestsManager.FetchCommunity(communityAddress, storeNodeRequestOptions) fetchedCommunity, stats, err := user.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityAddress, storeNodeRequestOptions)
result.EnvelopesCount = stats.FetchedEnvelopesCount result.EnvelopesCount = stats.FetchedEnvelopesCount
result.FetchedCommunity = fetchedCommunity result.FetchedCommunity = fetchedCommunity

View File

@ -365,6 +365,8 @@ func SetIdentityImagesAndWaitForChange(s *suite.Suite, messenger *Messenger, tim
} }
func WaitForAvailableStoreNode(s *suite.Suite, m *Messenger, ctx context.Context) { func WaitForAvailableStoreNode(s *suite.Suite, m *Messenger, ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
available := m.transport.WaitForAvailableStoreNode(ctx) available := m.transport.WaitForAvailableStoreNode(ctx)
s.Require().True(available) s.Require().True(available)
} }