mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 02:48:57 -07:00
fd0a147ae6
Request to terminate currently ongoing downloads and jump to the bumped file incoming in 3, 2, 1. Also, has a slightly strange effect where we pop a job off the queue, but the copyChannel is still busy and blocks, though it gets moved to the progress slice in the jobqueue, and looks like it's in progress which it isn't as it's waiting to be picked up from the copyChan. As a result, the progress emitter doesn't register on the task, and hence the file doesn't have a progress bar, but cannot be replaced by a bump. I guess I can fix progress bar issue by moving the progressEmiter.Register just before passing the file to the copyChan, but then we are back to the initial problem of a file with a progress bar, but no progress happening as it's stuck on write to copyChan I checked if there is a way to check for channel writeability (before popping) but got struck by lightning just for bringing the idea up in #go-nuts. My ideal scenario would be to check if copyChan is writeable, pop job from the queue and shove it down handleFile. This way jobs would stay in the queue while they cannot be handled, meaning that the `Bump` could bring your file up higher.
107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
// any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
// more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package model
|
|
|
|
import (
|
|
"container/list"
|
|
"sync"
|
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
|
)
|
|
|
|
type JobQueue struct {
|
|
progress []*protocol.FileInfo
|
|
|
|
queued *list.List
|
|
lookup map[string]*list.Element // O(1) lookups
|
|
|
|
mut sync.Mutex
|
|
}
|
|
|
|
func NewJobQueue() *JobQueue {
|
|
return &JobQueue{
|
|
progress: []*protocol.FileInfo{},
|
|
queued: list.New(),
|
|
lookup: make(map[string]*list.Element),
|
|
}
|
|
}
|
|
|
|
func (q *JobQueue) Push(file *protocol.FileInfo) {
|
|
q.mut.Lock()
|
|
defer q.mut.Unlock()
|
|
|
|
q.lookup[file.Name] = q.queued.PushBack(file)
|
|
}
|
|
|
|
func (q *JobQueue) Pop() *protocol.FileInfo {
|
|
q.mut.Lock()
|
|
defer q.mut.Unlock()
|
|
|
|
if q.queued.Len() == 0 {
|
|
return nil
|
|
}
|
|
|
|
f := q.queued.Remove(q.queued.Front()).(*protocol.FileInfo)
|
|
delete(q.lookup, f.Name)
|
|
q.progress = append(q.progress, f)
|
|
|
|
return f
|
|
}
|
|
|
|
func (q *JobQueue) Bump(filename string) {
|
|
q.mut.Lock()
|
|
defer q.mut.Unlock()
|
|
|
|
ele, ok := q.lookup[filename]
|
|
if ok {
|
|
q.queued.MoveToFront(ele)
|
|
}
|
|
}
|
|
|
|
func (q *JobQueue) Done(file *protocol.FileInfo) {
|
|
q.mut.Lock()
|
|
defer q.mut.Unlock()
|
|
|
|
for i := range q.progress {
|
|
if q.progress[i].Name == file.Name {
|
|
copy(q.progress[i:], q.progress[i+1:])
|
|
q.progress[len(q.progress)-1] = nil
|
|
q.progress = q.progress[:len(q.progress)-1]
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (q *JobQueue) Jobs() ([]protocol.FileInfoTruncated, []protocol.FileInfoTruncated) {
|
|
q.mut.Lock()
|
|
defer q.mut.Unlock()
|
|
|
|
progress := make([]protocol.FileInfoTruncated, len(q.progress))
|
|
for i := range q.progress {
|
|
progress[i] = q.progress[i].ToTruncated()
|
|
}
|
|
|
|
queued := make([]protocol.FileInfoTruncated, q.queued.Len())
|
|
i := 0
|
|
for e := q.queued.Front(); e != nil; e = e.Next() {
|
|
fi := e.Value.(*protocol.FileInfo)
|
|
queued[i] = fi.ToTruncated()
|
|
i++
|
|
}
|
|
|
|
return progress, queued
|
|
}
|