From fc7ed4ea0182cf3a345f0e939e70fdfc17216bd1 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 23 Sep 2021 08:19:22 +0200 Subject: 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. --- internal/streamcache/cache.go | 2 +- internal/streamcache/filestore.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'internal/streamcache') 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()) -- cgit v1.2.3