From ae9d7a5167da58de9a1a4beac489cf1e6adcea11 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 20 May 2013 10:58:35 -0700 Subject: [PATCH] Avoid cast each write for flusher --- server.go | 12 ++++++------ utils/utils.go | 25 +++++++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/server.go b/server.go index b07e85b44c..956a4f8d36 100644 --- a/server.go +++ b/server.go @@ -68,7 +68,7 @@ func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) { } func (srv *Server) ImageInsert(name, url, path string, out io.Writer) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) img, err := srv.runtime.repositories.LookupImage(name) if err != nil { return err @@ -290,7 +290,7 @@ func (srv *Server) ContainerTag(name, repo, tag string, force bool) error { } func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []string) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) history, err := srv.registry.GetRemoteHistory(imgId, registry, token) if err != nil { return err @@ -326,7 +326,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } func (srv *Server) pullRepository(out io.Writer, remote, askedTag string) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) fmt.Fprintf(out, "Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress()) repoData, err := srv.registry.GetRepositoryData(remote) if err != nil { @@ -465,7 +465,7 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgDat } func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) fmt.Fprintf(out, "Processing checksums\n") imgList, err := srv.getImageList(localRepo) if err != nil { @@ -505,7 +505,7 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri } func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []string) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json")) if err != nil { return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) @@ -565,7 +565,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st } func (srv *Server) ImagePush(name, registry string, out io.Writer) error { - out = &utils.WriteFlusher{W: out} + out = utils.NewWriteFlusher(out) img, err := srv.runtime.graph.Get(name) if err != nil { fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name])) diff --git a/utils/utils.go b/utils/utils.go index a2fd3bde6d..150eae8570 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -104,7 +104,7 @@ func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string if template == "" { template = "%v/%v (%v)" } - return &progressReader{r, &WriteFlusher{W: output}, size, 0, 0, template} + return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template} } // HumanDuration returns a human-readable approximation of a duration @@ -531,14 +531,27 @@ func GetKernelVersion() (*KernelVersionInfo, error) { }, nil } +type NopFlusher struct{} + +func (f *NopFlusher) Flush() {} + type WriteFlusher struct { - W io.Writer + w io.Writer + flusher http.Flusher } func (wf *WriteFlusher) Write(b []byte) (n int, err error) { - n, err = wf.W.Write(b) - if f, ok := wf.W.(http.Flusher); ok { - f.Flush() - } + n, err = wf.w.Write(b) + wf.flusher.Flush() return n, err } + +func NewWriteFlusher(w io.Writer) *WriteFlusher { + var flusher http.Flusher + if f, ok := w.(http.Flusher); ok { + flusher = f + } else { + flusher = &NopFlusher{} + } + return &WriteFlusher{w: w, flusher: flusher} +}