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 ( import (
"encoding/json" "encoding/json"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -273,6 +274,14 @@ func OpenBlock(logger log.Logger, dir string, pool chunkenc.Pool) (pb *Block, er
if logger == nil { if logger == nil {
logger = log.NewNopLogger() logger = log.NewNopLogger()
} }
var closers []io.Closer
defer func() {
if err != nil {
for _, c := range closers {
c.Close()
}
}
}()
meta, err := readMetaFile(dir) meta, err := readMetaFile(dir)
if err != nil { if err != nil {
return nil, err return nil, err
@ -282,31 +291,19 @@ func OpenBlock(logger log.Logger, dir string, pool chunkenc.Pool) (pb *Block, er
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() { closers = append(closers, cr)
if err != nil {
cr.Close()
}
}()
ir, err := index.NewFileReader(filepath.Join(dir, indexFilename)) ir, err := index.NewFileReader(filepath.Join(dir, indexFilename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
closers = append(closers, ir)
defer func() {
if err != nil {
ir.Close()
}
}()
tr, tsr, err := readTombstones(dir) tr, tsr, err := readTombstones(dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() { closers = append(closers, tr)
if err != nil {
tr.Close()
}
}()
// TODO refactor to set this at block creation time as // TODO refactor to set this at block creation time as
// that would be the logical place for a block size to be calculated. // that would be the logical place for a block size to be calculated.