Skip to content
Snippets Groups Projects
Commit 660365ed authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix formatting

parent 68c03dbe
No related branches found
No related tags found
2 merge requests!510Release,!340Project/channels
package attempts package attempts
import ( import (
"fmt"
"sort" "sort"
"strconv"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
) )
const maxHistogramSize = 100 const (
const minElements = 3 maxHistogramSize = 100
const percentileNumerator = 66 minElements = 3
const percentileDenominator = 99 percentileNumerator = 66
const percentileDenominatorOffset = 49 percentileDenominator = 99
percentileDenominatorOffset = 49
optimalAttemptsInitValue = -1
)
type SendAttemptTracker interface { type SendAttemptTracker interface {
SubmitProbeAttempt(numAttemptsUntilSuccessful int) SubmitProbeAttempt(numAttemptsUntilSuccessful int)
...@@ -18,32 +24,32 @@ type SendAttemptTracker interface { ...@@ -18,32 +24,32 @@ type SendAttemptTracker interface {
} }
type sendAttempts struct { type sendAttempts struct {
lock sync.Mutex
numAttempts []int
currentIndex int
isFull bool
optimalAttempts *int32 optimalAttempts *int32
isFull bool
currentIndex int
numAttempts []int
lock sync.Mutex
} }
func NewSendAttempts() SendAttemptTracker { func NewSendAttempts() SendAttemptTracker {
optimalAttempts := int32(-1) optimalAttempts := int32(optimalAttemptsInitValue)
sa := &sendAttempts{ sa := &sendAttempts{
numAttempts: make([]int, maxHistogramSize),
currentIndex: 0,
isFull: false,
optimalAttempts: &optimalAttempts, optimalAttempts: &optimalAttempts,
isFull: false,
currentIndex: 0,
numAttempts: make([]int, maxHistogramSize),
} }
return sa return sa
} }
func (sa *sendAttempts) SubmitProbeAttempt(a int) { func (sa *sendAttempts) SubmitProbeAttempt(numAttemptsUntilSuccessful int) {
sa.lock.Lock() sa.lock.Lock()
defer sa.lock.Unlock() defer sa.lock.Unlock()
sa.numAttempts[sa.currentIndex] = a sa.numAttempts[sa.currentIndex] = numAttemptsUntilSuccessful
sa.currentIndex += 1 sa.currentIndex++
if sa.currentIndex == len(sa.numAttempts) { if sa.currentIndex == len(sa.numAttempts) {
sa.currentIndex = 0 sa.currentIndex = 0
sa.isFull = true sa.isFull = true
...@@ -55,7 +61,7 @@ func (sa *sendAttempts) SubmitProbeAttempt(a int) { ...@@ -55,7 +61,7 @@ func (sa *sendAttempts) SubmitProbeAttempt(a int) {
func (sa *sendAttempts) GetOptimalNumAttempts() (attempts int, ready bool) { func (sa *sendAttempts) GetOptimalNumAttempts() (attempts int, ready bool) {
optimalAttempts := atomic.LoadInt32(sa.optimalAttempts) optimalAttempts := atomic.LoadInt32(sa.optimalAttempts)
if optimalAttempts == -1 { if optimalAttempts == optimalAttemptsInitValue {
return 0, false return 0, false
} }
...@@ -69,16 +75,28 @@ func (sa *sendAttempts) computeOptimalUnsafe() { ...@@ -69,16 +75,28 @@ func (sa *sendAttempts) computeOptimalUnsafe() {
return return
} }
toCopy = sa.currentIndex toCopy = sa.currentIndex
} }
histoCopy := make([]int, toCopy) histogramCopy := make([]int, toCopy)
copy(histoCopy, sa.numAttempts[:toCopy]) copy(histogramCopy, sa.numAttempts[:toCopy])
sort.Ints(histogramCopy)
sort.Slice(histoCopy, func(i, j int) bool {
return histoCopy[i] < histoCopy[j]
})
optimal := histoCopy[((toCopy*percentileNumerator)+percentileDenominatorOffset)/percentileDenominator] i := ((toCopy * percentileNumerator) + percentileDenominatorOffset) /
percentileDenominator
optimal := histogramCopy[i]
atomic.StoreInt32(sa.optimalAttempts, int32(optimal)) atomic.StoreInt32(sa.optimalAttempts, int32(optimal))
} }
// String prints the values in the sendAttempts in a human-readable form for
// debugging and logging purposes. This function adheres to the fmt.Stringer
// interface.
func (sa *sendAttempts) String() string {
fields := []string{
"optimalAttempts:" + strconv.Itoa(int(atomic.LoadInt32(sa.optimalAttempts))),
"isFull:" + strconv.FormatBool(sa.isFull),
"currentIndex:" + strconv.Itoa(sa.currentIndex),
"numAttempts:" + fmt.Sprintf("%d", sa.numAttempts),
}
return "{" + strings.Join(fields, " ") + "}"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment