config: better memory management for libucl
This commit is contained in:
parent
50830e429a
commit
f22cc62b2c
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue