Skip to content

Commit

Permalink
prom: optimize connection memory alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed May 19, 2023
1 parent ecfa482 commit 51efbab
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 {

Check warning on line 39 in pkg/prom/connections.go

View check run for this annotation

Codecov / codecov/patch

pkg/prom/connections.go#L39

Added line #L39 was not covered by tests
return
}
c.Inc()
p.gauge[state].Inc()

Check warning on line 42 in pkg/prom/connections.go

View check run for this annotation

Codecov / codecov/patch

pkg/prom/connections.go#L42

Added line #L42 was not covered by tests
}

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

Check warning on line 46 in pkg/prom/connections.go

View check run for this annotation

Codecov / codecov/patch

pkg/prom/connections.go#L46

Added line #L46 was not covered by tests
return
}
c.Dec()
p.gauge[state].Dec()

Check warning on line 49 in pkg/prom/connections.go

View check run for this annotation

Codecov / codecov/patch

pkg/prom/connections.go#L49

Added line #L49 was not covered by tests
}

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

0 comments on commit 51efbab

Please sign in to comment.