progressemitter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "path/filepath"
  9. "reflect"
  10. "time"
  11. "github.com/syncthing/syncthing/internal/config"
  12. "github.com/syncthing/syncthing/internal/events"
  13. "github.com/syncthing/syncthing/internal/sync"
  14. )
  15. type ProgressEmitter struct {
  16. registry map[string]*sharedPullerState
  17. interval time.Duration
  18. last map[string]map[string]*pullerProgress
  19. mut sync.Mutex
  20. timer *time.Timer
  21. stop chan struct{}
  22. }
  23. // NewProgressEmitter creates a new progress emitter which emits
  24. // DownloadProgress events every interval.
  25. func NewProgressEmitter(cfg *config.Wrapper) *ProgressEmitter {
  26. t := &ProgressEmitter{
  27. stop: make(chan struct{}),
  28. registry: make(map[string]*sharedPullerState),
  29. last: make(map[string]map[string]*pullerProgress),
  30. timer: time.NewTimer(time.Millisecond),
  31. mut: sync.NewMutex(),
  32. }
  33. t.Changed(cfg.Raw())
  34. cfg.Subscribe(t)
  35. return t
  36. }
  37. // Serve starts the progress emitter which starts emitting DownloadProgress
  38. // events as the progress happens.
  39. func (t *ProgressEmitter) Serve() {
  40. for {
  41. select {
  42. case <-t.stop:
  43. if debug {
  44. l.Debugln("progress emitter: stopping")
  45. }
  46. return
  47. case <-t.timer.C:
  48. t.mut.Lock()
  49. if debug {
  50. l.Debugln("progress emitter: timer - looking after", len(t.registry))
  51. }
  52. output := make(map[string]map[string]*pullerProgress)
  53. for _, puller := range t.registry {
  54. if output[puller.folder] == nil {
  55. output[puller.folder] = make(map[string]*pullerProgress)
  56. }
  57. output[puller.folder][puller.file.Name] = puller.Progress()
  58. }
  59. if !reflect.DeepEqual(t.last, output) {
  60. events.Default.Log(events.DownloadProgress, output)
  61. t.last = output
  62. if debug {
  63. l.Debugf("progress emitter: emitting %#v", output)
  64. }
  65. } else if debug {
  66. l.Debugln("progress emitter: nothing new")
  67. }
  68. if len(t.registry) != 0 {
  69. t.timer.Reset(t.interval)
  70. }
  71. t.mut.Unlock()
  72. }
  73. }
  74. }
  75. // Changed implements the config.Handler Interface to handle configuration
  76. // changes
  77. func (t *ProgressEmitter) Changed(cfg config.Configuration) error {
  78. t.mut.Lock()
  79. defer t.mut.Unlock()
  80. t.interval = time.Duration(cfg.Options.ProgressUpdateIntervalS) * time.Second
  81. if debug {
  82. l.Debugln("progress emitter: updated interval", t.interval)
  83. }
  84. return nil
  85. }
  86. // Stop stops the emitter.
  87. func (t *ProgressEmitter) Stop() {
  88. t.stop <- struct{}{}
  89. }
  90. // Register a puller with the emitter which will start broadcasting pullers
  91. // progress.
  92. func (t *ProgressEmitter) Register(s *sharedPullerState) {
  93. t.mut.Lock()
  94. defer t.mut.Unlock()
  95. if debug {
  96. l.Debugln("progress emitter: registering", s.folder, s.file.Name)
  97. }
  98. if len(t.registry) == 0 {
  99. t.timer.Reset(t.interval)
  100. }
  101. t.registry[filepath.Join(s.folder, s.file.Name)] = s
  102. }
  103. // Deregister a puller which will stop broadcasting pullers state.
  104. func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
  105. t.mut.Lock()
  106. defer t.mut.Unlock()
  107. if debug {
  108. l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
  109. }
  110. delete(t.registry, filepath.Join(s.folder, s.file.Name))
  111. }
  112. // BytesCompleted returns the number of bytes completed in the given folder.
  113. func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
  114. t.mut.Lock()
  115. defer t.mut.Unlock()
  116. for _, s := range t.registry {
  117. if s.folder == folder {
  118. bytes += s.Progress().BytesDone
  119. }
  120. }
  121. if debug {
  122. l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
  123. }
  124. return
  125. }