Fixed the enduring nasty "too many files open" bug!! Fix #446

This commit is contained in:
Deluan 2021-07-01 12:07:32 -04:00
parent 0c2ca2a5e4
commit 452c8dc44b
2 changed files with 7 additions and 0 deletions

View File

@ -121,6 +121,7 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
return &CachedStream{
Reader: sr,
Seeker: sr,
Closer: r,
Cached: true,
}, nil
} else {
@ -135,11 +136,15 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
type CachedStream struct {
io.Reader
io.Seeker
io.Closer
Cached bool
}
func (s *CachedStream) Seekable() bool { return s.Seeker != nil }
func (s *CachedStream) Close() error {
if s.Closer != nil {
return s.Closer.Close()
}
if c, ok := s.Reader.(io.Closer); ok {
return c.Close()
}

View File

@ -60,6 +60,7 @@ var _ = Describe("File Caches", func() {
s, err := fc.Get(context.TODO(), &testArg{"test"})
Expect(err).To(BeNil())
Expect(s.Cached).To(BeFalse())
Expect(s.Closer).To(BeNil())
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
// Second call is a HIT
@ -68,6 +69,7 @@ var _ = Describe("File Caches", func() {
Expect(err).To(BeNil())
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
Expect(s.Cached).To(BeTrue())
Expect(s.Closer).ToNot(BeNil())
Expect(called).To(BeFalse())
})