Skip to content

Commit

Permalink
Allow stopping FS handler cleanup gorountine (#942)
Browse files Browse the repository at this point in the history
* Allow stopping FS handler cleanup gorountine

* CleanStop
  • Loading branch information
erikdubbelboer authored Feb 6, 2021
1 parent ed1cedd commit 3cec26d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
27 changes: 26 additions & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ type FS struct {
// FSCompressedFileSuffixes is used by default.
CompressedFileSuffixes map[string]string

// If CleanStop is set, the channel can be closed to stop the cleanup handlers
// for the FS RequestHandlers created with NewRequestHandler.
// NEVER close this channel while the handler is still being used!
CleanStop chan struct{}

once sync.Once
h RequestHandler
}
Expand Down Expand Up @@ -399,9 +404,29 @@ func (fs *FS) initRequestHandler() {

go func() {
var pendingFiles []*fsFile

clean := func() {
pendingFiles = h.cleanCache(pendingFiles)
}

if fs.CleanStop != nil {
t := time.NewTicker(cacheDuration / 2)
for {
select {
case <-t.C:
clean()
case _, stillOpen := <-fs.CleanStop:
// Ignore values send on the channel, only stop when it is closed.
if !stillOpen {
t.Stop()
return
}
}
}
}
for {
time.Sleep(cacheDuration / 2)
pendingFiles = h.cleanCache(pendingFiles)
clean()
}
}()

Expand Down
20 changes: 20 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ func testPathNotFound(t *testing.T, pathNotFoundFunc RequestHandler) {
req.SetRequestURI("http//some.url/file")
ctx.Init(&req, nil, TestLogger{t})

stop := make(chan struct{})
defer close(stop)

fs := &FS{
Root: "./",
PathNotFound: pathNotFoundFunc,
CleanStop: stop,
}
fs.NewRequestHandler()(&ctx)

Expand Down Expand Up @@ -299,9 +303,13 @@ func TestServeFileUncompressed(t *testing.T) {
func TestFSByteRangeConcurrent(t *testing.T) {
t.Parallel()

stop := make(chan struct{})
defer close(stop)

fs := &FS{
Root: ".",
AcceptByteRange: true,
CleanStop: stop,
}
h := fs.NewRequestHandler()

Expand Down Expand Up @@ -329,9 +337,13 @@ func TestFSByteRangeConcurrent(t *testing.T) {
func TestFSByteRangeSingleThread(t *testing.T) {
t.Parallel()

stop := make(chan struct{})
defer close(stop)

fs := &FS{
Root: ".",
AcceptByteRange: true,
CleanStop: stop,
}
h := fs.NewRequestHandler()

Expand Down Expand Up @@ -468,11 +480,15 @@ func testParseByteRangeError(t *testing.T, v string, contentLength int) {
func TestFSCompressConcurrent(t *testing.T) {
// This test can't run parallel as files in / might by changed by other tests.

stop := make(chan struct{})
defer close(stop)

fs := &FS{
Root: ".",
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
CleanStop: stop,
}
h := fs.NewRequestHandler()

Expand Down Expand Up @@ -501,11 +517,15 @@ func TestFSCompressConcurrent(t *testing.T) {
func TestFSCompressSingleThread(t *testing.T) {
// This test can't run parallel as files in / might by changed by other tests.

stop := make(chan struct{})
defer close(stop)

fs := &FS{
Root: ".",
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
CleanStop: stop,
}
h := fs.NewRequestHandler()

Expand Down

0 comments on commit 3cec26d

Please sign in to comment.