diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-09-23 09:19:22 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-07 10:14:27 +0300 |
commit | fc7ed4ea0182cf3a345f0e939e70fdfc17216bd1 (patch) | |
tree | 04f3575781d0375ffbecff2123cc1f34df1ba674 /internal/streamcache | |
parent | 125f313fbae3aa0f509a9953cb4f3934b703cd1d (diff) |
dontpanic: Refactor code to allow stopping of `GoForever()`
The dontpanic package provides a `GoForever()` function which allows the
caller to run a function forever even if it panics. While this is a
useful thing to have, the current design also causes leakage of
Goroutines because we do not have a way to stop these functions even in
the context of tests.
Introduce a new `Forever` structure which now hosts the `Go()` function.
Like this, we can also easily introduce a `Cancel()` function which
causes us to abort execution of the function.
Callers are not yet adjusted to make use of the `Cancel()` function.
Diffstat (limited to 'internal/streamcache')
-rw-r--r-- | internal/streamcache/cache.go | 2 | ||||
-rw-r--r-- | internal/streamcache/filestore.go | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/internal/streamcache/cache.go b/internal/streamcache/cache.go index ffeca32ce..882838206 100644 --- a/internal/streamcache/cache.go +++ b/internal/streamcache/cache.go @@ -145,7 +145,7 @@ func newCacheWithSleep(dir string, maxAge time.Duration, sleep func(time.Duratio dir: dir, } - dontpanic.GoForever(1*time.Minute, func() { + dontpanic.NewForever(time.Minute).Go(func() { sleepLoop(c.stop, c.maxAge, sleep, c.clean) }) go func() { diff --git a/internal/streamcache/filestore.go b/internal/streamcache/filestore.go index 67337c95f..c3d4d99cd 100644 --- a/internal/streamcache/filestore.go +++ b/internal/streamcache/filestore.go @@ -65,7 +65,7 @@ func newFilestore(dir string, maxAge time.Duration, sleep func(time.Duration), l stop: make(chan struct{}), } - dontpanic.GoForever(1*time.Minute, func() { + dontpanic.NewForever(time.Minute).Go(func() { sleepLoop(fs.stop, fs.maxAge, sleep, func() { diskUsageGauge.WithLabelValues(fs.dir).Set(fs.diskUsage()) |