config: better memory management for libucl

This commit is contained in:
Mitchell Hashimoto 2014-05-23 16:30:28 -07:00
parent 50830e429a
commit f22cc62b2c
4 changed files with 39 additions and 0 deletions

View File

@ -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) {

View File

@ -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
}

View File

@ -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

View File

@ -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)
}