progressemitter.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if debug {
  45. l.Debugln("progress emitter: stopping")
  46. }
  47. return
  48. case <-t.timer.C:
  49. t.mut.Lock()
  50. if debug {
  51. l.Debugln("progress emitter: timer - looking after", len(t.registry))
  52. }
  53. output := make(map[string]map[string]*pullerProgress)
  54. for _, puller := range t.registry {
  55. if output[puller.folder] == nil {
  56. output[puller.folder] = make(map[string]*pullerProgress)
  57. }
  58. output[puller.folder][puller.file.Name] = puller.Progress()
  59. }
  60. if !reflect.DeepEqual(t.last, output) {
  61. events.Default.Log(events.DownloadProgress, output)
  62. t.last = output
  63. if debug {
  64. l.Debugf("progress emitter: emitting %#v", output)
  65. }
  66. } else if debug {
  67. l.Debugln("progress emitter: nothing new")
  68. }
  69. if len(t.registry) != 0 {
  70. t.timer.Reset(t.interval)
  71. }
  72. t.mut.Unlock()
  73. }
  74. }
  75. }
  76. // VerifyConfiguration implements the config.Committer interface
  77. func (t *ProgressEmitter) VerifyConfiguration(from, to config.Configuration) error {
  78. return nil
  79. }
  80. // CommitConfiguration implements the config.Committer interface
  81. func (t *ProgressEmitter) CommitConfiguration(from, to config.Configuration) bool {
  82. t.mut.Lock()
  83. defer t.mut.Unlock()
  84. t.interval = time.Duration(to.Options.ProgressUpdateIntervalS) * time.Second
  85. if debug {
  86. l.Debugln("progress emitter: updated interval", t.interval)
  87. }
  88. return true
  89. }
  90. // Stop stops the emitter.
  91. func (t *ProgressEmitter) Stop() {
  92. t.stop <- struct{}{}
  93. }
  94. // Register a puller with the emitter which will start broadcasting pullers
  95. // progress.
  96. func (t *ProgressEmitter) Register(s *sharedPullerState) {
  97. t.mut.Lock()
  98. defer t.mut.Unlock()
  99. if debug {
  100. l.Debugln("progress emitter: registering", s.folder, s.file.Name)
  101. }
  102. if len(t.registry) == 0 {
  103. t.timer.Reset(t.interval)
  104. }
  105. t.registry[filepath.Join(s.folder, s.file.Name)] = s
  106. }
  107. // Deregister a puller which will stop broadcasting pullers state.
  108. func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
  109. t.mut.Lock()
  110. defer t.mut.Unlock()
  111. if debug {
  112. l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
  113. }
  114. delete(t.registry, filepath.Join(s.folder, s.file.Name))
  115. }
  116. // BytesCompleted returns the number of bytes completed in the given folder.
  117. func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
  118. t.mut.Lock()
  119. defer t.mut.Unlock()
  120. for _, s := range t.registry {
  121. if s.folder == folder {
  122. bytes += s.Progress().BytesDone
  123. }
  124. }
  125. if debug {
  126. l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
  127. }
  128. return
  129. }
  130. func (t *ProgressEmitter) String() string {
  131. return fmt.Sprintf("ProgressEmitter@%p", t)
  132. }