suppressor.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package model
  5. import (
  6. "os"
  7. "sync"
  8. "time"
  9. )
  10. const (
  11. MaxChangeHistory = 4
  12. )
  13. type change struct {
  14. size int64
  15. when time.Time
  16. }
  17. type changeHistory struct {
  18. changes []change
  19. next int64
  20. prevSup bool
  21. }
  22. type suppressor struct {
  23. sync.Mutex
  24. changes map[string]changeHistory
  25. threshold int64 // bytes/s
  26. }
  27. func (h changeHistory) bandwidth(t time.Time) int64 {
  28. if len(h.changes) == 0 {
  29. return 0
  30. }
  31. var t0 = h.changes[0].when
  32. if t == t0 {
  33. return 0
  34. }
  35. var bw float64
  36. for _, c := range h.changes {
  37. bw += float64(c.size)
  38. }
  39. return int64(bw / t.Sub(t0).Seconds())
  40. }
  41. func (h *changeHistory) append(size int64, t time.Time) {
  42. c := change{size, t}
  43. if len(h.changes) == MaxChangeHistory {
  44. h.changes = h.changes[1:MaxChangeHistory]
  45. }
  46. h.changes = append(h.changes, c)
  47. }
  48. func (s *suppressor) Suppress(name string, fi os.FileInfo) (cur, prev bool) {
  49. return s.suppress(name, fi.Size(), time.Now())
  50. }
  51. func (s *suppressor) suppress(name string, size int64, t time.Time) (bool, bool) {
  52. s.Lock()
  53. if s.changes == nil {
  54. s.changes = make(map[string]changeHistory)
  55. }
  56. h := s.changes[name]
  57. sup := h.bandwidth(t) > s.threshold
  58. prevSup := h.prevSup
  59. h.prevSup = sup
  60. if !sup {
  61. h.append(size, t)
  62. }
  63. s.changes[name] = h
  64. s.Unlock()
  65. return sup, prevSup
  66. }