diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2021-02-06 10:22:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-06 10:22:14 +0100 |
commit | 3cec26d42db0529f14e82115515c9731ead2dd78 (patch) | |
tree | dcac50a16051b55efa1450fbd251979d27aa7424 /fs.go | |
parent | Fix race condition in Client.DoTimeout (diff) | |
download | fasthttp-3cec26d42db0529f14e82115515c9731ead2dd78.tar.gz fasthttp-3cec26d42db0529f14e82115515c9731ead2dd78.tar.bz2 fasthttp-3cec26d42db0529f14e82115515c9731ead2dd78.zip |
Allow stopping FS handler cleanup gorountine (#942)
* Allow stopping FS handler cleanup gorountine
* CleanStop
Diffstat (limited to 'fs.go')
-rw-r--r-- | fs.go | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -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 } @@ -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() } }() |