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
defer func() {
if err != nil {
for _, c := range closers {
c.Close()
}
var merr MultiError
merr.Add(err)
merr.Add(closeAll(closers))
err = merr.Err()
}
}()
meta, err := readMetaFile(dir)

View File

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

2
db.go
View File

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