Skip to content

Commit

Permalink
prom: optimize connection memory alloc (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed May 19, 2023
1 parent ecfa482 commit 528300c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/prom/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type connections struct {
once sync.Once
vec *prometheus.GaugeVec
gauge map[http.ConnState]prometheus.Gauge
storage sync.Map
}

Expand All @@ -25,24 +26,27 @@ func (p *connections) init() {
Namespace: Namespace,
Name: "connections",
}, []string{"state"})
p.gauge = map[http.ConnState]prometheus.Gauge{
http.StateNew: p.vec.WithLabelValues(http.StateNew.String()),
http.StateActive: p.vec.WithLabelValues(http.StateActive.String()),
http.StateIdle: p.vec.WithLabelValues(http.StateIdle.String()),
}
reg.MustRegister(p.vec)
})
}

func (p *connections) inc(state http.ConnState) {
c, err := p.vec.GetMetricWith(prometheus.Labels{"state": state.String()})
if err != nil {
if p.gauge == nil {
return
}
c.Inc()
p.gauge[state].Inc()
}

func (p *connections) dec(state http.ConnState) {
c, err := p.vec.GetMetricWith(prometheus.Labels{"state": state.String()})
if err != nil {
if p.gauge == nil {
return
}
c.Dec()
p.gauge[state].Dec()
}

func (p *connections) connState(conn net.Conn, state http.ConnState) {
Expand Down

0 comments on commit 528300c

Please sign in to comment.