sample.go 888 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package vegas
  2. import (
  3. "sync/atomic"
  4. )
  5. type sample struct {
  6. count int64
  7. maxInFlight int64
  8. drop int64
  9. // nanoseconds
  10. totalRTT int64
  11. }
  12. func (s *sample) Add(rtt int64, inFlight int64, drop bool) {
  13. if drop {
  14. atomic.StoreInt64(&s.drop, 1)
  15. }
  16. for max := atomic.LoadInt64(&s.maxInFlight); max < inFlight; max = atomic.LoadInt64(&s.maxInFlight) {
  17. if atomic.CompareAndSwapInt64(&s.maxInFlight, max, inFlight) {
  18. break
  19. }
  20. }
  21. atomic.AddInt64(&s.totalRTT, rtt)
  22. atomic.AddInt64(&s.count, 1)
  23. }
  24. func (s *sample) RTT() int64 {
  25. count := atomic.LoadInt64(&s.count)
  26. if count == 0 {
  27. return 0
  28. }
  29. return atomic.LoadInt64(&s.totalRTT) / count
  30. }
  31. func (s *sample) MaxInFlight() int64 {
  32. return atomic.LoadInt64(&s.maxInFlight)
  33. }
  34. func (s *sample) Count() int64 {
  35. return atomic.LoadInt64(&s.count)
  36. }
  37. func (s *sample) Drop() bool {
  38. return atomic.LoadInt64(&s.drop) == 1
  39. }