1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Avoid cast each write for flusher

This commit is contained in:
Guillaume J. Charmes 2013-05-20 10:58:35 -07:00
parent 98b0fd173b
commit ae9d7a5167
2 changed files with 25 additions and 12 deletions

View file

@ -68,7 +68,7 @@ func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) {
} }
func (srv *Server) ImageInsert(name, url, path string, out io.Writer) 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) img, err := srv.runtime.repositories.LookupImage(name)
if err != nil { if err != nil {
return err 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 { 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) history, err := srv.registry.GetRemoteHistory(imgId, registry, token)
if err != nil { if err != nil {
return err 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 { 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()) fmt.Fprintf(out, "Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
repoData, err := srv.registry.GetRepositoryData(remote) repoData, err := srv.registry.GetRepositoryData(remote)
if err != nil { 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 { 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") fmt.Fprintf(out, "Processing checksums\n")
imgList, err := srv.getImageList(localRepo) imgList, err := srv.getImageList(localRepo)
if err != nil { 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 { 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")) jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json"))
if err != nil { if err != nil {
return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) 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 { 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) img, err := srv.runtime.graph.Get(name)
if err != nil { if err != nil {
fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name])) fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name]))

View file

@ -104,7 +104,7 @@ func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string
if template == "" { if template == "" {
template = "%v/%v (%v)" 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 // HumanDuration returns a human-readable approximation of a duration
@ -531,14 +531,27 @@ func GetKernelVersion() (*KernelVersionInfo, error) {
}, nil }, nil
} }
type NopFlusher struct{}
func (f *NopFlusher) Flush() {}
type WriteFlusher struct { type WriteFlusher struct {
W io.Writer w io.Writer
flusher http.Flusher
} }
func (wf *WriteFlusher) Write(b []byte) (n int, err error) { func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
n, err = wf.W.Write(b) n, err = wf.w.Write(b)
if f, ok := wf.W.(http.Flusher); ok { wf.flusher.Flush()
f.Flush()
}
return n, err 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}
}