queue.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stats
  14. import (
  15. "sync"
  16. "time"
  17. )
  18. const (
  19. queueCapacity = 200
  20. )
  21. // RequestStats represent the stats for a request.
  22. // It encapsulates the sending time and the size of the request.
  23. type RequestStats struct {
  24. SendingTime time.Time
  25. Size int
  26. }
  27. type statsQueue struct {
  28. items [queueCapacity]*RequestStats
  29. size int
  30. front int
  31. back int
  32. totalReqSize int
  33. rwl sync.RWMutex
  34. }
  35. func (q *statsQueue) Len() int {
  36. return q.size
  37. }
  38. func (q *statsQueue) ReqSize() int {
  39. return q.totalReqSize
  40. }
  41. // FrontAndBack gets the front and back elements in the queue
  42. // We must grab front and back together with the protection of the lock
  43. func (q *statsQueue) frontAndBack() (*RequestStats, *RequestStats) {
  44. q.rwl.RLock()
  45. defer q.rwl.RUnlock()
  46. if q.size != 0 {
  47. return q.items[q.front], q.items[q.back]
  48. }
  49. return nil, nil
  50. }
  51. // Insert function insert a RequestStats into the queue and update the records
  52. func (q *statsQueue) Insert(p *RequestStats) {
  53. q.rwl.Lock()
  54. defer q.rwl.Unlock()
  55. q.back = (q.back + 1) % queueCapacity
  56. if q.size == queueCapacity { //dequeue
  57. q.totalReqSize -= q.items[q.front].Size
  58. q.front = (q.back + 1) % queueCapacity
  59. } else {
  60. q.size++
  61. }
  62. q.items[q.back] = p
  63. q.totalReqSize += q.items[q.back].Size
  64. }
  65. // Rate function returns the package rate and byte rate
  66. func (q *statsQueue) Rate() (float64, float64) {
  67. front, back := q.frontAndBack()
  68. if front == nil || back == nil {
  69. return 0, 0
  70. }
  71. if time.Now().Sub(back.SendingTime) > time.Second {
  72. q.Clear()
  73. return 0, 0
  74. }
  75. sampleDuration := back.SendingTime.Sub(front.SendingTime)
  76. pr := float64(q.Len()) / float64(sampleDuration) * float64(time.Second)
  77. br := float64(q.ReqSize()) / float64(sampleDuration) * float64(time.Second)
  78. return pr, br
  79. }
  80. // Clear function clear up the statsQueue
  81. func (q *statsQueue) Clear() {
  82. q.rwl.Lock()
  83. defer q.rwl.Unlock()
  84. q.back = -1
  85. q.front = 0
  86. q.size = 0
  87. q.totalReqSize = 0
  88. }