2014-12-08 06:02:27 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-12-08 06:02:27 -07:00
|
|
|
|
2014-11-16 16:18:59 -07:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2015-06-03 00:47:39 -07:00
|
|
|
"fmt"
|
2014-11-16 16:18:59 -07:00
|
|
|
"time"
|
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-04-15 03:59:41 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-11-16 16:18:59 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProgressEmitter struct {
|
2016-04-15 03:59:41 -07:00
|
|
|
registry map[string]*sharedPullerState
|
|
|
|
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
|
|
|
|
mut sync.Mutex
|
2014-11-16 16:18:59 -07:00
|
|
|
|
|
|
|
timer *time.Timer
|
|
|
|
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
// NewProgressEmitter creates a new progress emitter which emits
|
|
|
|
// DownloadProgress events every interval.
|
2014-12-08 08:39:11 -07:00
|
|
|
func NewProgressEmitter(cfg *config.Wrapper) *ProgressEmitter {
|
2014-11-16 16:18:59 -07:00
|
|
|
t := &ProgressEmitter{
|
2016-04-15 03:59:41 -07:00
|
|
|
stop: make(chan struct{}),
|
|
|
|
registry: make(map[string]*sharedPullerState),
|
|
|
|
timer: time.NewTimer(time.Millisecond),
|
|
|
|
sentDownloadStates: make(map[protocol.DeviceID]*sentDownloadState),
|
|
|
|
connections: make(map[string][]protocol.Connection),
|
|
|
|
mut: sync.NewMutex(),
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
2015-06-03 00:47:39 -07:00
|
|
|
|
2016-11-12 02:34:18 -07:00
|
|
|
t.CommitConfiguration(config.Configuration{}, cfg.RawCopy())
|
2014-11-16 16:18:59 -07:00
|
|
|
cfg.Subscribe(t)
|
2015-06-03 00:47:39 -07:00
|
|
|
|
2014-11-16 16:18:59 -07:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
// Serve starts the progress emitter which starts emitting DownloadProgress
|
|
|
|
// events as the progress happens.
|
2014-11-16 16:18:59 -07:00
|
|
|
func (t *ProgressEmitter) Serve() {
|
2016-04-15 03:59:41 -07:00
|
|
|
var lastUpdate time.Time
|
|
|
|
var lastCount, newCount int
|
2014-11-16 16:18:59 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.stop:
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("progress emitter: stopping")
|
2014-11-16 16:18:59 -07:00
|
|
|
return
|
|
|
|
case <-t.timer.C:
|
2014-11-29 14:51:13 -07:00
|
|
|
t.mut.Lock()
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("progress emitter: timer - looking after", len(t.registry))
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
newLastUpdated := lastUpdate
|
|
|
|
newCount = len(t.registry)
|
2014-11-16 16:18:59 -07:00
|
|
|
for _, puller := range t.registry {
|
2016-04-15 03:59:41 -07:00
|
|
|
updated := puller.Updated()
|
|
|
|
if updated.After(newLastUpdated) {
|
|
|
|
newLastUpdated = updated
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
if !newLastUpdated.Equal(lastUpdate) || newCount != lastCount {
|
|
|
|
lastUpdate = newLastUpdated
|
|
|
|
lastCount = newCount
|
|
|
|
t.sendDownloadProgressEvent()
|
|
|
|
if len(t.connections) > 0 {
|
|
|
|
t.sendDownloadProgressMessages()
|
|
|
|
}
|
2015-10-03 08:25:21 -07:00
|
|
|
} else {
|
2014-11-16 16:18:59 -07:00
|
|
|
l.Debugln("progress emitter: nothing new")
|
|
|
|
}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
if newCount != 0 {
|
2014-11-16 16:18:59 -07:00
|
|
|
t.timer.Reset(t.interval)
|
|
|
|
}
|
|
|
|
t.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 03:59:41 -07:00
|
|
|
func (t *ProgressEmitter) sendDownloadProgressEvent() {
|
|
|
|
// registry lock already held
|
|
|
|
output := make(map[string]map[string]*pullerProgress)
|
|
|
|
for _, puller := range t.registry {
|
|
|
|
if output[puller.folder] == nil {
|
|
|
|
output[puller.folder] = make(map[string]*pullerProgress)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
|
|
state, ok := t.sentDownloadStates[id]
|
|
|
|
if !ok {
|
|
|
|
state = &sentDownloadState{
|
|
|
|
folderStates: make(map[string]*sentFolderDownloadState),
|
|
|
|
}
|
|
|
|
t.sentDownloadStates[id] = state
|
|
|
|
}
|
|
|
|
|
|
|
|
var activePullers []*sharedPullerState
|
|
|
|
for _, puller := range t.registry {
|
|
|
|
if puller.folder != folder || puller.file.IsSymlink() || puller.file.IsDirectory() || len(puller.file.Blocks) <= t.minBlocks {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
activePullers = append(activePullers, puller)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For every new puller that hasn't yet been seen, it will send all the blocks the puller has available
|
|
|
|
// For every existing puller, it will check for new blocks, and send update for the new blocks only
|
|
|
|
// For every puller that we've seen before but is no longer there, we will send a forget message
|
|
|
|
updates := state.update(folder, activePullers)
|
|
|
|
|
|
|
|
if len(updates) > 0 {
|
2016-07-04 03:40:29 -07:00
|
|
|
conn.DownloadProgress(folder, updates)
|
2016-04-15 03:59:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up sentDownloadStates for devices which we are no longer connected to.
|
|
|
|
for id := range t.sentDownloadStates {
|
|
|
|
_, ok := deviceConns[id]
|
|
|
|
if !ok {
|
|
|
|
// Null out outstanding entries for device
|
|
|
|
delete(t.sentDownloadStates, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 each of the folders that the state is aware of,
|
|
|
|
// try to match it with a shared folder we've discovered above,
|
|
|
|
for _, folder := range state.folders() {
|
|
|
|
for _, existingFolder := range sharedDeviceFolders {
|
|
|
|
if existingFolder == folder {
|
|
|
|
continue nextFolder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fail to find that folder, we tell the state to forget about it
|
|
|
|
// and return us a list of updates which would clean up the state
|
|
|
|
// on the remote end.
|
|
|
|
updates := state.cleanup(folder)
|
|
|
|
if len(updates) > 0 {
|
|
|
|
// XXX: Don't send this now, as the only way we've unshared a folder
|
|
|
|
// is by breaking the connection and reconnecting, hence sending
|
|
|
|
// forget messages for some random folder currently makes no sense.
|
|
|
|
// deviceConns[id].DownloadProgress(folder, updates, 0, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 00:47:39 -07:00
|
|
|
// VerifyConfiguration implements the config.Committer interface
|
|
|
|
func (t *ProgressEmitter) VerifyConfiguration(from, to config.Configuration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitConfiguration implements the config.Committer interface
|
|
|
|
func (t *ProgressEmitter) CommitConfiguration(from, to config.Configuration) bool {
|
2014-11-16 16:18:59 -07:00
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
2015-06-03 00:47:39 -07:00
|
|
|
t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
|
2016-08-16 11:22:01 -07:00
|
|
|
if t.interval < time.Second {
|
|
|
|
t.interval = time.Second
|
|
|
|
}
|
2016-04-15 03:59:41 -07:00
|
|
|
t.minBlocks = to.Options.TempIndexMinBlocks
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("progress emitter: updated interval", t.interval)
|
2015-06-03 00:47:39 -07:00
|
|
|
|
|
|
|
return true
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
// Stop stops the emitter.
|
2014-11-16 16:18:59 -07:00
|
|
|
func (t *ProgressEmitter) Stop() {
|
|
|
|
t.stop <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a puller with the emitter which will start broadcasting pullers
|
|
|
|
// progress.
|
|
|
|
func (t *ProgressEmitter) Register(s *sharedPullerState) {
|
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("progress emitter: registering", s.folder, s.file.Name)
|
2014-11-16 16:18:59 -07:00
|
|
|
if len(t.registry) == 0 {
|
|
|
|
t.timer.Reset(t.interval)
|
|
|
|
}
|
2017-02-25 01:12:13 -07:00
|
|
|
// Separate the folder ID (arbitrary string) and the file name by "//"
|
2016-12-01 05:34:20 -07:00
|
|
|
// because it never appears in a valid file name.
|
|
|
|
t.registry[s.folder+"//"+s.file.Name] = s
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 08:34:55 -07:00
|
|
|
// Deregister a puller which will stop broadcasting pullers state.
|
2014-11-16 16:18:59 -07:00
|
|
|
func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
|
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2016-12-01 05:34:20 -07:00
|
|
|
delete(t.registry, s.folder+"//"+s.file.Name)
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
// BytesCompleted returns the number of bytes completed in the given folder.
|
2014-11-16 16:18:59 -07:00
|
|
|
func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
|
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
2014-11-25 15:07:18 -07:00
|
|
|
for _, s := range t.registry {
|
|
|
|
if s.folder == folder {
|
|
|
|
bytes += s.Progress().BytesDone
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
|
2014-11-16 16:18:59 -07:00
|
|
|
return
|
|
|
|
}
|
2015-06-03 00:47:39 -07:00
|
|
|
|
|
|
|
func (t *ProgressEmitter) String() string {
|
|
|
|
return fmt.Sprintf("ProgressEmitter@%p", t)
|
|
|
|
}
|
2015-10-19 23:51:14 -07:00
|
|
|
|
|
|
|
func (t *ProgressEmitter) lenRegistry() int {
|
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
|
|
|
return len(t.registry)
|
|
|
|
}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
func (t *ProgressEmitter) temporaryIndexSubscribe(conn protocol.Connection, folders []string) {
|
|
|
|
t.mut.Lock()
|
|
|
|
for _, folder := range folders {
|
|
|
|
t.connections[folder] = append(t.connections[folder], conn)
|
|
|
|
}
|
|
|
|
t.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.connections = left
|
|
|
|
t.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func connsWithout(conns []protocol.Connection, conn protocol.Connection) []protocol.Connection {
|
|
|
|
if len(conns) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newConns := make([]protocol.Connection, 0, len(conns)-1)
|
|
|
|
for _, existingConn := range conns {
|
|
|
|
if existingConn != conn {
|
|
|
|
newConns = append(newConns, existingConn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newConns
|
|
|
|
}
|