Skip to content

Commit

Permalink
Use simple channel select to download index file
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed May 4, 2022
1 parent 58f3dbe commit 78cdfd0
Showing 1 changed file with 55 additions and 56 deletions.
111 changes: 55 additions & 56 deletions protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1642,82 +1642,81 @@ func (m *Manager) DownloadHistoryArchivesByMagnetlink(communityID types.HexBytes

ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ticker.C:
if indexFile.BytesCompleted() == indexFile.Length() {
index, err := m.LoadHistoryArchiveIndexFromFile(communityID)
if err != nil {
return nil, err
}
<-ticker.C
if indexFile.BytesCompleted() == indexFile.Length() {
index, err := m.LoadHistoryArchiveIndexFromFile(communityID)
if err != nil {
return nil, err
}

var archiveIDs []string
var archiveIDs []string

archiveHashes := make(archiveMDSlice, 0, len(index.Archives))
archiveHashes := make(archiveMDSlice, 0, len(index.Archives))

for hash, metadata := range index.Archives {
archiveHashes = append(archiveHashes, &archiveMetadata{hash: hash, from: metadata.Metadata.From})
}
for hash, metadata := range index.Archives {
archiveHashes = append(archiveHashes, &archiveMetadata{hash: hash, from: metadata.Metadata.From})
}

sort.Sort(archiveHashes)
sort.Sort(archiveHashes)

for _, hd := range archiveHashes {
hash := hd.hash
metadata := index.Archives[hash]
hasArchive, err := m.persistence.HasMessageArchiveID(communityID, hash)
if err != nil {
m.logger.Debug("failed to check if has message archive id", zap.Error(err))
continue
}
if hasArchive {
continue
}
for _, hd := range archiveHashes {
hash := hd.hash
metadata := index.Archives[hash]
hasArchive, err := m.persistence.HasMessageArchiveID(communityID, hash)
if err != nil {
m.logger.Debug("failed to check if has message archive id", zap.Error(err))
continue
}
if hasArchive {
continue
}

startIndex := int(metadata.Offset) / pieceLength
endIndex := startIndex + int(metadata.Size)/pieceLength
startIndex := int(metadata.Offset) / pieceLength
endIndex := startIndex + int(metadata.Size)/pieceLength

m.archiveLogger.Debug("downloading data for message archive", zap.String("hash", hash))
m.archiveLogger.Debug("pieces (start, end)", zap.Any("startIndex", startIndex), zap.Any("endIndex", endIndex-1))
torrent.DownloadPieces(startIndex, endIndex)
m.archiveLogger.Debug("downloading data for message archive", zap.String("hash", hash))
m.archiveLogger.Debug("pieces (start, end)", zap.Any("startIndex", startIndex), zap.Any("endIndex", endIndex-1))
torrent.DownloadPieces(startIndex, endIndex)

psc := torrent.SubscribePieceStateChanges()
psc := torrent.SubscribePieceStateChanges()
for {
i := startIndex
done := false
for {
i := startIndex
done := false
for {
if i > endIndex-1 {
break
}
done = torrent.PieceState(i).Complete
i++
}
if done {
psc.Close()
if i > endIndex-1 {
break
}
<-psc.Values
done = torrent.PieceState(i).Complete
i++
}
archiveIDs = append(archiveIDs, hash)
err = m.persistence.SaveMessageArchiveID(communityID, hash)
if err != nil {
m.logger.Debug("couldn't save message archive ID", zap.Error(err))
continue
if done {
psc.Close()
break
}
m.publish(&Subscription{
HistoryArchiveDownloadedSignal: &signal.HistoryArchiveDownloadedSignal{
CommunityID: communityID.String(),
From: int(metadata.Metadata.From),
To: int(metadata.Metadata.To),
},
})
<-psc.Values
}
archiveIDs = append(archiveIDs, hash)
err = m.persistence.SaveMessageArchiveID(communityID, hash)
if err != nil {
m.logger.Debug("couldn't save message archive ID", zap.Error(err))
continue
}
m.publish(&Subscription{
HistoryArchivesSeedingSignal: &signal.HistoryArchivesSeedingSignal{
HistoryArchiveDownloadedSignal: &signal.HistoryArchiveDownloadedSignal{
CommunityID: communityID.String(),
From: int(metadata.Metadata.From),
To: int(metadata.Metadata.To),
},
})
return archiveIDs, nil
}
m.publish(&Subscription{
HistoryArchivesSeedingSignal: &signal.HistoryArchivesSeedingSignal{
CommunityID: communityID.String(),
},
})
return archiveIDs, nil
}
}
}
Expand Down

0 comments on commit 78cdfd0

Please sign in to comment.