simplify closers

Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
Krasi Georgiev 2019-02-08 13:35:32 +02:00
parent e138c7ed7e
commit d48606827c

View file

@ -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.