diff options
author | Sameer Ajmani <sameer@golang.org> | 2015-09-01 22:25:18 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2015-09-01 22:25:18 +0000 |
commit | c17fdf129a9579156775bf2389f7273c26c5fa29 (patch) | |
tree | ea766b1c81e28cc23e22e8ef63d65abe525fd115 | |
parent | 4b6191103c1f4a4932e902cb0ca66b14abe87e97 (diff) | |
parent | 87156cb7667343326165d84f3557bd91ab57428b (diff) | |
download | tools-c17fdf129a9579156775bf2389f7273c26c5fa29.tar.gz |
tools/godoc: remove server\'s dependence on "expvar".
automerge: 87156cb
* commit '87156cb7667343326165d84f3557bd91ab57428b':
tools/godoc: remove server's dependence on "expvar".
-rw-r--r-- | godoc/server.go | 20 | ||||
-rw-r--r-- | godoc/server_test.go | 79 |
2 files changed, 5 insertions, 94 deletions
diff --git a/godoc/server.go b/godoc/server.go index 7963cd0..2c18efb 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -7,7 +7,6 @@ package godoc import ( "bytes" "encoding/json" - "expvar" "fmt" "go/ast" "go/build" @@ -460,27 +459,18 @@ func (w *writerCapturesErr) Write(p []byte) (int, error) { return n, err } -var httpErrors *expvar.Map - -func init() { - httpErrors = expvar.NewMap("httpWriteErrors").Init() -} - // applyTemplateToResponseWriter uses an http.ResponseWriter as the io.Writer // for the call to template.Execute. It uses an io.Writer wrapper to capture -// errors from the underlying http.ResponseWriter. If an error is found, an -// expvar will be incremented. Other template errors will be logged. This is -// done to keep from polluting log files with error messages due to networking -// issues, such as client disconnects and http HEAD protocol violations. +// errors from the underlying http.ResponseWriter. Errors are logged only when +// they come from the template processing and not the Writer; this avoid +// polluting log files with error messages due to networking issues, such as +// client disconnects and http HEAD protocol violations. func applyTemplateToResponseWriter(rw http.ResponseWriter, t *template.Template, data interface{}) { w := &writerCapturesErr{w: rw} err := t.Execute(w, data) // There are some cases where template.Execute does not return an error when // rw returns an error, and some where it does. So check w.err first. - if w.err != nil { - // For http errors, increment an expvar. - httpErrors.Add(w.err.Error(), 1) - } else if err != nil { + if w.err == nil && err != nil { // Log template errors. log.Printf("%s.Execute: %s", t.Name(), err) } diff --git a/godoc/server_test.go b/godoc/server_test.go deleted file mode 100644 index 95c2762..0000000 --- a/godoc/server_test.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package godoc - -import ( - "errors" - "expvar" - "net/http" - "net/http/httptest" - "testing" - "text/template" -) - -var ( - // NOTE: with no plain-text in the template, template.Execute will not - // return an error when http.ResponseWriter.Write does return an error. - tmpl = template.Must(template.New("test").Parse("{{.Foo}}")) -) - -type withFoo struct { - Foo int -} - -type withoutFoo struct { -} - -type errResponseWriter struct { -} - -func (*errResponseWriter) Header() http.Header { - return http.Header{} -} - -func (*errResponseWriter) WriteHeader(int) { -} - -func (*errResponseWriter) Write(p []byte) (int, error) { - return 0, errors.New("error") -} - -func TestApplyTemplateToResponseWriter(t *testing.T) { - for _, tc := range []struct { - desc string - rw http.ResponseWriter - data interface{} - expVars int - }{ - { - desc: "no error", - rw: &httptest.ResponseRecorder{}, - data: &withFoo{}, - expVars: 0, - }, - { - desc: "template error", - rw: &httptest.ResponseRecorder{}, - data: &withoutFoo{}, - expVars: 0, - }, - { - desc: "ResponseWriter error", - rw: &errResponseWriter{}, - data: &withFoo{}, - expVars: 1, - }, - } { - httpErrors.Init() - applyTemplateToResponseWriter(tc.rw, tmpl, tc.data) - gotVars := 0 - httpErrors.Do(func(expvar.KeyValue) { - gotVars++ - }) - if gotVars != tc.expVars { - t.Errorf("applyTemplateToResponseWriter(%q): got %d vars, want %d", tc.desc, gotVars, tc.expVars) - } - } -} |