Skip to content
Permalink
Browse files

simplify atomic write logic (#114)

  • Loading branch information...
gsquire authored and stapelberg committed Jul 18, 2019
1 parent e36cb8d commit 73b1bc52ed78c6dcb94c96faa5c5196158df75c7
Showing with 8 additions and 12 deletions.
  1. +8 −12 internal/write/atomically.go
@@ -35,28 +35,24 @@ func Atomically(dest string, compress bool, write func(w io.Writer) error) (err

bufw := bufio.NewWriter(f)

w := io.Writer(bufw)
var gzipw *gzip.Writer
if compress {
// NOTE(stapelberg): gzip’s decompression phase takes the same
// time, regardless of compression level. Hence, we invest the
// maximum CPU time once to achieve the best compression.
gzipw, err = gzip.NewWriterLevel(bufw, gzip.BestCompression)
gzipw, err := gzip.NewWriterLevel(bufw, gzip.BestCompression)
if err != nil {
return err
}
defer gzipw.Close()
w = gzipw
}

if err := write(w); err != nil {
return err
}

if compress {
if err := write(gzipw); err != nil {
return err
}
if err := gzipw.Close(); err != nil {
return err
}
} else {
if err := write(bufw); err != nil {
return err
}
}

if err := bufw.Flush(); err != nil {

0 comments on commit 73b1bc5

Please sign in to comment.
You can’t perform that action at this time.