Skip to content

Commit 811e3d1

Browse files
committed
fix: gosec http listener linter issues
1 parent 00238ae commit 811e3d1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

cmd/rpi_exporter/main.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"net/http"
66
"os"
7+
"time"
78

89
"github.com/schubergphilis/rpi_exporter/pkg/export/prometheus"
910
"github.com/schubergphilis/rpi_exporter/pkg/mbox"
@@ -15,6 +16,12 @@ var (
1516
flagDebug = flag.Bool("debug", false, "Print debug messages")
1617
)
1718

19+
const (
20+
httpReadTimeout = 5 * time.Second
21+
httpWriteTimeout = 10 * time.Second
22+
httpIdleTimeout = 120 * time.Second
23+
)
24+
1825
func main() {
1926
flag.Parse()
2027

@@ -27,9 +34,18 @@ func main() {
2734
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
2835
}
2936
}))
37+
3038
log.Printf("Listening on %s", *flagAddr)
31-
if err := http.ListenAndServe(*flagAddr, nil); err != nil {
32-
log.WithError(err).Fatal("unable to listen and server http")
39+
40+
srv := &http.Server{
41+
Addr: *flagAddr,
42+
Handler: nil,
43+
ReadTimeout: httpReadTimeout,
44+
WriteTimeout: httpWriteTimeout,
45+
IdleTimeout: httpIdleTimeout,
46+
}
47+
if err := srv.ListenAndServe(); err != nil {
48+
log.WithError(err).Fatal("unable to listen and serve http")
3349
}
3450

3551
return

pkg/export/prometheus/writer.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const (
2020
metricTypeGauge = "gauge"
2121
)
2222

23+
const (
24+
fahrenheitFactorNumerator = 9
25+
fahrenheitFactorDenominator = 5
26+
fahrenheitOffset = 32
27+
)
28+
2329
var voltageLabelsByID = map[mbox.VoltageID]string{
2430
mbox.VoltageIDCore: "core",
2531
mbox.VoltageIDSDRAMC: "sdram_c",
@@ -234,7 +240,7 @@ func (w *expWriter) writeTemperatures(mboxOpen *mbox.Mailbox) error {
234240
w.writeSample(formatTemp(temp), "soc")
235241

236242
w.writeHeaderGauge("rpi_temperature_f", "Temperature of the SoC in degrees fahrenheit.", metricTypeGauge, "id")
237-
w.writeSample(formatTemp(temp*9/5+32), "soc")
243+
w.writeSample(formatTemp(temp*fahrenheitFactorNumerator/fahrenheitFactorDenominator+fahrenheitOffset), "soc")
238244

239245
w.writeHeaderGauge(
240246
"rpi_max_temperature_c",
@@ -255,7 +261,7 @@ func (w *expWriter) writeTemperatures(mboxOpen *mbox.Mailbox) error {
255261
"Maximum temperature of the SoC in degrees fahrenheit.",
256262
metricTypeGauge,
257263
"id")
258-
w.writeSample(formatTemp(maxTemp*9/5+32), "soc")
264+
w.writeSample(formatTemp(temp*fahrenheitFactorNumerator/fahrenheitFactorDenominator+fahrenheitOffset), "soc")
259265

260266
return nil
261267
}

pkg/mbox/mailbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ func (m *Mailbox) writeRequestHeader(bufferBytes int, tagID uint32, args []uint3
370370
m.buf[1] = RequestCodeDefault
371371
m.buf[2] = tagID
372372
m.buf[3] = uint32(bufferBytes)
373-
m.buf[4] = 0 // request
374-
copy(m.buf[MailboxRequestHeaderWords:], args) // TODO: zero remaining buffer and end tag
373+
m.buf[4] = 0 // request
374+
copy(m.buf[MailboxRequestHeaderWords:], args)
375375
}
376376

377377
// debugBuffer prints out buffer values for debugging.

0 commit comments

Comments
 (0)