progressemitter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // 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. mut: sync.NewMutex(),
  32. }
  33. t.Changed(cfg.Raw())
  34. cfg.Subscribe(t)
  35. return t
  36. }
  37. // Starts progress emitter which starts emitting DownloadProgress events as
  38. // 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. // Interface method to handle configuration changes
  76. func (t *ProgressEmitter) Changed(cfg config.Configuration) error {
  77. t.mut.Lock()
  78. defer t.mut.Unlock()
  79. t.interval = time.Duration(cfg.Options.ProgressUpdateIntervalS) * time.Second
  80. if debug {
  81. l.Debugln("progress emitter: updated interval", t.interval)
  82. }
  83. return nil
  84. }
  85. // Stops the emitter.
  86. func (t *ProgressEmitter) Stop() {
  87. t.stop <- struct{}{}
  88. }
  89. // Register a puller with the emitter which will start broadcasting pullers
  90. // progress.
  91. func (t *ProgressEmitter) Register(s *sharedPullerState) {
  92. t.mut.Lock()
  93. defer t.mut.Unlock()
  94. if debug {
  95. l.Debugln("progress emitter: registering", s.folder, s.file.Name)
  96. }
  97. if len(t.registry) == 0 {
  98. t.timer.Reset(t.interval)
  99. }
  100. t.registry[filepath.Join(s.folder, s.file.Name)] = s
  101. }
  102. // Deregister a puller which will stop boardcasting pullers state.
  103. func (t *ProgressEmitter) Deregister(s *sharedPullerState) {
  104. t.mut.Lock()
  105. defer t.mut.Unlock()
  106. if debug {
  107. l.Debugln("progress emitter: deregistering", s.folder, s.file.Name)
  108. }
  109. delete(t.registry, filepath.Join(s.folder, s.file.Name))
  110. }
  111. // Returns number of bytes completed in the given folder.
  112. func (t *ProgressEmitter) BytesCompleted(folder string) (bytes int64) {
  113. t.mut.Lock()
  114. defer t.mut.Unlock()
  115. for _, s := range t.registry {
  116. if s.folder == folder {
  117. bytes += s.Progress().BytesDone
  118. }
  119. }
  120. if debug {
  121. l.Debugf("progress emitter: bytes completed for %s: %d", folder, bytes)
  122. }
  123. return
  124. }