Skip to content

Commit

Permalink
Add errgroup and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bootjp committed Jun 11, 2024
1 parent ea049eb commit 04f8180
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/alecthomas/kingpin/v2 v2.3.1
github.com/prometheus/client_golang v1.15.0
go.bug.st/serial v1.5.0
golang.org/x/sync v0.1.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1
go.bug.st/serial v1.5.0 h1:ThuUkHpOEmCVXxGEfpoExjQCS2WBVV4ZcUKVYInM9T4=
go.bug.st/serial v1.5.0/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
23 changes: 13 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.bug.st/serial"
"golang.org/x/sync/errgroup"
)

type Status struct {
Expand Down Expand Up @@ -58,10 +59,10 @@ func parser(data string) (Status, error) {
return result, nil
}

func recordMetrics() {
func recordMetrics() error {
port, err := serial.Open(*deviceName, &serial.Mode{})
if err != nil {
log.Fatalln(err)
return err
}
mode := &serial.Mode{
BaudRate: 115200,
Expand All @@ -70,11 +71,11 @@ func recordMetrics() {
StopBits: serial.OneStopBit,
}
if err := port.SetMode(mode); err != nil {
logger.Fatalln(err)
return err
}
_, err = port.Write([]byte("STA\r\n"))
if err != nil {
logger.Fatalln(err)
return err
}

defer func() {
Expand Down Expand Up @@ -102,6 +103,8 @@ func recordMetrics() {
temperature.Set(stat.temp)
humidity.Set(stat.hum)
}

return err
}

var (
Expand All @@ -120,7 +123,8 @@ var (
)

func run() error {
go recordMetrics()
e := errgroup.Group{}
e.Go(recordMetrics)

http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
Expand All @@ -131,12 +135,11 @@ func run() error {
return
}
})
err := http.ListenAndServe(*addr, nil)
if err != nil {
return err
}
e.Go(func() error {
return http.ListenAndServe(*addr, nil)
})

return nil
return e.Wait()
}

var (
Expand Down
7 changes: 5 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"errors"
"testing"
)

func TestParser(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -49,7 +52,7 @@ func TestParser(t *testing.T) {

for _, tt := range tests {
stat, err := parser(tt.in)
if err != tt.err {
if !errors.Is(err, tt.err) {
t.Errorf("expect error got %v, want %v", stat, tt.out)
}
if stat != tt.out {
Expand Down

0 comments on commit 04f8180

Please sign in to comment.