From ab6908c0e22229a4ed5de9a3d0627c9d2fd6fbf4 Mon Sep 17 00:00:00 2001 From: matst80 Date: Sat, 9 Nov 2024 12:12:50 +0100 Subject: [PATCH] handle prom in thread --- grain-pool.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/grain-pool.go b/grain-pool.go index dfbdeba..1a8827c 100644 --- a/grain-pool.go +++ b/grain-pool.go @@ -19,6 +19,10 @@ var ( Name: "cart_pool_size", Help: "The total number of mutations", }) + poolUsage = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "cart_grain_pool_usage", + Help: "The current usage of the grain pool", + }) ) type GrainPool interface { @@ -32,7 +36,6 @@ type Ttl struct { } type GrainLocalPool struct { - gauage prometheus.Gauge grains map[CartId]*CartGrain expiry []Ttl spawn func(id CartId) (*CartGrain, error) @@ -41,12 +44,8 @@ type GrainLocalPool struct { } func NewGrainLocalPool(size int, ttl time.Duration, spawn func(id CartId) (*CartGrain, error)) *GrainLocalPool { - gauage := promauto.NewGauge(prometheus.GaugeOpts{ - Name: "cart_grain_pool_usage", - Help: "The current usage of the grain pool", - }) + ret := &GrainLocalPool{ - gauage: gauage, spawn: spawn, grains: make(map[CartId]*CartGrain), expiry: make([]Ttl, 0), @@ -116,11 +115,13 @@ func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) { p.grains[id] = grain } - l := float64(len(p.grains)) - ps := float64(p.PoolSize) - p.gauage.Set(l / ps) - poolGrains.Set(l) - poolSize.Set(ps) + go func() { + l := float64(len(p.grains)) + ps := float64(p.PoolSize) + poolUsage.Set(l / ps) + poolGrains.Set(l) + poolSize.Set(ps) + }() return grain, err }