Skip to content

Commit

Permalink
Switch to strings.Builder and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
klausman committed Apr 8, 2024
1 parent d007527 commit 0e1be65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
43 changes: 21 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,28 @@ func main() {
}
prometheus.MustRegister(jt, reg)
panic(srv.ListenAndServe())
} else {
log.Printf("[%d] Forking off webserver process", pid)
wscmd := forkWebserver()
defer killNoCheck(wscmd)
log.Printf("[%d] Starting NFT exporter", pid)
go func(wscmd *exec.Cmd) {
pid := os.Getpid()
err := wscmd.Wait()
var eerr *exec.ExitError
if errors.As(err, &eerr) {
log.Printf("[%d] Webserver process with pid %d exited with code %d", pid, eerr.Pid(), eerr.ExitCode())
log.Printf("[%d] Webserver stderr: %v", pid, string(eerr.Stderr))
} else {
log.Printf("[%d] Webserver process failed to launch: %s", pid, err)
}
log.Printf("[%d] Nftables/Netlink process exiting", pid)
// an os.Exit implicitly does NOT run the defer kill... above,
// so we avoid a loop-de-loop here
os.Exit(-1)
}(wscmd)
exportNftForever()
// if we ever get here, we need to get rid of the child as well
}
log.Printf("[%d] Forking off webserver process", pid)
wscmd := forkWebserver()
defer killNoCheck(wscmd)
log.Printf("[%d] Starting NFT exporter", pid)
go func(wscmd *exec.Cmd) {
pid := os.Getpid()
err := wscmd.Wait()
var eerr *exec.ExitError
if errors.As(err, &eerr) {
log.Printf("[%d] Webserver process with pid %d exited with code %d", pid, eerr.Pid(), eerr.ExitCode())
log.Printf("[%d] Webserver stderr: %v", pid, string(eerr.Stderr))
} else {
log.Printf("[%d] Webserver process failed to launch: %s", pid, err)
}
log.Printf("[%d] Nftables/Netlink process exiting", pid)
// an os.Exit implicitly does NOT run the defer kill... above,
// so we avoid a loop-de-loop here
os.Exit(-1)
}(wscmd)
exportNftForever()
// if we ever get here, we need to get rid of the child as well
}

func dropPrivileges(username, group string) error {
Expand Down
8 changes: 4 additions & 4 deletions stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type Stat struct {
}

func (s Stat) String() string {
acc := make([]string, 0, len(s.Fields))
var acc strings.Builder
for k, v := range s.Fields {
acc = append(acc, fmt.Sprintf("%s=%s;", k, v))
acc.WriteString(fmt.Sprintf("%s=%s;", k, v))
}
acc = append(acc, fmt.Sprintf("By=%d;Pk=%d", s.Bytes, s.Packets))
return strings.Join(acc, "")
acc.WriteString(fmt.Sprintf("By=%d;Pk=%d", s.Bytes, s.Packets))
return acc.String()
}

// NewStat creates a new Stat object with the Fields map initialized
Expand Down

0 comments on commit 0e1be65

Please sign in to comment.