handle prom in thread
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 32s

This commit is contained in:
matst80
2024-11-09 12:12:50 +01:00
parent db7dc28fc7
commit ab6908c0e2

View File

@@ -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
}