host_entry.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package hostpool
  2. import (
  3. "time"
  4. )
  5. // --- hostEntry - this is due to get upgraded
  6. type hostEntry struct {
  7. host string
  8. nextRetry time.Time
  9. retryCount int16
  10. retryDelay time.Duration
  11. dead bool
  12. epsilonCounts []int64
  13. epsilonValues []int64
  14. epsilonIndex int
  15. epsilonValue float64
  16. epsilonPercentage float64
  17. }
  18. func (h *hostEntry) canTryHost(now time.Time) bool {
  19. if !h.dead {
  20. return true
  21. }
  22. if h.nextRetry.Before(now) {
  23. return true
  24. }
  25. return false
  26. }
  27. func (h *hostEntry) willRetryHost(maxRetryInterval time.Duration) {
  28. h.retryCount += 1
  29. newDelay := h.retryDelay * 2
  30. if newDelay < maxRetryInterval {
  31. h.retryDelay = newDelay
  32. } else {
  33. h.retryDelay = maxRetryInterval
  34. }
  35. h.nextRetry = time.Now().Add(h.retryDelay)
  36. }
  37. func (h *hostEntry) getWeightedAverageResponseTime() float64 {
  38. var value float64
  39. var lastValue float64
  40. // start at 1 so we start with the oldest entry
  41. for i := 1; i <= epsilonBuckets; i += 1 {
  42. pos := (h.epsilonIndex + i) % epsilonBuckets
  43. bucketCount := h.epsilonCounts[pos]
  44. // Changing the line below to what I think it should be to get the weights right
  45. weight := float64(i) / float64(epsilonBuckets)
  46. if bucketCount > 0 {
  47. currentValue := float64(h.epsilonValues[pos]) / float64(bucketCount)
  48. value += currentValue * weight
  49. lastValue = currentValue
  50. } else {
  51. value += lastValue * weight
  52. }
  53. }
  54. return value
  55. }