2018-01-09 16:40:37 +01:00
|
|
|
package main
|
|
|
|
|
2018-01-10 09:46:27 +01:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VividCortex/ewma"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-01-10 16:42:14 +01:00
|
|
|
SizeEstimatorEwmaDecay = 100.0
|
2018-01-10 09:46:27 +01:00
|
|
|
)
|
2018-01-09 16:40:37 +01:00
|
|
|
|
|
|
|
type QuestionSizeEstimator struct {
|
|
|
|
sync.RWMutex
|
|
|
|
minQuestionSize int
|
2018-01-10 09:46:27 +01:00
|
|
|
ewma ewma.MovingAverage
|
2018-01-09 16:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewQuestionSizeEstimator() QuestionSizeEstimator {
|
2018-01-10 16:42:14 +01:00
|
|
|
return QuestionSizeEstimator{minQuestionSize: InitialMinQuestionSize, ewma: ewma.NewMovingAverage(SizeEstimatorEwmaDecay)}
|
2018-01-09 16:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (questionSizeEstimator *QuestionSizeEstimator) MinQuestionSize() int {
|
|
|
|
questionSizeEstimator.RLock()
|
|
|
|
minQuestionSize := questionSizeEstimator.minQuestionSize
|
|
|
|
questionSizeEstimator.RUnlock()
|
|
|
|
return minQuestionSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (questionSizeEstimator *QuestionSizeEstimator) blindAdjust() {
|
|
|
|
questionSizeEstimator.Lock()
|
2019-10-15 16:44:22 +02:00
|
|
|
questionSizeEstimator.minQuestionSize = Min(MaxDNSUDPPacketSize, questionSizeEstimator.minQuestionSize*2)
|
2018-01-10 09:46:27 +01:00
|
|
|
questionSizeEstimator.ewma.Set(float64(questionSizeEstimator.minQuestionSize))
|
|
|
|
questionSizeEstimator.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (questionSizeEstimator *QuestionSizeEstimator) adjust(packetSize int) {
|
|
|
|
questionSizeEstimator.Lock()
|
|
|
|
questionSizeEstimator.ewma.Add(float64(packetSize))
|
|
|
|
ma, minQuestionSize := int(questionSizeEstimator.ewma.Value()), questionSizeEstimator.minQuestionSize
|
|
|
|
if ma > InitialMinQuestionSize && ma < minQuestionSize/2 {
|
|
|
|
questionSizeEstimator.minQuestionSize = Max(InitialMinQuestionSize, minQuestionSize/2)
|
|
|
|
}
|
2018-01-09 16:40:37 +01:00
|
|
|
questionSizeEstimator.Unlock()
|
|
|
|
}
|