From 152fa23a4ceddb9876da4836eed15130fc1aba38 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Mon, 2 Mar 2020 17:10:25 +0100 Subject: [PATCH] Close files earlier when generating fields files (#16681) (#16685) Fields generator keeps all open files while generating the global fields.yml file. This is not needed and may lead to "too many open files" errors in some environments. (cherry picked from commit 9524e4af41a1bea00291943ea095af488982d89d) --- libbeat/generator/fields/fields.go | 32 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/libbeat/generator/fields/fields.go b/libbeat/generator/fields/fields.go index c6210b28d2e..d61e9f22537 100644 --- a/libbeat/generator/fields/fields.go +++ b/libbeat/generator/fields/fields.go @@ -123,24 +123,32 @@ func writeGeneratedFieldsYml(fieldFiles []*YmlFile, output io.Writer) error { func GenerateFieldsYml(fieldFiles []*YmlFile) ([]byte, error) { buf := bytes.NewBufferString("") for _, p := range fieldFiles { - file, err := os.Open(p.Path) + err := generateFieldsYmlForFile(buf, p) if err != nil { return nil, err } - defer file.Close() + } + return buf.Bytes(), nil +} - fs := bufio.NewScanner(file) - for fs.Scan() { - err = writeIndentedLine(buf, fs.Text()+"\n", p.Indent) - if err != nil { - return nil, err - } - } - if err := fs.Err(); err != nil { - return nil, err +func generateFieldsYmlForFile(buf *bytes.Buffer, p *YmlFile) error { + file, err := os.Open(p.Path) + if err != nil { + return err + } + defer file.Close() + + fs := bufio.NewScanner(file) + for fs.Scan() { + err = writeIndentedLine(buf, fs.Text()+"\n", p.Indent) + if err != nil { + return err } } - return buf.Bytes(), nil + if err := fs.Err(); err != nil { + return err + } + return nil } func writeIndentedLine(buf *bytes.Buffer, line string, indent int) error {