refactor multi errors

Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
Krasi Georgiev 2019-02-11 11:57:46 +02:00
parent 07df4fd383
commit bf2239079d
4 changed files with 14 additions and 8 deletions

View file

@ -277,9 +277,10 @@ func OpenBlock(logger log.Logger, dir string, pool chunkenc.Pool) (pb *Block, er
var closers []io.Closer var closers []io.Closer
defer func() { defer func() {
if err != nil { if err != nil {
for _, c := range closers { var merr MultiError
c.Close() merr.Add(err)
} merr.Add(closeAll(closers))
err = merr.Err()
} }
}() }()
meta, err := readMetaFile(dir) meta, err := readMetaFile(dir)

View file

@ -342,7 +342,7 @@ func NewDirReader(dir string, pool chunkenc.Pool) (*Reader, error) {
} }
func (s *Reader) Close() error { func (s *Reader) Close() error {
return closeAll(s.cs...) return closeAll(s.cs)
} }
// Size returns the size of the chunks. // Size returns the size of the chunks.
@ -410,7 +410,7 @@ func sequenceFiles(dir string) ([]string, error) {
return res, nil return res, nil
} }
func closeAll(cs ...io.Closer) (err error) { func closeAll(cs []io.Closer) (err error) {
for _, c := range cs { for _, c := range cs {
if e := c.Close(); e != nil { if e := c.Close(); e != nil {
err = e err = e

View file

@ -582,7 +582,7 @@ func (c *LeveledCompactor) write(dest string, meta *BlockMeta, blocks ...BlockRe
// populateBlock fills the index and chunk writers with new data gathered as the union // populateBlock fills the index and chunk writers with new data gathered as the union
// of the provided blocks. It returns meta information for the new block. // of the provided blocks. It returns meta information for the new block.
func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta, indexw IndexWriter, chunkw ChunkWriter) error { func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta, indexw IndexWriter, chunkw ChunkWriter) (err error) {
if len(blocks) == 0 { if len(blocks) == 0 {
return errors.New("cannot populate block from no readers") return errors.New("cannot populate block from no readers")
} }
@ -592,7 +592,12 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta,
allSymbols = make(map[string]struct{}, 1<<16) allSymbols = make(map[string]struct{}, 1<<16)
closers = []io.Closer{} closers = []io.Closer{}
) )
defer func() { closeAll(closers...) }() defer func() {
var merr MultiError
merr.Add(err)
merr.Add(closeAll(closers))
err = merr.Err()
}()
for i, b := range blocks { for i, b := range blocks {
indexr, err := b.Index() indexr, err := b.Index()

2
db.go
View file

@ -1097,7 +1097,7 @@ func (es MultiError) Err() error {
return es return es
} }
func closeAll(cs ...io.Closer) error { func closeAll(cs []io.Closer) error {
var merr MultiError var merr MultiError
for _, c := range cs { for _, c := range cs {