mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 14:23:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
|
||||
"github.com/GopeedLab/gopeed/pkg/util"
|
||||
"github.com/RoaringBitmap/roaring"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
@@ -162,20 +163,209 @@ func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||
return f.meta
|
||||
}
|
||||
|
||||
func (f *Fetcher) Stats() any {
|
||||
func (f *Fetcher) Stats() *fetcher.Stats {
|
||||
var stats torrent.TorrentStats
|
||||
var pieceMap *base.PieceMap
|
||||
pieceMapReady := false
|
||||
peers := make([]*base.PeerStats, 0)
|
||||
if f.torrent != nil {
|
||||
stats = f.torrent.Stats()
|
||||
pieceMap, pieceMapReady = buildBTPieceMap(f.torrent)
|
||||
peers = buildBTPeers(f.torrent)
|
||||
} else {
|
||||
stats = torrent.TorrentStats{}
|
||||
}
|
||||
return &bt.Stats{
|
||||
TotalPeers: stats.TotalPeers,
|
||||
ActivePeers: stats.ActivePeers,
|
||||
ConnectedSeeders: stats.ConnectedSeeders,
|
||||
SeedBytes: f.data.SeedBytes,
|
||||
SeedRatio: f.seedRadio(),
|
||||
SeedTime: f.data.SeedTime,
|
||||
var snapshot any
|
||||
if pieceMapReady {
|
||||
snapshot = &bt.StatsSnapshot{
|
||||
SeedBytes: f.data.SeedBytes,
|
||||
SeedRatio: f.seedRadio(),
|
||||
SeedTime: f.data.SeedTime,
|
||||
PieceMap: pieceMap.Clone(),
|
||||
}
|
||||
}
|
||||
// When completion is temporarily unknown while anacrolix restores storage
|
||||
// or verifies pieces, the nil Snapshot tells Downloader to retain the last
|
||||
// trusted bitset instead of replacing it with an all-empty map.
|
||||
return &fetcher.Stats{
|
||||
Snapshot: snapshot,
|
||||
Runtime: &bt.StatsRuntime{
|
||||
TotalPeers: stats.TotalPeers,
|
||||
ActivePeers: stats.ActivePeers,
|
||||
ConnectedSeeders: stats.ConnectedSeeders,
|
||||
ConnectedLeechers: stats.ActivePeers - stats.ConnectedSeeders,
|
||||
Peers: peers,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// buildBTPieceMap converts anacrolix's ordered completion runs to Gopeed's
|
||||
// one-bit map. ready is false until storage knows the completion state of every
|
||||
// piece; this prevents initial hash checking from briefly replacing a restored
|
||||
// map with an all-empty value.
|
||||
func buildBTPieceMap(t *torrent.Torrent) (pieceMap *base.PieceMap, ready bool) {
|
||||
info := t.Info()
|
||||
if info == nil {
|
||||
return nil, false
|
||||
}
|
||||
pieceMap = base.NewPieceMap(info.NumPieces(), info.PieceLength)
|
||||
ready = applyBTPieceRuns(pieceMap, t.PieceStateRuns())
|
||||
return pieceMap, ready
|
||||
}
|
||||
|
||||
func applyBTPieceRuns(pieceMap *base.PieceMap, runs []torrent.PieceStateRun) (ready bool) {
|
||||
ready = true
|
||||
pieceIndex := 0
|
||||
for _, run := range runs {
|
||||
if !run.Ok {
|
||||
ready = false
|
||||
}
|
||||
completed := run.Ok && run.Complete
|
||||
for range run.Length {
|
||||
if pieceIndex >= pieceMap.PieceCount() {
|
||||
return ready
|
||||
}
|
||||
_ = pieceMap.Update(pieceIndex, completed)
|
||||
pieceIndex++
|
||||
}
|
||||
}
|
||||
return ready && pieceIndex == pieceMap.PieceCount()
|
||||
}
|
||||
|
||||
func buildBTPeers(t *torrent.Torrent) []*base.PeerStats {
|
||||
connections := t.PeerConns()
|
||||
totalPieces := 0
|
||||
var missingWantedPieces *roaring.Bitmap
|
||||
// Magnet torrents can connect to peers before their metadata arrives. Keep
|
||||
// those runtime peer stats, but leave completion and relevance unavailable
|
||||
// until the metadata provides a trusted total piece count.
|
||||
if info := t.Info(); info != nil {
|
||||
totalPieces = info.NumPieces()
|
||||
missingWantedPieces = btMissingWantedPieces(t.PieceStateRuns(), totalPieces)
|
||||
}
|
||||
peers := make([]*base.PeerStats, 0, len(connections))
|
||||
for _, connection := range connections {
|
||||
peerStats := connection.Stats()
|
||||
peers = append(peers, &base.PeerStats{
|
||||
Address: safeBTPeerAddress(connection),
|
||||
Client: btPeerClient(connection),
|
||||
DownloadSpeed: int64(peerStats.DownloadRate),
|
||||
UploadSpeed: int64(peerStats.LastWriteUploadRate),
|
||||
PieceCount: peerStats.RemotePieceCount,
|
||||
Completion: btPeerCompletion(peerStats.RemotePieceCount, totalPieces),
|
||||
Relevance: btPeerRelevance(connection.PeerPieces(), missingWantedPieces),
|
||||
Source: normalizeBTPeerSource(connection.Discovery),
|
||||
Transport: normalizeBTTransport(connection.Network),
|
||||
})
|
||||
}
|
||||
return peers
|
||||
}
|
||||
|
||||
// btPeerCompletion converts anacrolix's remote piece count into the ratio the
|
||||
// API exposes. A nil value distinguishes unavailable metadata from a real 0%.
|
||||
func btPeerCompletion(pieceCount, totalPieces int) *float64 {
|
||||
if totalPieces <= 0 {
|
||||
return nil
|
||||
}
|
||||
if pieceCount < 0 {
|
||||
pieceCount = 0
|
||||
} else if pieceCount > totalPieces {
|
||||
pieceCount = totalPieces
|
||||
}
|
||||
completion := float64(pieceCount) / float64(totalPieces)
|
||||
return &completion
|
||||
}
|
||||
|
||||
// btMissingWantedPieces builds the denominator used by peer relevance. Pieces
|
||||
// belonging only to skipped files are excluded because their priority is none.
|
||||
func btMissingWantedPieces(runs []torrent.PieceStateRun, totalPieces int) *roaring.Bitmap {
|
||||
if totalPieces <= 0 {
|
||||
return nil
|
||||
}
|
||||
missing := roaring.New()
|
||||
pieceIndex := 0
|
||||
for _, run := range runs {
|
||||
for range run.Length {
|
||||
if pieceIndex >= totalPieces {
|
||||
return nil
|
||||
}
|
||||
if run.Priority != torrent.PiecePriorityNone && !(run.Ok && run.Complete) {
|
||||
missing.Add(uint32(pieceIndex))
|
||||
}
|
||||
pieceIndex++
|
||||
}
|
||||
}
|
||||
if pieceIndex != totalPieces {
|
||||
return nil
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
// btPeerRelevance reports how much of the local wanted, missing set the remote
|
||||
// peer can provide. A zero value is meaningful; nil means there is no denominator.
|
||||
func btPeerRelevance(peerPieces, missingWantedPieces *roaring.Bitmap) *float64 {
|
||||
if peerPieces == nil || missingWantedPieces == nil || missingWantedPieces.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
relevantPieces := roaring.And(peerPieces, missingWantedPieces).GetCardinality()
|
||||
relevance := float64(relevantPieces) / float64(missingWantedPieces.GetCardinality())
|
||||
return &relevance
|
||||
}
|
||||
|
||||
// A uTP socket may close between PeerConns and this snapshot. Some uTP address
|
||||
// implementations panic after close, so one disappearing address must not make
|
||||
// the entire stats endpoint fail.
|
||||
func safeBTPeerAddress(connection *torrent.PeerConn) (address string) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
address = ""
|
||||
}
|
||||
}()
|
||||
if connection.RemoteAddr == nil {
|
||||
return ""
|
||||
}
|
||||
return connection.RemoteAddr.String()
|
||||
}
|
||||
|
||||
func btPeerClient(connection *torrent.PeerConn) string {
|
||||
value := connection.PeerClientName.Load()
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
|
||||
func normalizeBTPeerSource(source torrent.PeerSource) string {
|
||||
switch source {
|
||||
case torrent.PeerSourceTracker:
|
||||
return "tracker"
|
||||
case torrent.PeerSourceDhtGetPeers, torrent.PeerSourceDhtAnnouncePeer:
|
||||
return "dht"
|
||||
case torrent.PeerSourcePex:
|
||||
return "pex"
|
||||
case torrent.PeerSourceIncoming:
|
||||
return "incoming"
|
||||
case torrent.PeerSourceDirect:
|
||||
return "direct"
|
||||
case torrent.PeerSourceUtHolepunch:
|
||||
return "holepunch"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeBTTransport(network string) string {
|
||||
network = strings.ToLower(network)
|
||||
switch {
|
||||
case strings.Contains(network, "webrtc"):
|
||||
return "webrtc"
|
||||
case strings.Contains(network, "udp"):
|
||||
// anacrolix labels uTP sockets with their underlying udp network.
|
||||
return "utp"
|
||||
case strings.Contains(network, "tcp"):
|
||||
return "tcp"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
package bt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
protocolbt "github.com/GopeedLab/gopeed/pkg/protocol/bt"
|
||||
"github.com/RoaringBitmap/roaring"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
func TestStatsSeparatesSnapshotAndRuntime(t *testing.T) {
|
||||
frame := (&Fetcher{
|
||||
meta: &fetcher.FetcherMeta{Res: &base.Resource{Size: 256}},
|
||||
data: &fetcherData{SeedBytes: 128, SeedTime: 64},
|
||||
}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil until piece completion is known", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*protocolbt.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if len(runtime.Peers) != 0 || runtime.TotalPeers != 0 {
|
||||
t.Fatalf("unexpected runtime: %+v", runtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsBeforeTorrentMetadataIsAvailable(t *testing.T) {
|
||||
config := torrent.TestingConfig(t)
|
||||
config.DisableTCP = true
|
||||
config.DisableUTP = true
|
||||
client, err := torrent.NewClient(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
torrentWithoutMetadata, err := client.AddMagnet(
|
||||
"magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info := torrentWithoutMetadata.Info(); info != nil {
|
||||
t.Fatalf("torrent info = %+v, want nil before metadata is available", info)
|
||||
}
|
||||
|
||||
frame := (&Fetcher{
|
||||
torrent: torrentWithoutMetadata,
|
||||
meta: &fetcher.FetcherMeta{},
|
||||
data: &fetcherData{},
|
||||
}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil before metadata is available", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*protocolbt.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if len(runtime.Peers) != 0 || runtime.TotalPeers != 0 {
|
||||
t.Fatalf("unexpected runtime before metadata is available: %+v", runtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyBTPieceRunsPublishesVerifiedCompletionOnly(t *testing.T) {
|
||||
pieceMap := base.NewPieceMap(4, 1024)
|
||||
ready := applyBTPieceRuns(pieceMap, []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, false), Partial: true}, Length: 2},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(false, false), Hashing: true}, Length: 1},
|
||||
})
|
||||
if ready {
|
||||
t.Fatal("piece map became ready while one completion is unknown")
|
||||
}
|
||||
want := []bool{true, false, false, false}
|
||||
for index, expected := range want {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != expected {
|
||||
t.Fatalf("piece %d = %v, %v; want %v", index, got, err, expected)
|
||||
}
|
||||
}
|
||||
|
||||
knownMap := base.NewPieceMap(2, 1024)
|
||||
if !applyBTPieceRuns(knownMap, []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, false)}, Length: 1},
|
||||
}) {
|
||||
t.Fatal("fully known completion map was not ready")
|
||||
}
|
||||
if applyBTPieceRuns(base.NewPieceMap(2, 1024), []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
}) {
|
||||
t.Fatal("incomplete piece runs were published")
|
||||
}
|
||||
}
|
||||
|
||||
func torrentCompletion(ok, complete bool) storage.Completion {
|
||||
return storage.Completion{Ok: ok, Complete: complete}
|
||||
}
|
||||
|
||||
func TestNormalizeBTPeerMetadata(t *testing.T) {
|
||||
if got := normalizeBTPeerSource(torrent.PeerSourceDhtGetPeers); got != "dht" {
|
||||
t.Fatalf("DHT source = %q", got)
|
||||
}
|
||||
if got := normalizeBTPeerSource(torrent.PeerSourcePex); got != "pex" {
|
||||
t.Fatalf("PEX source = %q", got)
|
||||
}
|
||||
transports := map[string]string{
|
||||
"udp4": "utp",
|
||||
"udp6": "utp",
|
||||
"tcp4": "tcp",
|
||||
"tcp6": "tcp",
|
||||
"webrtc": "webrtc",
|
||||
"unix": "unknown",
|
||||
}
|
||||
for network, want := range transports {
|
||||
if got := normalizeBTTransport(network); got != want {
|
||||
t.Errorf("normalizeBTTransport(%q) = %q, want %q", network, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerCompletion(t *testing.T) {
|
||||
if got := btPeerCompletion(1, 4); got == nil || *got != 0.25 {
|
||||
t.Fatalf("completion = %v, want 0.25", got)
|
||||
}
|
||||
if got := btPeerCompletion(5, 4); got == nil || *got != 1 {
|
||||
t.Fatalf("clamped completion = %v, want 1", got)
|
||||
}
|
||||
if got := btPeerCompletion(-1, 4); got == nil || *got != 0 {
|
||||
t.Fatalf("negative completion = %v, want 0", got)
|
||||
}
|
||||
if got := btPeerCompletion(0, 0); got != nil {
|
||||
t.Fatalf("completion without metadata = %v, want nil", *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerRelevanceUsesWantedMissingPieces(t *testing.T) {
|
||||
missing := btMissingWantedPieces([]torrent.PieceStateRun{
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, true),
|
||||
Priority: torrent.PiecePriorityNormal,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, false),
|
||||
Priority: torrent.PiecePriorityNormal,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, false),
|
||||
Priority: torrent.PiecePriorityNone,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
}, 6)
|
||||
if missing == nil || !missing.Equals(roaring.BitmapOf(2, 3)) {
|
||||
t.Fatalf("missing wanted pieces = %v, want {2, 3}", missing)
|
||||
}
|
||||
|
||||
if got := btPeerRelevance(roaring.BitmapOf(0, 2, 4), missing); got == nil || *got != 0.5 {
|
||||
t.Fatalf("relevance = %v, want 0.5", got)
|
||||
}
|
||||
if got := btPeerRelevance(roaring.BitmapOf(0, 1, 4), missing); got == nil || *got != 0 {
|
||||
t.Fatalf("zero relevance = %v, want 0", got)
|
||||
}
|
||||
if got := btPeerRelevance(roaring.BitmapOf(2, 3), roaring.New()); got != nil {
|
||||
t.Fatalf("relevance with no missing pieces = %v, want nil", *got)
|
||||
}
|
||||
if got := btMissingWantedPieces(nil, 6); got != nil {
|
||||
t.Fatalf("incomplete piece states = %v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerRelevanceTracksRemainingDownload(t *testing.T) {
|
||||
missing := roaring.BitmapOf(90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
|
||||
if got := btPeerRelevance(missing.Clone(), missing); got == nil || *got != 1 {
|
||||
t.Fatalf("peer covering every missing piece = %v, want 1", got)
|
||||
}
|
||||
|
||||
oneRelevantPiece := roaring.BitmapOf(90)
|
||||
if got := btPeerRelevance(oneRelevantPiece, missing); got == nil || *got != 0.1 {
|
||||
t.Fatalf("peer covering one of ten missing pieces = %v, want 0.1", got)
|
||||
}
|
||||
|
||||
missingAfterTransfer := missing.Clone()
|
||||
missingAfterTransfer.Remove(90)
|
||||
if got := btPeerRelevance(oneRelevantPiece, missingAfterTransfer); got == nil || *got != 0 {
|
||||
t.Fatalf("peer after transferring its only relevant piece = %v, want 0", got)
|
||||
}
|
||||
}
|
||||
@@ -210,23 +210,76 @@ func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||
return f.meta
|
||||
}
|
||||
|
||||
func (f *Fetcher) Stats() any {
|
||||
func (f *Fetcher) Stats() *fetcher.Stats {
|
||||
handle := f.currentHandle()
|
||||
if !handle.IsValid() {
|
||||
return &ped2k.Stats{}
|
||||
return &fetcher.Stats{
|
||||
Runtime: &ped2k.StatsRuntime{
|
||||
Peers: make([]*base.PeerStats, 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
status := handle.GetStatus()
|
||||
return &ped2k.Stats{
|
||||
State: string(status.State),
|
||||
ActivePeers: handle.ActiveConnections(),
|
||||
TotalPeers: status.NumPeers,
|
||||
DownloadRate: status.DownloadRate,
|
||||
Upload: status.Upload,
|
||||
UploadRate: status.UploadRate,
|
||||
TotalDone: status.TotalDone,
|
||||
TotalReceived: status.TotalReceived,
|
||||
TotalWanted: status.TotalWanted,
|
||||
pieceMap := buildED2KPieceMap(handle.PieceSnapshots())
|
||||
mapReady := pieceMap != nil && status.State != goed2k.LoadingResumeData && status.State != goed2k.Verifying
|
||||
var snapshot any
|
||||
if mapReady {
|
||||
snapshot = &ped2k.StatsSnapshot{
|
||||
Upload: status.Upload,
|
||||
TotalDone: status.TotalDone,
|
||||
TotalReceived: status.TotalReceived,
|
||||
TotalWanted: status.TotalWanted,
|
||||
PieceMap: pieceMap.Clone(),
|
||||
}
|
||||
}
|
||||
// Resume loading and verification are transitional. A nil Snapshot retains
|
||||
// the last trusted completion bitset until goed2k publishes a stable map.
|
||||
return &fetcher.Stats{
|
||||
Snapshot: snapshot,
|
||||
Runtime: &ped2k.StatsRuntime{
|
||||
State: string(status.State),
|
||||
Paused: status.Paused,
|
||||
ActivePeers: handle.ActiveConnections(),
|
||||
TotalPeers: status.NumPeers,
|
||||
DownloadRate: status.DownloadRate,
|
||||
UploadRate: status.UploadRate,
|
||||
Peers: buildED2KPeers(handle.GetPeersInfo()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func buildED2KPieceMap(snapshots []goed2k.PieceSnapshot) *base.PieceMap {
|
||||
if len(snapshots) == 0 {
|
||||
return nil
|
||||
}
|
||||
pieceMap := base.NewPieceMap(len(snapshots), goed2k.PieceSize)
|
||||
for _, snapshot := range snapshots {
|
||||
if snapshot.Index < 0 || snapshot.Index >= pieceMap.PieceCount() {
|
||||
continue
|
||||
}
|
||||
_ = pieceMap.Update(snapshot.Index, snapshot.State == goed2k.PieceSnapshotFinished)
|
||||
}
|
||||
return pieceMap
|
||||
}
|
||||
|
||||
func buildED2KPeers(peerInfos []goed2k.PeerInfo) []*base.PeerStats {
|
||||
peers := make([]*base.PeerStats, 0, len(peerInfos))
|
||||
for _, peerInfo := range peerInfos {
|
||||
peers = append(peers, &base.PeerStats{
|
||||
Address: peerInfo.Endpoint.String(),
|
||||
Client: ed2kPeerClient(peerInfo),
|
||||
DownloadSpeed: int64(peerInfo.PayloadDownloadSpeed),
|
||||
UploadSpeed: int64(peerInfo.PayloadUploadSpeed),
|
||||
PieceCount: peerInfo.RemotePieces.Count(),
|
||||
Source: peerInfo.SourceString(),
|
||||
Transport: "tcp",
|
||||
})
|
||||
}
|
||||
return peers
|
||||
}
|
||||
|
||||
func ed2kPeerClient(peerInfo goed2k.PeerInfo) string {
|
||||
return strings.TrimSpace(peerInfo.ModName + " " + peerInfo.StrModVersion)
|
||||
}
|
||||
|
||||
func (f *Fetcher) Progress() fetcher.Progress {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package ed2k
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ped2k "github.com/GopeedLab/gopeed/pkg/protocol/ed2k"
|
||||
"github.com/monkeyWie/goed2k"
|
||||
"github.com/monkeyWie/goed2k/protocol"
|
||||
)
|
||||
|
||||
func TestStatsSeparatesSnapshotAndRuntimeWithoutHandle(t *testing.T) {
|
||||
frame := (&Fetcher{}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil without a valid handle", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*ped2k.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if runtime.Peers == nil || len(runtime.Peers) != 0 {
|
||||
t.Fatalf("unexpected runtime peers: %+v", runtime.Peers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildED2KPieceMapPreservesIndexes(t *testing.T) {
|
||||
snapshots := []goed2k.PieceSnapshot{
|
||||
{Index: 0, State: goed2k.PieceSnapshotFinished},
|
||||
{Index: 1, State: goed2k.PieceSnapshotMissing},
|
||||
{Index: 2, State: goed2k.PieceSnapshotDownloading},
|
||||
{Index: 3, State: goed2k.PieceSnapshotFinished},
|
||||
}
|
||||
pieceMap := buildED2KPieceMap(snapshots)
|
||||
if pieceMap == nil {
|
||||
t.Fatal("buildED2KPieceMap returned nil")
|
||||
}
|
||||
want := []bool{true, false, false, true}
|
||||
for index, expected := range want {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != expected {
|
||||
t.Fatalf("piece %d = %v, %v; want %v", index, got, err, expected)
|
||||
}
|
||||
}
|
||||
if pieceMap.CompletedPieces() != 2 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 2", pieceMap.CompletedPieces())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildED2KPeers(t *testing.T) {
|
||||
remotePieces := protocol.NewBitField(4)
|
||||
remotePieces.SetBit(0)
|
||||
remotePieces.SetBit(2)
|
||||
endpoint, err := protocol.EndpointFromString("127.0.0.1", 4662)
|
||||
if err != nil {
|
||||
t.Fatalf("EndpointFromString: %v", err)
|
||||
}
|
||||
peers := buildED2KPeers([]goed2k.PeerInfo{{
|
||||
PayloadDownloadSpeed: 2048,
|
||||
PayloadUploadSpeed: 1024,
|
||||
RemotePieces: remotePieces,
|
||||
Endpoint: endpoint,
|
||||
ModName: "aMule",
|
||||
StrModVersion: "2.3.3",
|
||||
SourceFlag: int(goed2k.PeerServer | goed2k.PeerDHT),
|
||||
}})
|
||||
if len(peers) != 1 {
|
||||
t.Fatalf("len(peers) = %d, want 1", len(peers))
|
||||
}
|
||||
peer := peers[0]
|
||||
if peer.Client != "aMule 2.3.3" || peer.PieceCount != 2 || peer.Source != "server|kad" || peer.Transport != "tcp" {
|
||||
t.Fatalf("unexpected peer: %+v", peer)
|
||||
}
|
||||
}
|
||||
@@ -857,7 +857,9 @@ func (f *Fetcher) startResolveDownload() {
|
||||
Chunk: newChunk(0, 0), // For non-range, end doesn't matter
|
||||
}
|
||||
conn.ctx, conn.cancel = context.WithCancel(f.ctx)
|
||||
f.connMu.Lock()
|
||||
f.connections = append(f.connections, conn)
|
||||
f.connMu.Unlock()
|
||||
|
||||
f.wg.Add(1)
|
||||
// Use the resolve response directly
|
||||
@@ -1029,7 +1031,9 @@ func (f *Fetcher) runConnection(conn *connection) {
|
||||
buf := make([]byte, 8192)
|
||||
|
||||
retries := 0
|
||||
f.connMu.Lock()
|
||||
conn.retryTimes = 0
|
||||
f.connMu.Unlock()
|
||||
|
||||
for {
|
||||
// Rebuild client with updated fast-fail timeout on retries
|
||||
@@ -1058,7 +1062,9 @@ func (f *Fetcher) runConnection(conn *connection) {
|
||||
|
||||
// Reset counters after a successful help switch
|
||||
retries = 0
|
||||
f.connMu.Lock()
|
||||
conn.retryTimes = 0
|
||||
f.connMu.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1082,11 +1088,13 @@ func (f *Fetcher) runConnection(conn *connection) {
|
||||
return
|
||||
}
|
||||
|
||||
f.connMu.Lock()
|
||||
if re := extractRequestError(err); re != nil {
|
||||
conn.lastErr = re
|
||||
} else {
|
||||
conn.lastErr = err
|
||||
}
|
||||
f.connMu.Unlock()
|
||||
|
||||
if shouldCountHTTPFailure(err) {
|
||||
if re := extractRequestError(err); re != nil && re.Code == 403 {
|
||||
@@ -1099,14 +1107,15 @@ func (f *Fetcher) runConnection(conn *connection) {
|
||||
}
|
||||
return
|
||||
}
|
||||
conn.retryTimes++
|
||||
f.connMu.Lock()
|
||||
conn.retryTimes++
|
||||
retryTimes := conn.retryTimes
|
||||
conn.failed = true
|
||||
f.connMu.Unlock()
|
||||
if f.slowStart != nil {
|
||||
f.slowStart.onConnectFailed()
|
||||
}
|
||||
if conn.retryTimes >= 3 {
|
||||
if retryTimes >= 3 {
|
||||
f.connMu.Lock()
|
||||
conn.State = connFailed
|
||||
f.connMu.Unlock()
|
||||
@@ -1866,11 +1875,13 @@ func (f *Fetcher) runConnectionFallback(conn *connection) {
|
||||
return
|
||||
}
|
||||
|
||||
f.connMu.Lock()
|
||||
if re := extractRequestError(err); re != nil {
|
||||
conn.lastErr = re
|
||||
} else {
|
||||
conn.lastErr = err
|
||||
}
|
||||
f.connMu.Unlock()
|
||||
|
||||
if shouldCountHTTPFailure(err) {
|
||||
// Immediate fail for server connection limit (403)
|
||||
@@ -1884,7 +1895,9 @@ func (f *Fetcher) runConnectionFallback(conn *connection) {
|
||||
}
|
||||
return
|
||||
}
|
||||
f.connMu.Lock()
|
||||
conn.retryTimes++
|
||||
f.connMu.Unlock()
|
||||
countedRetries++
|
||||
if countedRetries >= 3 {
|
||||
f.connMu.Lock()
|
||||
@@ -2348,22 +2361,72 @@ func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||
return f.meta
|
||||
}
|
||||
|
||||
func (f *Fetcher) Stats() any {
|
||||
func (f *Fetcher) Stats() *fetcher.Stats {
|
||||
f.connMu.Lock()
|
||||
defer f.connMu.Unlock()
|
||||
|
||||
terminal := f.getState() == stateDone
|
||||
prefetched := int64(0)
|
||||
connectionCount := len(f.connections)
|
||||
if connectionCount == 0 {
|
||||
prefetched = f.resolveDataPos.Load()
|
||||
if terminal && f.meta != nil && f.meta.Res != nil && f.meta.Res.Size > prefetched {
|
||||
// A small range response can finish entirely during resolve prefetch,
|
||||
// before a formal connection is created. The fetcher owns this fact, so
|
||||
// expose one aggregate connection here instead of making Downloader
|
||||
// infer protocol state from task-level progress.
|
||||
prefetched = f.meta.Res.Size
|
||||
}
|
||||
if prefetched > 0 {
|
||||
connectionCount = 1
|
||||
}
|
||||
}
|
||||
connectionTotal := f.averageConnectionTotal(connectionCount)
|
||||
statsConnections := make([]*fhttp.StatsConnection, 0)
|
||||
for _, connection := range f.connections {
|
||||
completed := connection.Completed || connection.State == connCompleted
|
||||
// A successful fetcher has no active downloads left. Work stealing and
|
||||
// completion-by-total-size can leave a helper connection's last internal
|
||||
// state non-terminal even though the whole HTTP transfer is finished.
|
||||
if terminal && !connection.failed {
|
||||
completed = true
|
||||
}
|
||||
statsConnections = append(statsConnections, &fhttp.StatsConnection{
|
||||
Downloaded: connection.Downloaded,
|
||||
Completed: connection.Completed,
|
||||
Total: connectionTotal,
|
||||
Completed: completed,
|
||||
Failed: connection.failed,
|
||||
RetryTimes: connection.retryTimes,
|
||||
})
|
||||
}
|
||||
return &fhttp.Stats{
|
||||
Connections: statsConnections,
|
||||
if len(statsConnections) == 0 && prefetched > 0 {
|
||||
statsConnections = append(statsConnections, &fhttp.StatsConnection{
|
||||
Downloaded: prefetched,
|
||||
Total: connectionTotal,
|
||||
Completed: terminal,
|
||||
})
|
||||
}
|
||||
return &fetcher.Stats{
|
||||
Snapshot: &fhttp.Stats{
|
||||
Connections: statsConnections,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// averageConnectionTotal returns the nominal equal share used to compare how
|
||||
// much work each currently visible connection has contributed. HTTP slow start
|
||||
// can add connections over time, so all lanes are recalculated against the
|
||||
// current connection count whenever stats are requested. A remainder is
|
||||
// rounded up to avoid reporting 100% before a connection reaches its share.
|
||||
func (f *Fetcher) averageConnectionTotal(connectionCount int) int64 {
|
||||
if f.meta == nil || f.meta.Res == nil || f.meta.Res.Size <= 0 || connectionCount <= 0 {
|
||||
return 0
|
||||
}
|
||||
total := f.meta.Res.Size / int64(connectionCount)
|
||||
if f.meta.Res.Size%int64(connectionCount) != 0 {
|
||||
total++
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func (f *Fetcher) Progress() fetcher.Progress {
|
||||
|
||||
@@ -443,7 +443,7 @@ func TestFetcher_DownloadContinue_NoRangeRestart(t *testing.T) {
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if len(stats.Connections) != 1 {
|
||||
t.Fatalf("expected a single non-range connection, got %d", len(stats.Connections))
|
||||
}
|
||||
@@ -461,7 +461,7 @@ func TestFetcher_DownloadContinue_NoRangeRestart(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
finalStats := fetcher.Stats().(*http.Stats)
|
||||
finalStats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if len(finalStats.Connections) != 1 {
|
||||
t.Fatalf("expected a single non-range connection after resume, got %d", len(finalStats.Connections))
|
||||
}
|
||||
@@ -518,7 +518,7 @@ func TestFetcher_DownloadIgnoredRangeFallsBackToSequential(t *testing.T) {
|
||||
if f.meta.Res.Range {
|
||||
t.Fatal("expected ignored Range response to disable range mode")
|
||||
}
|
||||
stats := f.Stats().(*http.Stats)
|
||||
stats := f.Stats().Snapshot.(*http.Stats)
|
||||
if len(stats.Connections) != 1 {
|
||||
t.Fatalf("expected one sequential connection, got %d", len(stats.Connections))
|
||||
}
|
||||
@@ -783,7 +783,7 @@ func TestFetcher_SequentialRetryReprobesRange(t *testing.T) {
|
||||
t.Fatalf("Range mode = %v, want %v", f.meta.Res.Range, tt.wantRangeMode)
|
||||
}
|
||||
if tt.wantParallel {
|
||||
stats := f.Stats().(*http.Stats)
|
||||
stats := f.Stats().Snapshot.(*http.Stats)
|
||||
if len(stats.Connections) <= 1 {
|
||||
f.slowStart.mu.Lock()
|
||||
totalLaunched := f.slowStart.totalLaunched
|
||||
@@ -1999,7 +1999,7 @@ func TestFetcher_DownloadResponseBodyReadTimeout(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if len(stats.Connections) == 0 {
|
||||
t.Fatalf("expected connections stats for timeout test")
|
||||
}
|
||||
@@ -2045,7 +2045,7 @@ func TestFetcher_Download500Recovery(t *testing.T) {
|
||||
}
|
||||
|
||||
// Verify 500 errors don't count as failures (retryTimes should be 0)
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
for _, conn := range stats.Connections {
|
||||
if conn.RetryTimes != 0 {
|
||||
t.Errorf("Expected retryTimes to be 0 for 500 errors (exempt), got %d", conn.RetryTimes)
|
||||
@@ -2144,22 +2144,87 @@ func TestFetcher_Stats(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
// With slow-start strategy, connection count may be less than max if download is fast
|
||||
// Just verify we have at least 1 connection and no more than max
|
||||
if len(stats.Connections) < 1 || len(stats.Connections) > 16 {
|
||||
t.Errorf("Stats() connection count got = %v, want between 1 and 16", len(stats.Connections))
|
||||
}
|
||||
totalDownloaded := int64(0)
|
||||
expectedConnectionTotal := test.BuildSize / int64(len(stats.Connections))
|
||||
if test.BuildSize%int64(len(stats.Connections)) != 0 {
|
||||
expectedConnectionTotal++
|
||||
}
|
||||
for i, conn := range stats.Connections {
|
||||
t.Logf("Connection %d: Downloaded=%d, Completed=%v", i, conn.Downloaded, conn.Completed)
|
||||
totalDownloaded += conn.Downloaded
|
||||
if conn.Total != expectedConnectionTotal {
|
||||
t.Errorf("connection %d total = %d, want shared total %d", i, conn.Total, expectedConnectionTotal)
|
||||
}
|
||||
}
|
||||
if totalDownloaded != test.BuildSize {
|
||||
t.Errorf("Stats() got = %v, want %v", totalDownloaded, test.BuildSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetcher_StatsMarksTerminalNonFailedConnectionsComplete(t *testing.T) {
|
||||
fetcher := &Fetcher{
|
||||
meta: &fetcher.FetcherMeta{Res: &base.Resource{Size: 4096, Range: true}},
|
||||
connections: []*connection{
|
||||
{State: connDownloading, Chunk: newChunk(0, 2047), Downloaded: 1024},
|
||||
{State: connCompleted, Chunk: newChunk(2048, 3071), Downloaded: 2048},
|
||||
{State: connFailed, Chunk: newChunk(3072, 4095), Downloaded: 512, failed: true, retryTimes: 3},
|
||||
},
|
||||
}
|
||||
fetcher.setState(stateDone)
|
||||
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if !stats.Connections[0].Completed {
|
||||
t.Fatal("terminal non-failed connection was reported as downloading")
|
||||
}
|
||||
if !stats.Connections[1].Completed {
|
||||
t.Fatal("completed connection was not reported as completed")
|
||||
}
|
||||
if stats.Connections[2].Completed || !stats.Connections[2].Failed {
|
||||
t.Fatal("failed connection lost its failure state")
|
||||
}
|
||||
const averageTotal = int64(1366) // ceil(4096 / 3)
|
||||
for index, connection := range stats.Connections {
|
||||
if connection.Total != averageTotal {
|
||||
t.Fatalf("connection %d total = %d, want shared total %d", index, connection.Total, averageTotal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetcher_StatsUsesUnknownTotalWithoutResourceSize(t *testing.T) {
|
||||
fetcher := &Fetcher{
|
||||
meta: &fetcher.FetcherMeta{Res: &base.Resource{}},
|
||||
connections: []*connection{
|
||||
{State: connDownloading, Chunk: newChunk(0, 0), Downloaded: 1024},
|
||||
},
|
||||
}
|
||||
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if stats.Connections[0].Total != 0 {
|
||||
t.Fatalf("unknown resource total = %d, want 0", stats.Connections[0].Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetcher_StatsExposesCompletedResolvePrefetch(t *testing.T) {
|
||||
fetcher := &Fetcher{meta: &fetcher.FetcherMeta{Res: &base.Resource{Size: 100}}}
|
||||
fetcher.resolveDataPos.Store(100)
|
||||
fetcher.setState(stateDone)
|
||||
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
if len(stats.Connections) != 1 {
|
||||
t.Fatalf("connection count = %d, want 1", len(stats.Connections))
|
||||
}
|
||||
connection := stats.Connections[0]
|
||||
if connection.Downloaded != 100 || connection.Total != 100 || !connection.Completed {
|
||||
t.Fatalf("unexpected prefetch connection: %+v", connection)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFetcher_DownloadOneTimeURL tests downloading from a URL that can only be accessed once
|
||||
// This simulates signed URLs or one-time download links that expire after first use
|
||||
func TestFetcher_DownloadOneTimeURL(t *testing.T) {
|
||||
@@ -2261,7 +2326,7 @@ func TestFetcher_SlowStartExpansion(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check final connection count equals maxConns exactly
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
finalConns := len(stats.Connections)
|
||||
|
||||
// Debug: show connection details and metadata
|
||||
@@ -2407,7 +2472,7 @@ func TestFetcher_AsyncPrefetch(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check stats - should have connections that downloaded remaining data
|
||||
stats := fetcher.Stats().(*http.Stats)
|
||||
stats := fetcher.Stats().Snapshot.(*http.Stats)
|
||||
t.Logf("Final connections: %d", len(stats.Connections))
|
||||
|
||||
prefetchedUsed := fetcher.resolveDataPos.Load()
|
||||
|
||||
Reference in new issue
Block a user