Cache total stats in memory

This commit is contained in:
2021-11-03 21:24:23 +01:00
parent 492c88c5b2
commit bd6eafd9f7
8 changed files with 177 additions and 36 deletions

View File

@@ -1,9 +1,6 @@
package store
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.uio.no/torjus/apiary/models"
)
@@ -17,7 +14,7 @@ type MetricsCollectingStore struct {
totalAttemptsCount prometheus.Gauge
}
func NewMetricsCollectingStore(ctx context.Context, store LoginAttemptStore) *MetricsCollectingStore {
func NewMetricsCollectingStore(store LoginAttemptStore) *MetricsCollectingStore {
mcs := &MetricsCollectingStore{store: store}
mcs.attemptsCounter = prometheus.NewCounterVec(
@@ -62,17 +59,6 @@ func NewMetricsCollectingStore(ctx context.Context, store LoginAttemptStore) *Me
prometheus.MustRegister(mcs.uniqueIPsCount)
prometheus.MustRegister(mcs.totalAttemptsCount)
// Kinda jank, we just fetch the stats every 10seconds, but it should be cached most of the time.
go func(ctx context.Context) {
ticker := time.NewTicker(10 * time.Second)
select {
case <-ctx.Done():
return
case <-ticker.C:
mcs.Stats(LoginStatsTotals, 0)
}
}(ctx)
return mcs
}