Skip to content

Commit beae22d

Browse files
tonmnndamoon
andauthored
fix: added timeouts to avoid memory leaking (#24)
* limit fcgi client maximum request execution time * configure prometheus handler to limit maximum requests in flight and maximum request execution time (cherry picked from commit 57e431b) * add error handling on SetTimeout-Call --------- Co-authored-by: David Sauer <davedamoon@gmail.com>
1 parent 420b644 commit beae22d

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

cmd/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ to quickly create a Cobra application.`,
7171
IdleTimeout: time.Second * 60,
7272
}
7373

74-
http.Handle(metricsEndpoint, promhttp.Handler())
74+
opts := promhttp.HandlerOpts{
75+
Timeout: 5 * time.Second,
76+
MaxRequestsInFlight: 2,
77+
}
78+
handler := promhttp.InstrumentMetricHandler(
79+
prometheus.DefaultRegisterer,
80+
promhttp.HandlerFor(prometheus.DefaultGatherer, opts),
81+
)
82+
http.Handle(metricsEndpoint, handler)
7583
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
7684
_, err := w.Write([]byte(`<html>
7785
<head><title>php-fpm_exporter</title></head>

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ require (
4949
gopkg.in/ini.v1 v1.67.0 // indirect
5050
gopkg.in/yaml.v3 v3.0.1 // indirect
5151
)
52+
53+
replace github.com/tomasen/fcgi_client => github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
2121
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
2222
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
2323
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
24+
github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7 h1:W0fAsQ7bC1db4k9O2X6yZvatz/0c/ISyxhmNnc6arZA=
25+
github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7/go.mod h1:dHpIS7C6YjFguh5vo9QBVEojDoL3vh3v6oEho2HtNyA=
2426
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
2527
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
2628
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -92,8 +94,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
9294
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
9395
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
9496
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
95-
github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19 h1:ZCmSnT6CLGhfoQ2lPEhL4nsJstKDCw1F1RfN8/smTCU=
96-
github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19/go.mod h1:SXTY+QvI+KTTKXQdg0zZ7nx0u94QWh8ZAwBQYsW9cqk=
9797
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
9898
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
9999
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=

phpfpm/phpfpm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func (p *Pool) Update() (err error) {
163163

164164
defer fcgi.Close()
165165

166+
err = fcgi.SetTimeout(time.Duration(3) * time.Second)
167+
if err != nil {
168+
return p.error(err)
169+
}
170+
166171
env := map[string]string{
167172
"SCRIPT_FILENAME": path,
168173
"SCRIPT_NAME": path,

0 commit comments

Comments
 (0)