diff --git a/config/import_tree.go b/config/import_tree.go index 56800080c..f26904da1 100644 --- a/config/import_tree.go +++ b/config/import_tree.go @@ -1,6 +1,7 @@ package config import ( + "io" "fmt" "path/filepath" ) @@ -63,6 +64,21 @@ func loadTree(root string) (*importTree, error) { }, nil } +// Close releases any resources we might be holding open for the importTree. +// +// This can safely be called even while ConfigTree results are alive. The +// importTree is not bound to these. +func (t *importTree) Close() error { + if c, ok := t.Raw.(io.Closer); ok { + c.Close() + } + for _, ct := range t.Children { + ct.Close() + } + + return nil +} + // ConfigTree traverses the importTree and turns each node into a *Config // object, ultimately returning a *configTree. func (t *importTree) ConfigTree() (*configTree, error) { diff --git a/config/loader.go b/config/loader.go index ae6c2972d..a7d992804 100644 --- a/config/loader.go +++ b/config/loader.go @@ -11,6 +11,11 @@ func Load(path string) (*Config, error) { } configTree, err := importTree.ConfigTree() + + // Close the importTree now so that we can clear resources as quickly + // as possible. + importTree.Close() + if err != nil { return nil, err } diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 66aac9fe9..41f118248 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -17,6 +17,10 @@ type libuclConfigurable struct { Object *libucl.Object } +func (t *libuclConfigurable) Close() error { + return t.Object.Close() +} + func (t *libuclConfigurable) Config() (*Config, error) { var rawConfig struct { Variable map[string]Variable diff --git a/config/loader_libucl_test.go b/config/loader_libucl_test.go new file mode 100644 index 000000000..9f6624019 --- /dev/null +++ b/config/loader_libucl_test.go @@ -0,0 +1,14 @@ +package config + +import ( + "io" + "testing" +) + +func TestLibuclConfigurableCloser(t *testing.T) { + var _ io.Closer = new(libuclConfigurable) +} + +func TestLibuclConfigurableConfigurable(t *testing.T) { + var _ configurable = new(libuclConfigurable) +}