Skip to content

Commit

Permalink
Merge pull request #107 from weaveworks/errcheck
Browse files Browse the repository at this point in the history
Address non-Close errors identified by errcheck
  • Loading branch information
peterbourgon committed May 26, 2015
2 parents e518c71 + 2f16729 commit 92adbf1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
9 changes: 7 additions & 2 deletions app/api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func makeTopologyHandlers(

// Websocket for the full topology. This route overlaps with the next.
get.HandleFunc(base+"/ws", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if err := r.ParseForm(); err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
return
}
loop := websocketLoop
if t := r.Form.Get("t"); t != "" {
var err error
Expand Down Expand Up @@ -140,7 +143,9 @@ func handleWebsocket(
diff := report.TopoDiff(previousTopo, newTopo)
previousTopo = newTopo

conn.SetWriteDeadline(time.Now().Add(websocketTimeout))
if err := conn.SetWriteDeadline(time.Now().Add(websocketTimeout)); err != nil {
return
}
if err := conn.WriteJSON(diff); err != nil {
return
}
Expand Down
11 changes: 0 additions & 11 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"net/http"
Expand All @@ -28,7 +27,6 @@ func main() {
probes = flag.String("probes", strings.Join(defaultProbes, ","), "list of probe endpoints, comma separated")
batch = flag.Duration("batch", 1*time.Second, "batch interval")
window = flag.Duration("window", 15*time.Second, "window")
pidfile = flag.String("pidfile", "", "write PID file")
listen = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address")
)
flag.Parse()
Expand Down Expand Up @@ -57,15 +55,6 @@ func main() {
log.SetOutput(f)
}

if *pidfile != "" {
err := ioutil.WriteFile(*pidfile, []byte(fmt.Sprint(os.Getpid())), 0644)
if err != nil {
log.Print(err)
return
}
defer os.Remove(*pidfile)
}

log.Printf("app starting, version %s", version)

// Collector deals with the probes, and generates merged reports.
Expand Down
5 changes: 4 additions & 1 deletion app/server_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package main

import (
"encoding/json"
"log"
"net/http"
)

func respondWith(w http.ResponseWriter, code int, response interface{}) {
w.Header().Set("Content-Type", "application/json")
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(code)
json.NewEncoder(w).Encode(response)
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Print(err)
}
}
7 changes: 5 additions & 2 deletions experimental/example/goapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ func get(target string) {
log.Printf("%s: %v", target, err)
return
}
defer resp.Body.Close()
log.Printf("%s: %s", target, resp.Status)
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
log.Printf("%s: %v", target, err)
return
}
}

func interrupt() error {
Expand Down
5 changes: 4 additions & 1 deletion experimental/genreport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"encoding/json"
"flag"
"log"
"os"
)

func main() {
nodes := flag.Int("nodes", 10, "node count")
flag.Parse()
json.NewEncoder(os.Stdout).Encode(DemoReport(*nodes))
if err := json.NewEncoder(os.Stdout).Encode(DemoReport(*nodes)); err != nil {
log.Print(err)
}
}
2 changes: 1 addition & 1 deletion probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
log.Printf("exposing Prometheus endpoint at %s%s", *httpListen, *prometheusEndpoint)
http.Handle(*prometheusEndpoint, makePrometheusHandler())
}
go http.ListenAndServe(*httpListen, nil)
go func(err error) { log.Print(err) }(http.ListenAndServe(*httpListen, nil))
}

if *spyProcs && os.Getegid() != 0 {
Expand Down
12 changes: 9 additions & 3 deletions probe/process_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCgroupMapper(t *testing.T) {
"/netscape/notify_on_release": "0\n",
"/weirdfile": "",
})
defer os.RemoveAll(tmp)
defer removeAll(t, tmp)

m := newCgroupMapper(tmp, 1*time.Second)
for pid, want := range map[uint]string{
Expand All @@ -44,14 +44,20 @@ func setupTmpFS(t *testing.T, fs map[string]string) string {
for file, content := range fs {
dir := path.Dir(file)
if err := os.MkdirAll(filepath.Join(tmp, dir), 0777); err != nil {
os.RemoveAll(tmp)
removeAll(t, tmp)
t.Fatalf("MkdirAll: %v", err)
}

if err := ioutil.WriteFile(filepath.Join(tmp, file), []byte(content), 0655); err != nil {
os.RemoveAll(tmp)
removeAll(t, tmp)
t.Fatalf("WriteFile: %v", err)
}
}
return tmp
}

func removeAll(t *testing.T, path string) {
if err := os.RemoveAll(path); err != nil {
t.Error(err)
}
}

0 comments on commit 92adbf1

Please sign in to comment.