Skip to content

Commit

Permalink
Try to improve download speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ChienNM3 committed Jan 24, 2024
1 parent 937659b commit 1b3e03b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion handler/handler_video_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func (h *Handler) Watch(w http.ResponseWriter, r *http.Request, infoHash, fileNa
return
}

// This step is for speed up the download!
if err := h.torrentManager.CancelOthers(infoHash); err != nil {
handleError(w, r, "Cancel others", err, http.StatusBadRequest)
return
}

_ = ui.VideoPlayer(torrentInfo, fileName).Render(r.Context(), w)
}

Expand All @@ -33,9 +39,10 @@ func (h *Handler) Stream(w http.ResponseWriter, r *http.Request, infoHash, fileN
}

// TODO: Maybe implement more effective file reader to seed up the download
file.Download()
reader := file.NewReader()
reader.SetResponsive()
reader.SetReadahead(file.Length() / 100) // Read ahead 1% of the file
//reader.SetReadahead(file.Length() / 100) // Read ahead 1% of the file

mime, err := mimetype.DetectReader(reader)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions torrent/manager_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package torrent

import (
"fmt"
)

// CancelOthers cancels all pieces of all torrents except the one with the given info hash.
func (m *Manager) CancelOthers(infoHash string) error {
infoHashHex, err := infoHashFromHexString(infoHash)
if err != nil {
return fmt.Errorf("parse infohash: %w", err)
}

for _, tor := range m.client.Torrents() {
if tor.InfoHash() != infoHashHex {
tor.CancelPieces(0, tor.NumPieces())
}
}

return nil
}

0 comments on commit 1b3e03b

Please sign in to comment.