Browse Source

Avoid cast each write for flusher

Guillaume J. Charmes 12 years ago
parent
commit
ae9d7a5167
2 changed files with 25 additions and 12 deletions
  1. 6 6
      server.go
  2. 19 6
      utils/utils.go

+ 6 - 6
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]))

+ 19 - 6
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}
+}