From d48606827c06abdf320bcee6a3df79b3708979b1 Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Fri, 8 Feb 2019 13:35:32 +0200 Subject: [PATCH] simplify closers Signed-off-by: Krasi Georgiev --- block.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/block.go b/block.go index 26853ea30..08b4861f8 100644 --- a/block.go +++ b/block.go @@ -16,6 +16,7 @@ package tsdb import ( "encoding/json" + "io" "io/ioutil" "os" "path/filepath" @@ -273,6 +274,14 @@ func OpenBlock(logger log.Logger, dir string, pool chunkenc.Pool) (pb *Block, er if logger == nil { logger = log.NewNopLogger() } + var closers []io.Closer + defer func() { + if err != nil { + for _, c := range closers { + c.Close() + } + } + }() meta, err := readMetaFile(dir) if err != nil { return nil, err @@ -282,31 +291,19 @@ func OpenBlock(logger log.Logger, dir string, pool chunkenc.Pool) (pb *Block, er if err != nil { return nil, err } - defer func() { - if err != nil { - cr.Close() - } - }() + closers = append(closers, cr) + ir, err := index.NewFileReader(filepath.Join(dir, indexFilename)) if err != nil { return nil, err } - - defer func() { - if err != nil { - ir.Close() - } - }() + closers = append(closers, ir) tr, tsr, err := readTombstones(dir) if err != nil { return nil, err } - defer func() { - if err != nil { - tr.Close() - } - }() + closers = append(closers, tr) // TODO refactor to set this at block creation time as // that would be the logical place for a block size to be calculated.