diff --git a/CHANGELOG.md b/CHANGELOG.md index 94638866a..5c67e36ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [CHANGE] spanlogger: Take interface implementation for extracting tenant ID. #59 * [CHANGE] The `status_code` label on gRPC client metrics has changed from '200' and '500' to '2xx', '5xx', '4xx', 'cancel' or 'error'. #68 * [CHANGE] Memberlist: changed probe interval from `1s` to `5s` and probe timeout from `500ms` to `2s`. #90 +* [CHANGE] Remove package `math`. #104 * [ENHANCEMENT] Add middleware package. #38 * [ENHANCEMENT] Add the ring package #45 * [ENHANCEMENT] Add limiter package. #41 diff --git a/concurrency/runner.go b/concurrency/runner.go index 846b136cf..a6740f3ac 100644 --- a/concurrency/runner.go +++ b/concurrency/runner.go @@ -6,7 +6,7 @@ import ( "golang.org/x/sync/errgroup" - "github.com/grafana/dskit/math" + "github.com/grafana/dskit/internal/math" "github.com/grafana/dskit/multierror" ) diff --git a/internal/math/math.go b/internal/math/math.go new file mode 100644 index 000000000..9d6422e50 --- /dev/null +++ b/internal/math/math.go @@ -0,0 +1,9 @@ +package math + +// Min returns the minimum of two ints. +func Min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/math/math.go b/math/math.go deleted file mode 100644 index 01e544384..000000000 --- a/math/math.go +++ /dev/null @@ -1,33 +0,0 @@ -package math - -// Max returns the maximum of two ints -func Max(a, b int) int { - if a > b { - return a - } - return b -} - -// Min returns the minimum of two ints -func Min(a, b int) int { - if a < b { - return a - } - return b -} - -// Max64 returns the maximum of two int64s -func Max64(a, b int64) int64 { - if a > b { - return a - } - return b -} - -// Min64 returns the minimum of two int64s -func Min64(a, b int64) int64 { - if a < b { - return a - } - return b -} diff --git a/math/rate.go b/math/rate.go deleted file mode 100644 index 19bbe6428..000000000 --- a/math/rate.go +++ /dev/null @@ -1,59 +0,0 @@ -package math - -import ( - "sync" - "time" - - "go.uber.org/atomic" -) - -// EwmaRate tracks an exponentially weighted moving average of a per-second rate. -type EwmaRate struct { - newEvents atomic.Int64 - - alpha float64 - interval time.Duration - - mutex sync.RWMutex - lastRate float64 - init bool -} - -func NewEWMARate(alpha float64, interval time.Duration) *EwmaRate { - return &EwmaRate{ - alpha: alpha, - interval: interval, - } -} - -// Rate returns the per-second rate. -func (r *EwmaRate) Rate() float64 { - r.mutex.RLock() - defer r.mutex.RUnlock() - return r.lastRate -} - -// Tick assumes to be called every r.interval. -func (r *EwmaRate) Tick() { - newEvents := r.newEvents.Swap(0) - instantRate := float64(newEvents) / r.interval.Seconds() - - r.mutex.Lock() - defer r.mutex.Unlock() - - if r.init { - r.lastRate += r.alpha * (instantRate - r.lastRate) - } else { - r.init = true - r.lastRate = instantRate - } -} - -// Inc counts one event. -func (r *EwmaRate) Inc() { - r.newEvents.Inc() -} - -func (r *EwmaRate) Add(delta int64) { - r.newEvents.Add(delta) -} diff --git a/math/rate_test.go b/math/rate_test.go deleted file mode 100644 index 378180caa..000000000 --- a/math/rate_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package math - -import ( - "testing" - "time" - - "github.com/stretchr/testify/require" -) - -func TestRate(t *testing.T) { - ticks := []struct { - events int - want float64 - }{ - {60, 1}, - {30, 0.9}, - {0, 0.72}, - {60, 0.776}, - {0, 0.6208}, - {0, 0.49664}, - {0, 0.397312}, - {0, 0.3178496}, - {0, 0.25427968}, - {0, 0.203423744}, - {0, 0.1627389952}, - } - r := NewEWMARate(0.2, time.Minute) - - for _, tick := range ticks { - for e := 0; e < tick.events; e++ { - r.Inc() - } - r.Tick() - // We cannot do double comparison, because double operations on different - // platforms may actually produce results that differ slightly. - // There are multiple issues about this in Go's github, eg: 18354 or 20319. - require.InDelta(t, tick.want, r.Rate(), 0.0000000001, "unexpected rate") - } - - r = NewEWMARate(0.2, time.Minute) - - for _, tick := range ticks { - r.Add(int64(tick.events)) - r.Tick() - require.InDelta(t, tick.want, r.Rate(), 0.0000000001, "unexpected rate") - } -} diff --git a/ring/ring.go b/ring/ring.go index 63e3a547c..b9a11b83a 100644 --- a/ring/ring.go +++ b/ring/ring.go @@ -23,7 +23,7 @@ import ( "github.com/grafana/dskit/services" "github.com/grafana/dskit/flagext" - dsmath "github.com/grafana/dskit/math" + dsmath "github.com/grafana/dskit/internal/math" ) const (