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