-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleopen.go
297 lines (270 loc) · 5.88 KB
/
singleopen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Package singleopen provides functionality for reusing
// file handles when possible.
package singleopen
import (
"errors"
"io"
"io/fs"
"sync"
"golang.org/x/sync/singleflight"
"github.com/dwlnetnl/singleopen/internal/lru"
)
// FS is a file system that reuses file handles.
type FS struct {
// FS is the underlying file system used to open files.
// The underlying file system should return files that
// implement io.ReaderAt. If the file is just a fs.File
// calls to Read will be synchronised.
FS fs.FS
opener singleflight.Group
mu sync.Mutex // protects all below
files map[string]*file
cache *lru.Cache
closer chan *file
}
var _ fs.FS = (*FS)(nil)
// Open opens a file or returns the already open file.
// Only regular files are being reused. Before opening
// Stat is being called to determine the kind of file.
func (fsys *FS) Open(name string) (fs.File, error) {
fsys.mu.Lock()
f, ok := fsys.files[name]
if ok {
f.refc++
fsys.mu.Unlock()
if ra, ok := f.File.(io.ReaderAt); ok {
return &fileReaderAt{f, ra, 0}, nil
}
return f, nil
}
// get file from close cache
if fsys.cache != nil {
cv, ok := fsys.cache.Get(name)
if ok {
f := cv.(*file)
f.refc++ // increment before cache removal
fsys.cache.Remove(name)
fsys.files[name] = f
fsys.mu.Unlock()
if ra, ok := f.File.(io.ReaderAt); ok {
return &fileReaderAt{f, ra, 0}, nil
}
return f, nil
}
}
// unlock while opening file
fsys.mu.Unlock()
// call stat to detect if a directory is being opened
// use fs support for stat
if sfs, ok := fsys.FS.(fs.StatFS); ok {
fi, err := sfs.Stat(name)
if err != nil {
if errors.Is(err, (*fs.PathError)(nil)) {
return nil, err
}
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
if fi.IsDir() {
return fsys.FS.Open(name)
}
f, err := fsys.open(name)
if err != nil {
return nil, err
}
if ra, ok := f.File.(io.ReaderAt); ok {
return &fileReaderAt{f, ra, 0}, nil
}
return f, nil
}
// do stat on opened file
f, err := fsys.open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
if fi.IsDir() {
// remove from reusable files and close cache
fsys.mu.Lock()
delete(fsys.files, name)
if fsys.cache != nil {
fsys.cache.Remove(name)
}
fsys.mu.Unlock()
// strip file reuse wrapper
ff := f.File
f = nil
return ff, nil
}
if ra, ok := f.File.(io.ReaderAt); ok {
return &fileReaderAt{f, ra, 0}, nil
}
return f, nil
}
func (fsys *FS) open(name string) (*file, error) {
v, err, shared := fsys.opener.Do(name, func() (interface{}, error) {
ff, err := fsys.FS.Open(name)
if err != nil {
return nil, err
}
f := &file{
File: ff,
fsys: fsys,
name: name,
refc: 1,
}
fsys.mu.Lock()
if fsys.files == nil {
fsys.files = make(map[string]*file)
}
fsys.files[name] = f
fsys.mu.Unlock()
return f, nil
})
if err != nil {
return nil, err
}
f := v.(*file)
if shared {
// increment reference count, file open was shared
fsys.mu.Lock()
if f.refc == 0 {
// retry, file is already closed
fsys.mu.Unlock()
return fsys.open(name)
}
f.refc++
fsys.mu.Unlock()
}
return f, nil
}
// KeepLast enables a cache that keeps the last n recently
// closed files open. If n <= 0, the cache is disabled.
func (fsys *FS) KeepLast(n int) {
fsys.mu.Lock()
if n <= 0 {
// disable close cache by removing the reference while
// holding the lock and clearing the cache (and closing
// cached files on eviction) without holding the lock
cc := fsys.cache
fsys.cache = nil // disable sends on fsys.closer
close(fsys.closer)
fsys.closer = nil
fsys.mu.Unlock()
// from now on just close files on cache eviction
cc.OnEvicted = func(key lru.Key, value interface{}) {
f := value.(*file)
f.File.Close()
}
cc.Clear()
return
}
defer fsys.mu.Unlock()
if fsys.cache == nil {
fsys.cache = &lru.Cache{
MaxEntries: n,
OnEvicted: func(key lru.Key, value interface{}) {
// fsys.mu is held in this function
f := value.(*file)
if f.refc == 0 {
f.fsys.closer <- f
}
},
}
fsys.closer = make(chan *file, n)
go fsys.fileCloser()
return
}
if fsys.cache.MaxEntries < n {
fsys.cache.MaxEntries = n
}
}
func (fsys *FS) fileCloser() {
for f := range fsys.closer {
f.close()
}
}
type file struct {
fs.File
fsys *FS
name string
refc int // protected by fsys.mu
read sync.Mutex
}
var _ fs.File = (*file)(nil)
func (f *file) Read(b []byte) (int, error) {
f.read.Lock()
defer f.read.Unlock()
return f.File.Read(b)
}
func (f *file) Close() error {
f.fsys.mu.Lock()
if f.refc == 0 {
f.fsys.mu.Unlock()
return fs.ErrClosed
}
f.refc--
if f.refc < 0 {
panic("negative reference count")
}
if f.refc == 0 {
closeFile := true
if f.fsys.cache != nil {
f.fsys.cache.Add(f.name, f)
closeFile = false
}
delete(f.fsys.files, f.name)
f.fsys.mu.Unlock()
if !closeFile {
return nil
}
return f.close()
}
f.fsys.mu.Unlock()
return nil
}
func (f *file) close() error {
err := f.File.Close()
f.File = nil // panic on use after close
return err
}
type fileReaderAt struct {
*file
io.ReaderAt
offset int64
}
var (
_ fs.File = (*fileReaderAt)(nil)
_ io.ReaderAt = (*fileReaderAt)(nil)
_ io.Seeker = (*fileReaderAt)(nil)
)
func (f *fileReaderAt) Read(p []byte) (n int, err error) {
n, err = f.ReadAt(p, f.offset)
f.offset += int64(n)
return n, err
}
func (f *fileReaderAt) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
// offset += 0
case io.SeekCurrent:
offset += f.offset
case io.SeekEnd:
fi, err := f.Stat()
if err != nil {
return 0, err
}
size := fi.Size()
offset += size
if offset > size {
offset = -1 // return error
}
}
if offset < 0 {
return 0, &fs.PathError{Op: "seek", Path: f.name, Err: fs.ErrInvalid}
}
f.offset = offset
return offset, nil
}