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