diff --git a/internal/db/db.go b/internal/db/db.go index 5ce3863..c7fa146 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -48,10 +48,16 @@ func Open(path string) (*DB, error) { } func migrate(sqlDB *sql.DB) error { - // Add display_name column to existing databases. - _, err := sqlDB.Exec(`ALTER TABLE users ADD COLUMN display_name TEXT NOT NULL DEFAULT ''`) - if err != nil && !strings.Contains(err.Error(), "duplicate column") { - return err + // Add display_name and transfer counter columns to existing databases. + for _, stmt := range []string{ + `ALTER TABLE users ADD COLUMN display_name TEXT NOT NULL DEFAULT ''`, + `ALTER TABLE torrents ADD COLUMN uploaded_bytes INTEGER NOT NULL DEFAULT 0`, + `ALTER TABLE torrents ADD COLUMN downloaded_bytes INTEGER NOT NULL DEFAULT 0`, + } { + _, err := sqlDB.Exec(stmt) + if err != nil && !strings.Contains(err.Error(), "duplicate column") { + return err + } } return nil } @@ -103,6 +109,10 @@ type Torrent struct { ErrorMessage string `json:"error_message,omitempty"` CreatedAt time.Time `json:"created_at"` CompletedAt *time.Time `json:"completed_at,omitempty"` + // Cumulative transfer totals persisted across restarts; live session bytes + // not yet flushed are added on top by the torrent engine. + UploadedBytes int64 `json:"-"` + DownloadedBytes int64 `json:"-"` } func (d *DB) CountUsers(ctx context.Context) (int64, error) { @@ -265,9 +275,9 @@ func (d *DB) GetTorrentByID(ctx context.Context, id int64) (*Torrent, error) { var status string var completedAt sql.NullTime err := d.SQL.QueryRowContext(ctx, ` - SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at + SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at, uploaded_bytes, downloaded_bytes FROM torrents WHERE id = ?`, id, - ).Scan(&t.ID, &t.OwnerID, &t.InfoHash, &t.Name, &t.MetaPath, &t.DataPath, &status, &t.ErrorMessage, &t.CreatedAt, &completedAt) + ).Scan(&t.ID, &t.OwnerID, &t.InfoHash, &t.Name, &t.MetaPath, &t.DataPath, &status, &t.ErrorMessage, &t.CreatedAt, &completedAt, &t.UploadedBytes, &t.DownloadedBytes) if err != nil { return nil, err } @@ -281,7 +291,7 @@ func (d *DB) GetTorrentByID(ctx context.Context, id int64) (*Torrent, error) { func (d *DB) ListTorrentsByOwner(ctx context.Context, ownerID int64) ([]Torrent, error) { rows, err := d.SQL.QueryContext(ctx, ` - SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at + SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at, uploaded_bytes, downloaded_bytes FROM torrents WHERE owner_id = ? ORDER BY created_at DESC`, ownerID, ) if err != nil { @@ -293,7 +303,7 @@ func (d *DB) ListTorrentsByOwner(ctx context.Context, ownerID int64) ([]Torrent, func (d *DB) ListAllTorrents(ctx context.Context) ([]Torrent, error) { rows, err := d.SQL.QueryContext(ctx, ` - SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at + SELECT id, owner_id, info_hash, name, meta_path, data_path, status, COALESCE(error_message, ''), created_at, completed_at, uploaded_bytes, downloaded_bytes FROM torrents ORDER BY created_at DESC`, ) if err != nil { @@ -309,7 +319,7 @@ func scanTorrents(rows *sql.Rows) ([]Torrent, error) { var t Torrent var status string var completedAt sql.NullTime - if err := rows.Scan(&t.ID, &t.OwnerID, &t.InfoHash, &t.Name, &t.MetaPath, &t.DataPath, &status, &t.ErrorMessage, &t.CreatedAt, &completedAt); err != nil { + if err := rows.Scan(&t.ID, &t.OwnerID, &t.InfoHash, &t.Name, &t.MetaPath, &t.DataPath, &status, &t.ErrorMessage, &t.CreatedAt, &completedAt, &t.UploadedBytes, &t.DownloadedBytes); err != nil { return nil, err } t.Status = TorrentStatus(status) @@ -330,6 +340,15 @@ func (d *DB) UpdateTorrentStatus(ctx context.Context, id int64, status TorrentSt return err } +// AddTorrentTransfer increments a torrent's persisted transfer totals. +func (d *DB) AddTorrentTransfer(ctx context.Context, id, uploaded, downloaded int64) error { + _, err := d.SQL.ExecContext(ctx, ` + UPDATE torrents SET uploaded_bytes = uploaded_bytes + ?, downloaded_bytes = downloaded_bytes + ? WHERE id = ?`, + uploaded, downloaded, id, + ) + return err +} + func (d *DB) DeleteTorrent(ctx context.Context, id int64) error { _, err := d.SQL.ExecContext(ctx, `DELETE FROM torrents WHERE id = ?`, id) return err diff --git a/internal/db/schema.sql b/internal/db/schema.sql index 422e21a..6e08f1e 100644 --- a/internal/db/schema.sql +++ b/internal/db/schema.sql @@ -30,6 +30,8 @@ CREATE TABLE IF NOT EXISTS torrents ( error_message TEXT, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, completed_at TIMESTAMP, + uploaded_bytes INTEGER NOT NULL DEFAULT 0, + downloaded_bytes INTEGER NOT NULL DEFAULT 0, UNIQUE (owner_id, info_hash) ); diff --git a/internal/torrent/engine.go b/internal/torrent/engine.go index 1820d95..9d0c354 100644 --- a/internal/torrent/engine.go +++ b/internal/torrent/engine.go @@ -23,7 +23,20 @@ type Engine struct { metaPath string downloadsPath string mu sync.Mutex - byID map[int64]*torrent.Torrent + byID map[int64]*liveTorrent + flushMu sync.Mutex + done chan struct{} +} + +// statsFlushInterval bounds how much transfer accounting is lost on a crash. +const statsFlushInterval = 30 * time.Second + +// liveTorrent tracks how much of the client's per-session transfer counters +// has already been added to the torrent's persisted totals. +type liveTorrent struct { + tt *torrent.Torrent + flushedUp int64 + flushedDown int64 } type LiveStats struct { @@ -64,19 +77,70 @@ func New(database *db.DB, metaPath, downloadsPath string) (*Engine, error) { return nil, fmt.Errorf("torrent client: %w", err) } - return &Engine{ + e := &Engine{ client: client, db: database, metaPath: metaPath, downloadsPath: downloadsPath, - byID: make(map[int64]*torrent.Torrent), - }, nil + byID: make(map[int64]*liveTorrent), + done: make(chan struct{}), + } + go e.flushLoop() + return e, nil } func (e *Engine) Close() { + close(e.done) + e.flushAll() e.client.Close() } +func (e *Engine) flushLoop() { + ticker := time.NewTicker(statsFlushInterval) + defer ticker.Stop() + for { + select { + case <-ticker.C: + e.flushAll() + case <-e.done: + return + } + } +} + +func (e *Engine) flushAll() { + e.mu.Lock() + live := make(map[int64]*liveTorrent, len(e.byID)) + for id, lt := range e.byID { + live[id] = lt + } + e.mu.Unlock() + for id, lt := range live { + e.flush(id, lt) + } +} + +// flush adds the transfer bytes accumulated since the last flush to the +// torrent's persisted totals. +func (e *Engine) flush(id int64, lt *liveTorrent) { + e.flushMu.Lock() + defer e.flushMu.Unlock() + stats := lt.tt.Stats() + up, down := stats.BytesWrittenData.Int64(), stats.BytesReadData.Int64() + e.mu.Lock() + deltaUp, deltaDown := up-lt.flushedUp, down-lt.flushedDown + e.mu.Unlock() + if deltaUp == 0 && deltaDown == 0 { + return + } + if err := e.db.AddTorrentTransfer(context.Background(), id, deltaUp, deltaDown); err != nil { + return + } + e.mu.Lock() + lt.flushedUp, lt.flushedDown = up, down + e.mu.Unlock() +} + func (e *Engine) Reload(ctx context.Context) error { all, err := e.db.ListAllTorrents(ctx) if err != nil { @@ -93,7 +157,7 @@ func (e *Engine) Reload(ctx context.Context) error { } tt.DownloadAll() e.mu.Lock() - e.byID[t.ID] = tt + e.byID[t.ID] = &liveTorrent{tt: tt} e.mu.Unlock() go e.watchCompletion(t.ID, tt) } @@ -168,7 +232,7 @@ func (e *Engine) AddFromTorrentFile(ctx context.Context, ownerID int64, torrentB tt.DownloadAll() e.mu.Lock() - e.byID[rec.ID] = tt + e.byID[rec.ID] = &liveTorrent{tt: tt} e.mu.Unlock() go e.watchCompletion(rec.ID, tt) @@ -205,13 +269,14 @@ func (e *Engine) watchCompletion(id int64, tt *torrent.Torrent) { func (e *Engine) Pause(ctx context.Context, id int64) error { e.mu.Lock() - tt, ok := e.byID[id] + lt, ok := e.byID[id] if ok { delete(e.byID, id) } e.mu.Unlock() if ok { - tt.Drop() + e.flush(id, lt) + lt.tt.Drop() } return e.db.UpdateTorrentStatus(ctx, id, db.StatusPaused, "", nil) } @@ -237,7 +302,7 @@ func (e *Engine) Resume(ctx context.Context, id int64) error { } tt.DownloadAll() e.mu.Lock() - e.byID[id] = tt + e.byID[id] = &liveTorrent{tt: tt} e.mu.Unlock() go e.watchCompletion(id, tt) @@ -254,13 +319,13 @@ func (e *Engine) Delete(ctx context.Context, id int64, removeData bool) error { return err } e.mu.Lock() - tt, ok := e.byID[id] + lt, ok := e.byID[id] if ok { delete(e.byID, id) } e.mu.Unlock() if ok { - tt.Drop() + lt.tt.Drop() } if err := e.db.DeleteTorrent(ctx, id); err != nil { return err @@ -295,23 +360,34 @@ func (e *Engine) ListForOwner(ctx context.Context, ownerID int64) ([]TorrentView func (e *Engine) LiveTorrent(id int64) (*torrent.Torrent, bool) { e.mu.Lock() defer e.mu.Unlock() - tt, ok := e.byID[id] - return tt, ok + lt, ok := e.byID[id] + if !ok { + return nil, false + } + return lt.tt, true } func (e *Engine) view(rec *db.Torrent) *TorrentView { v := &TorrentView{Torrent: *rec} e.mu.Lock() - tt, ok := e.byID[rec.ID] + lt, ok := e.byID[rec.ID] + var flushedUp, flushedDown int64 + if ok { + flushedUp, flushedDown = lt.flushedUp, lt.flushedDown + } e.mu.Unlock() - if !ok || tt.Info() == nil { - v.Stats = LiveStats{} + if !ok || lt.tt.Info() == nil { + v.Stats = LiveStats{ + Downloaded: rec.DownloadedBytes, + Uploaded: rec.UploadedBytes, + } if rec.Status == db.StatusSeeding || rec.CompletedAt != nil { v.Stats.Complete = true v.Stats.Progress = 1 } return v } + tt := lt.tt stats := tt.Stats() completed := tt.BytesCompleted() total := tt.Length() @@ -328,8 +404,8 @@ func (e *Engine) view(rec *db.Torrent) *TorrentView { } v.Stats = LiveStats{ Progress: progress, - Downloaded: stats.BytesReadData.Int64(), - Uploaded: stats.BytesWrittenData.Int64(), + Downloaded: rec.DownloadedBytes + stats.BytesReadData.Int64() - flushedDown, + Uploaded: rec.UploadedBytes + stats.BytesWrittenData.Int64() - flushedUp, DownloadRate: 0, UploadRate: 0, Peers: stats.ActivePeers,