progressemitter.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "fmt"
  9. "path/filepath"
  10. "reflect"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/events"
  14. "github.com/syncthing/syncthing/lib/sync"
  15. )
  16. type ProgressEmitter struct {
  17. registry map[string]*sharedPullerState
  18. interval time.Duration
  19. last map[string]map[string]*pullerProgress
  20. mut sync.Mutex
  21. timer *time.Timer
  22. stop chan struct{}
  23. }
  24. // NewProgressEmitter creates a new progress emitter which emits
  25. // DownloadProgress events every interval.
  26. func NewProgressEmitter(cfg *config.Wrapper) *ProgressEmitter {
  27. t := &ProgressEmitter{
  28. stop: make(chan struct{}),
  29. registry: make(map[string]*sharedPullerState),
  30. last: make(map[string]map[string]*pullerProgress),
  31. timer: time.NewTimer(time.Millisecond),
  32. mut: sync.NewMutex(),
  33. }
  34. t.CommitConfiguration(config.Configuration{}, cfg.Raw())
  35. cfg.Subscribe(t)
  36. return t
  37. }
  38. // Serve starts the progress emitter which starts emitting DownloadProgress
  39. // events as the progress happens.
  40. func (t *ProgressEmitter) Serve() {
  41. for {
  42. select {
  43. case <-t.stop:
  44. l.Debugln("progress emitter: stopping")
  45. return
  46. case <-t.timer.C:
  47. t.mut.Lock()
  48. l.Debugln("progress emitter: timer - looking after", len(t.registry))
  49. output := make(map[string]map[string]*pullerProgress)
  50. for _, puller := range t.registry {
  51. if output[puller.folder] == nil {
  52. output[puller.folder] = make(map[string]*pullerProgress)
  53. }
  54. output[puller.folder][puller.file.Name] = puller.Progress()
  55. }
  56. if !reflect.DeepEqual(t.last, output) {
  57. events.Default.Log(events.DownloadProgress, output)
  58. t.last = output
  59. l.Debugf("progress emitter: emitting %#v", output)
  60. } else {
  61. l.Debugln("progress emitter: nothing new")
  62. }
  63. if len(t.registry) != 0 {
  64. t.timer.Reset(t.interval)
  65. }
  66. t.mut.Unlock()
  67. }
  68. }
  69. }
  70. // VerifyConfiguration implements the config.Committer interface
  71. func (t *ProgressEmitter) VerifyConfiguration(from, to config.Configuration) error {
  72. return nil
  73. }
  74. // CommitConfiguration implements the config.Committer interface
  75. func (t *ProgressEmitter) CommitConfiguration(from, to config.Configuration) bool {
  76. t.mut.Lock()
  77. defer t.mut.Unlock()
  78. t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
  79. l.Debugln("progress emitter: updated interval", t.interval)
  80. return true
  81. }
  82. // Stop stops the emitter.
  83. func (t *ProgressEmitter) Stop() {
  84. t.stop <- struct{}{}
  85. }
  86. // Register a puller with the emitter which will start broadcasting pullers
  87. // progress.
  88. func (t *ProgressEmitter) Register(s *sharedPullerState) {
  89. t.mut.Lock()
  90. defer t.mut.Unlock()
  91. l.Debugln("progress emitter: registering", s.folder, s.file.Name)
  92. if len(t.registry) == 0 {
  93. t.timer.Reset(t.interval)
  94. }
  95. t.registry[filepath.Join(s.folder, s.file.Name)] = s
  96. }
  97. // Deregister a puller which will stop broadcasting pullers state.
  98. func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
  99. t.mut.Lock()
  100. defer t.mut.Unlock()
  101. l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
  102. delete(t.registry, filepath.Join(s.folder, s.file.Name))
  103. }
  104. // BytesCompleted returns the number of bytes completed in the given folder.
  105. func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
  106. t.mut.Lock()
  107. defer t.mut.Unlock()
  108. for _, s := range t.registry {
  109. if s.folder == folder {
  110. bytes += s.Progress().BytesDone
  111. }
  112. }
  113. l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
  114. return
  115. }
  116. func (t *ProgressEmitter) String() string {
  117. return fmt.Sprintf("ProgressEmitter@%p", t)
  118. }