feat(communities): retry downloading archive data

When fetching torrent info after receiving a magnet link,
it can happen that the request times out.

We want to retry downloading the data again at least once more
before giving up
This commit is contained in:
Pascal Precht 2022-10-07 12:24:50 +02:00 committed by r4bbit.eth
parent dd49c4c954
commit 69e84b5673
2 changed files with 15 additions and 4 deletions

View File

@ -36,6 +36,8 @@ var defaultAnnounceList = [][]string{
} }
var pieceLength = 100 * 1024 var pieceLength = 100 * 1024
var ErrTorrentTimedout = errors.New("torrent has timed out")
type Manager struct { type Manager struct {
persistence *Persistence persistence *Persistence
ensSubscription chan []*ens.VerificationRecord ensSubscription chan []*ens.VerificationRecord
@ -1929,7 +1931,7 @@ func (m *Manager) DownloadHistoryArchivesByMagnetlink(communityID types.HexBytes
m.LogStdout("fetching torrent info", zap.String("magnetlink", magnetlink)) m.LogStdout("fetching torrent info", zap.String("magnetlink", magnetlink))
select { select {
case <-timeout: case <-timeout:
return nil, errors.New("torrent has timed out") return nil, ErrTorrentTimedout
case <-torrent.GotInfo(): case <-torrent.GotInfo():
files := torrent.Files() files := torrent.Files()

View File

@ -925,9 +925,18 @@ func (m *Messenger) HandleHistoryArchiveMagnetlinkMessage(state *ReceivedMessage
go func() { go func() {
downloadedArchiveIDs, err := m.communitiesManager.DownloadHistoryArchivesByMagnetlink(id, magnetlink) downloadedArchiveIDs, err := m.communitiesManager.DownloadHistoryArchivesByMagnetlink(id, magnetlink)
if err != nil { if err != nil {
log.Println("failed to download history archive data", err) logMsg := "failed to download history archive data"
m.logger.Debug("failed to download history archive data", zap.Error(err)) if err == communities.ErrTorrentTimedout {
return m.communitiesManager.LogStdout("torrent has timed out, trying once more...")
downloadedArchiveIDs, err = m.communitiesManager.DownloadHistoryArchivesByMagnetlink(id, magnetlink)
if err != nil {
m.communitiesManager.LogStdout(logMsg, zap.Error(err))
return
}
} else {
m.communitiesManager.LogStdout(logMsg, zap.Error(err))
return
}
} }
messagesToHandle, err := m.communitiesManager.ExtractMessagesFromHistoryArchives(id, downloadedArchiveIDs) messagesToHandle, err := m.communitiesManager.ExtractMessagesFromHistoryArchives(id, downloadedArchiveIDs)