|
@@ -17,11 +17,13 @@ import (
|
|
|
)
|
|
|
|
|
|
type ProgressEmitter struct {
|
|
|
- registry map[string]*sharedPullerState
|
|
|
+ registry map[string]map[string]*sharedPullerState // folder: name: puller
|
|
|
interval time.Duration
|
|
|
minBlocks int
|
|
|
sentDownloadStates map[protocol.DeviceID]*sentDownloadState // States representing what we've sent to the other peer via DownloadProgress messages.
|
|
|
- connections map[string][]protocol.Connection
|
|
|
+ connections map[protocol.DeviceID]protocol.Connection
|
|
|
+ foldersByConns map[protocol.DeviceID][]string
|
|
|
+ disabled bool
|
|
|
mut sync.Mutex
|
|
|
|
|
|
timer *time.Timer
|
|
@@ -34,10 +36,11 @@ type ProgressEmitter struct {
|
|
|
func NewProgressEmitter(cfg config.Wrapper) *ProgressEmitter {
|
|
|
t := &ProgressEmitter{
|
|
|
stop: make(chan struct{}),
|
|
|
- registry: make(map[string]*sharedPullerState),
|
|
|
+ registry: make(map[string]map[string]*sharedPullerState),
|
|
|
timer: time.NewTimer(time.Millisecond),
|
|
|
sentDownloadStates: make(map[protocol.DeviceID]*sentDownloadState),
|
|
|
- connections: make(map[string][]protocol.Connection),
|
|
|
+ connections: make(map[protocol.DeviceID]protocol.Connection),
|
|
|
+ foldersByConns: make(map[protocol.DeviceID][]string),
|
|
|
mut: sync.NewMutex(),
|
|
|
}
|
|
|
|
|
@@ -62,20 +65,21 @@ func (t *ProgressEmitter) Serve() {
|
|
|
l.Debugln("progress emitter: timer - looking after", len(t.registry))
|
|
|
|
|
|
newLastUpdated := lastUpdate
|
|
|
- newCount = len(t.registry)
|
|
|
- for _, puller := range t.registry {
|
|
|
- updated := puller.Updated()
|
|
|
- if updated.After(newLastUpdated) {
|
|
|
- newLastUpdated = updated
|
|
|
+ newCount = t.lenRegistryLocked()
|
|
|
+ for _, pullers := range t.registry {
|
|
|
+ for _, puller := range pullers {
|
|
|
+ if updated := puller.Updated(); updated.After(newLastUpdated) {
|
|
|
+ newLastUpdated = updated
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if !newLastUpdated.Equal(lastUpdate) || newCount != lastCount {
|
|
|
lastUpdate = newLastUpdated
|
|
|
lastCount = newCount
|
|
|
- t.sendDownloadProgressEvent()
|
|
|
+ t.sendDownloadProgressEventLocked()
|
|
|
if len(t.connections) > 0 {
|
|
|
- t.sendDownloadProgressMessages()
|
|
|
+ t.sendDownloadProgressMessagesLocked()
|
|
|
}
|
|
|
} else {
|
|
|
l.Debugln("progress emitter: nothing new")
|
|
@@ -89,30 +93,29 @@ func (t *ProgressEmitter) Serve() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (t *ProgressEmitter) sendDownloadProgressEvent() {
|
|
|
- // registry lock already held
|
|
|
+func (t *ProgressEmitter) sendDownloadProgressEventLocked() {
|
|
|
output := make(map[string]map[string]*pullerProgress)
|
|
|
- for _, puller := range t.registry {
|
|
|
- if output[puller.folder] == nil {
|
|
|
- output[puller.folder] = make(map[string]*pullerProgress)
|
|
|
+ for folder, pullers := range t.registry {
|
|
|
+ if len(pullers) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ output[folder] = make(map[string]*pullerProgress)
|
|
|
+ for name, puller := range pullers {
|
|
|
+ output[folder][name] = puller.Progress()
|
|
|
}
|
|
|
- output[puller.folder][puller.file.Name] = puller.Progress()
|
|
|
}
|
|
|
events.Default.Log(events.DownloadProgress, output)
|
|
|
l.Debugf("progress emitter: emitting %#v", output)
|
|
|
}
|
|
|
|
|
|
-func (t *ProgressEmitter) sendDownloadProgressMessages() {
|
|
|
- // registry lock already held
|
|
|
- sharedFolders := make(map[protocol.DeviceID][]string)
|
|
|
- deviceConns := make(map[protocol.DeviceID]protocol.Connection)
|
|
|
- subscribers := t.connections
|
|
|
- for folder, conns := range subscribers {
|
|
|
- for _, conn := range conns {
|
|
|
- id := conn.ID()
|
|
|
-
|
|
|
- deviceConns[id] = conn
|
|
|
- sharedFolders[id] = append(sharedFolders[id], folder)
|
|
|
+func (t *ProgressEmitter) sendDownloadProgressMessagesLocked() {
|
|
|
+ for id, conn := range t.connections {
|
|
|
+ for _, folder := range t.foldersByConns[id] {
|
|
|
+ pullers, ok := t.registry[folder]
|
|
|
+ if !ok {
|
|
|
+ // There's never been any puller registered for this folder yet
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
state, ok := t.sentDownloadStates[id]
|
|
|
if !ok {
|
|
@@ -122,8 +125,8 @@ func (t *ProgressEmitter) sendDownloadProgressMessages() {
|
|
|
t.sentDownloadStates[id] = state
|
|
|
}
|
|
|
|
|
|
- var activePullers []*sharedPullerState
|
|
|
- for _, puller := range t.registry {
|
|
|
+ activePullers := make([]*sharedPullerState, 0, len(pullers))
|
|
|
+ for _, puller := range pullers {
|
|
|
if puller.folder != folder || puller.file.IsSymlink() || puller.file.IsDirectory() || len(puller.file.Blocks) <= t.minBlocks {
|
|
|
continue
|
|
|
}
|
|
@@ -143,7 +146,7 @@ func (t *ProgressEmitter) sendDownloadProgressMessages() {
|
|
|
|
|
|
// Clean up sentDownloadStates for devices which we are no longer connected to.
|
|
|
for id := range t.sentDownloadStates {
|
|
|
- _, ok := deviceConns[id]
|
|
|
+ _, ok := t.connections[id]
|
|
|
if !ok {
|
|
|
// Null out outstanding entries for device
|
|
|
delete(t.sentDownloadStates, id)
|
|
@@ -152,13 +155,12 @@ func (t *ProgressEmitter) sendDownloadProgressMessages() {
|
|
|
|
|
|
// If a folder was unshared from some device, tell it that all temp files
|
|
|
// are now gone.
|
|
|
- for id, sharedDeviceFolders := range sharedFolders {
|
|
|
- state := t.sentDownloadStates[id]
|
|
|
- nextFolder:
|
|
|
+ for id, state := range t.sentDownloadStates {
|
|
|
// For each of the folders that the state is aware of,
|
|
|
// try to match it with a shared folder we've discovered above,
|
|
|
+ nextFolder:
|
|
|
for _, folder := range state.folders() {
|
|
|
- for _, existingFolder := range sharedDeviceFolders {
|
|
|
+ for _, existingFolder := range t.foldersByConns[id] {
|
|
|
if existingFolder == folder {
|
|
|
continue nextFolder
|
|
|
}
|
|
@@ -189,12 +191,23 @@ func (t *ProgressEmitter) CommitConfiguration(from, to config.Configuration) boo
|
|
|
t.mut.Lock()
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
|
- t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
|
|
|
- if t.interval < time.Second {
|
|
|
- t.interval = time.Second
|
|
|
+ switch {
|
|
|
+ case t.disabled && to.Options.ProgressUpdateIntervalS >= 0:
|
|
|
+ t.disabled = false
|
|
|
+ l.Debugln("progress emitter: enabled")
|
|
|
+ fallthrough
|
|
|
+ case !t.disabled && from.Options.ProgressUpdateIntervalS != to.Options.ProgressUpdateIntervalS:
|
|
|
+ t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
|
|
|
+ if t.interval < time.Second {
|
|
|
+ t.interval = time.Second
|
|
|
+ }
|
|
|
+ l.Debugln("progress emitter: updated interval", t.interval)
|
|
|
+ case !t.disabled && to.Options.ProgressUpdateIntervalS < 0:
|
|
|
+ t.clearLocked()
|
|
|
+ t.disabled = true
|
|
|
+ l.Debugln("progress emitter: disabled")
|
|
|
}
|
|
|
t.minBlocks = to.Options.TempIndexMinBlocks
|
|
|
- l.Debugln("progress emitter: updated interval", t.interval)
|
|
|
|
|
|
return true
|
|
|
}
|
|
@@ -209,13 +222,18 @@ func (t *ProgressEmitter) Stop() {
|
|
|
func (t *ProgressEmitter) Register(s *sharedPullerState) {
|
|
|
t.mut.Lock()
|
|
|
defer t.mut.Unlock()
|
|
|
+ if t.disabled {
|
|
|
+ l.Debugln("progress emitter: disabled, skip registering")
|
|
|
+ return
|
|
|
+ }
|
|
|
l.Debugln("progress emitter: registering", s.folder, s.file.Name)
|
|
|
- if len(t.registry) == 0 {
|
|
|
+ if t.emptyLocked() {
|
|
|
t.timer.Reset(t.interval)
|
|
|
}
|
|
|
- // Separate the folder ID (arbitrary string) and the file name by "//"
|
|
|
- // because it never appears in a valid file name.
|
|
|
- t.registry[s.folder+"//"+s.file.Name] = s
|
|
|
+ if _, ok := t.registry[s.folder]; !ok {
|
|
|
+ t.registry[s.folder] = make(map[string]*sharedPullerState)
|
|
|
+ }
|
|
|
+ t.registry[s.folder][s.file.Name] = s
|
|
|
}
|
|
|
|
|
|
// Deregister a puller which will stop broadcasting pullers state.
|
|
@@ -223,9 +241,13 @@ func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
|
|
|
t.mut.Lock()
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
|
- l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
|
|
|
+ if t.disabled {
|
|
|
+ l.Debugln("progress emitter: disabled, skip deregistering")
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- delete(t.registry, s.folder+"//"+s.file.Name)
|
|
|
+ l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
|
|
|
+ delete(t.registry[s.folder], s.file.Name)
|
|
|
}
|
|
|
|
|
|
// BytesCompleted returns the number of bytes completed in the given folder.
|
|
@@ -233,10 +255,8 @@ func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
|
|
|
t.mut.Lock()
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
|
- for _, s := range t.registry {
|
|
|
- if s.folder == folder {
|
|
|
- bytes += s.Progress().BytesDone
|
|
|
- }
|
|
|
+ for _, s := range t.registry[folder] {
|
|
|
+ bytes += s.Progress().BytesDone
|
|
|
}
|
|
|
l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
|
|
|
return
|
|
@@ -249,40 +269,53 @@ func (t *ProgressEmitter) String() string {
|
|
|
func (t *ProgressEmitter) lenRegistry() int {
|
|
|
t.mut.Lock()
|
|
|
defer t.mut.Unlock()
|
|
|
- return len(t.registry)
|
|
|
+ return t.lenRegistryLocked()
|
|
|
}
|
|
|
|
|
|
-func (t *ProgressEmitter) temporaryIndexSubscribe(conn protocol.Connection, folders []string) {
|
|
|
- t.mut.Lock()
|
|
|
- for _, folder := range folders {
|
|
|
- t.connections[folder] = append(t.connections[folder], conn)
|
|
|
+func (t *ProgressEmitter) lenRegistryLocked() (out int) {
|
|
|
+ for _, pullers := range t.registry {
|
|
|
+ out += len(pullers)
|
|
|
}
|
|
|
- t.mut.Unlock()
|
|
|
+ return out
|
|
|
}
|
|
|
|
|
|
-func (t *ProgressEmitter) temporaryIndexUnsubscribe(conn protocol.Connection) {
|
|
|
- t.mut.Lock()
|
|
|
- left := make(map[string][]protocol.Connection, len(t.connections))
|
|
|
- for folder, conns := range t.connections {
|
|
|
- connsLeft := connsWithout(conns, conn)
|
|
|
- if len(connsLeft) > 0 {
|
|
|
- left[folder] = connsLeft
|
|
|
+func (t *ProgressEmitter) emptyLocked() bool {
|
|
|
+ for _, pullers := range t.registry {
|
|
|
+ if len(pullers) != 0 {
|
|
|
+ return false
|
|
|
}
|
|
|
}
|
|
|
- t.connections = left
|
|
|
- t.mut.Unlock()
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
-func connsWithout(conns []protocol.Connection, conn protocol.Connection) []protocol.Connection {
|
|
|
- if len(conns) == 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
+func (t *ProgressEmitter) temporaryIndexSubscribe(conn protocol.Connection, folders []string) {
|
|
|
+ t.mut.Lock()
|
|
|
+ defer t.mut.Unlock()
|
|
|
+ t.connections[conn.ID()] = conn
|
|
|
+ t.foldersByConns[conn.ID()] = folders
|
|
|
+}
|
|
|
+
|
|
|
+func (t *ProgressEmitter) temporaryIndexUnsubscribe(conn protocol.Connection) {
|
|
|
+ t.mut.Lock()
|
|
|
+ defer t.mut.Unlock()
|
|
|
+ delete(t.connections, conn.ID())
|
|
|
+ delete(t.foldersByConns, conn.ID())
|
|
|
+}
|
|
|
|
|
|
- newConns := make([]protocol.Connection, 0, len(conns)-1)
|
|
|
- for _, existingConn := range conns {
|
|
|
- if existingConn != conn {
|
|
|
- newConns = append(newConns, existingConn)
|
|
|
+func (t *ProgressEmitter) clearLocked() {
|
|
|
+ for id, state := range t.sentDownloadStates {
|
|
|
+ conn, ok := t.connections[id]
|
|
|
+ if !ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for _, folder := range state.folders() {
|
|
|
+ if updates := state.cleanup(folder); len(updates) > 0 {
|
|
|
+ conn.DownloadProgress(folder, updates)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- return newConns
|
|
|
+ t.registry = make(map[string]map[string]*sharedPullerState)
|
|
|
+ t.sentDownloadStates = make(map[protocol.DeviceID]*sentDownloadState)
|
|
|
+ t.connections = make(map[protocol.DeviceID]protocol.Connection)
|
|
|
+ t.foldersByConns = make(map[protocol.DeviceID][]string)
|
|
|
}
|