-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only open output files when necessary
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
- Loading branch information
Dave Henderson
committed
Aug 29, 2020
1 parent
611951c
commit d8175dd
Showing
12 changed files
with
363 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package iohelpers | ||
|
|
||
| import ( | ||
| "io" | ||
| "sync" | ||
| ) | ||
|
|
||
| // LazyReadCloser provides an interface to a ReadCloser that will open on the | ||
| // first access. The wrapped io.ReadCloser must be provided by 'open'. | ||
| func LazyReadCloser(open func() (io.ReadCloser, error)) io.ReadCloser { | ||
| return &lazyReadCloser{ | ||
| opened: sync.Once{}, | ||
| open: open, | ||
| } | ||
| } | ||
|
|
||
| type lazyReadCloser struct { | ||
| opened sync.Once | ||
| r io.ReadCloser | ||
| // caches the error that came from open(), if any | ||
| openErr error | ||
| open func() (io.ReadCloser, error) | ||
| } | ||
|
|
||
| var _ io.ReadCloser = (*lazyReadCloser)(nil) | ||
|
|
||
| func (l *lazyReadCloser) openReader() (r io.ReadCloser, err error) { | ||
| l.opened.Do(func() { | ||
| l.r, l.openErr = l.open() | ||
| }) | ||
| return l.r, l.openErr | ||
| } | ||
|
|
||
| func (l *lazyReadCloser) Close() error { | ||
| r, err := l.openReader() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return r.Close() | ||
| } | ||
|
|
||
| func (l *lazyReadCloser) Read(p []byte) (n int, err error) { | ||
| r, err := l.openReader() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return r.Read(p) | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package iohelpers | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestLazyReadCloser(t *testing.T) { | ||
| r := newBufferCloser(bytes.NewBufferString("hello world")) | ||
| opened := false | ||
| l, ok := LazyReadCloser(func() (io.ReadCloser, error) { | ||
| opened = true | ||
| return r, nil | ||
| }).(*lazyReadCloser) | ||
| assert.True(t, ok) | ||
|
|
||
| assert.False(t, opened) | ||
| assert.Nil(t, l.r) | ||
| assert.False(t, r.closed) | ||
|
|
||
| p := make([]byte, 5) | ||
| n, err := l.Read(p) | ||
| assert.NoError(t, err) | ||
| assert.True(t, opened) | ||
| assert.Equal(t, r, l.r) | ||
| assert.Equal(t, 5, n) | ||
|
|
||
| err = l.Close() | ||
| assert.NoError(t, err) | ||
| assert.True(t, r.closed) | ||
|
|
||
| // test error propagation | ||
| l = LazyReadCloser(func() (io.ReadCloser, error) { | ||
| return nil, os.ErrNotExist | ||
| }).(*lazyReadCloser) | ||
|
|
||
| assert.Nil(t, l.r) | ||
|
|
||
| p = make([]byte, 5) | ||
| _, err = l.Read(p) | ||
| assert.Error(t, err) | ||
|
|
||
| err = l.Close() | ||
| assert.Error(t, err) | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //+build integration | ||
| //+build !windows | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "math" | ||
| "os" | ||
|
|
||
| . "gopkg.in/check.v1" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| "gotest.tools/v3/assert" | ||
| "gotest.tools/v3/fs" | ||
| "gotest.tools/v3/icmd" | ||
| ) | ||
|
|
||
| func setFileUlimit(b uint64) error { | ||
| ulimit := unix.Rlimit{ | ||
| Cur: b, | ||
| Max: math.MaxInt64, | ||
| } | ||
| err := unix.Setrlimit(unix.RLIMIT_NOFILE, &ulimit) | ||
| return err | ||
| } | ||
|
|
||
| func (s *InputDirSuite) TestInputDirRespectsUlimit(c *C) { | ||
| numfiles := 32 | ||
| flist := map[string]string{} | ||
| for i := 0; i < numfiles; i++ { | ||
| k := fmt.Sprintf("file_%d", i) | ||
| flist[k] = fmt.Sprintf("hello world %d\n", i) | ||
| } | ||
| testdir := fs.NewDir(c, "ulimittestfiles", | ||
| fs.WithDir("in", fs.WithFiles(flist)), | ||
| ) | ||
| defer testdir.Remove() | ||
|
|
||
| // we need another ~11 fds for other various things, so we'd be guaranteed | ||
| // to hit the limit if we try to have all the input files open | ||
| // simultaneously | ||
| setFileUlimit(uint64(numfiles)) | ||
| defer setFileUlimit(8192) | ||
|
|
||
| result := icmd.RunCmd(icmd.Command(GomplateBin, | ||
| "--input-dir", testdir.Join("in"), | ||
| "--output-dir", testdir.Join("out"), | ||
| ), func(c *icmd.Cmd) { | ||
| c.Dir = testdir.Path() | ||
| }) | ||
| setFileUlimit(8192) | ||
| result.Assert(c, icmd.Success) | ||
|
|
||
| files, err := ioutil.ReadDir(testdir.Join("out")) | ||
| assert.NilError(c, err) | ||
| assert.Equal(c, numfiles, len(files)) | ||
|
|
||
| for i := 0; i < numfiles; i++ { | ||
| f := testdir.Join("out", fmt.Sprintf("file_%d", i)) | ||
| _, err := os.Stat(f) | ||
| assert.NilError(c, err) | ||
|
|
||
| content, err := ioutil.ReadFile(f) | ||
| assert.NilError(c, err) | ||
| expected := fmt.Sprintf("hello world %d\n", i) | ||
| assert.Equal(c, expected, string(content)) | ||
| } | ||
| } |
Oops, something went wrong.