-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Garbage collect caches that aren't needed any more
It's possible that due to pipelines disappearing, or being updated, some caches will no longer be needed. If these are shut down, the number of caches will only grow, which constitutes a leak of resources (though not necessarily a serious one, since it will max out at `clusters x types`). To be able to shut down caches that are no longer needed, we need to be able to do a few things: 1. detect when they aren't needed 2. stop them running by cancelling the context they were started with 3. have them stop when the controller is shutting down To do the first, I index the cache keys used by each pipeline. When a cache's event handler is run, it checks to see if the cache has entries in the index; and if not, it's not used by any pipeline and can be shut down. The second and third things are slightly tricky to arrange together. I have introduced `runner` (in runner.go) which can be Start()ed by the manager and thus gain access to its context, and which can then construct a context for each cache, derived from the manager's context. Each cache gets its own cancel func that can be used to shut it down, but will also be shut down by the manager when it's shutting down itself. Signed-off-by: Michael Bridgen <[email protected]>
- Loading branch information
Showing
3 changed files
with
169 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package leveltriggered | ||
|
||
// This is a dead simple way to run things using a manager's context as a base, so that they will | ||
// get shut down when the manager does. It must be constructed with `newRunner`, and added to a manager: | ||
// | ||
// r := newRunner() | ||
// mgr.Add(r) | ||
// | ||
// then you can use it to run funcs: | ||
// | ||
// cancel := r.run(func(context.Context)) | ||
// | ||
// The func will be run with its own context derived from the root context supplied by the manager, | ||
// with the cancel func returned to the caller as shown. This way you can cancel the context yourself, | ||
// or let it be canceled when the manager shuts down. | ||
// | ||
// It'll deadlock if you call `run` before adding it to a manager (or otherwise calling `Start`). | ||
|
||
type runWithContext struct { | ||
ctx context.Context | ||
Check failure on line 20 in controllers/leveltriggered/runner.go GitHub Actions / lint
|
||
run func(context.Context) | ||
} | ||
|
||
type runner struct { | ||
rootContext context.Context | ||
tostart chan runWithContext | ||
ready chan struct{} | ||
} | ||
|
||
func newRunner() *cacheRunner { | ||
return &runner{ | ||
tostart: make(chan runWithContext), | ||
ready: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (r *runner) run(fn func(ctx context.Context)) context.CancelFunc { | ||
<-r.ready // wait until there's a root context | ||
ctx, cancel := context.WithCancel(r.rootContext) | ||
r.tostart <- runWithContext{ | ||
run: fn, | ||
ctx: ctx, | ||
} | ||
return cancel | ||
} | ||
|
||
// Start makes this a manager.Runnable so it can be registered with | ||
// the manager and use its root context. | ||
func (r *runner) Start(ctx context.Context) error { | ||
r.rootContext = ctx | ||
close(r.ready) // broadcast that things can be run | ||
for { | ||
select { | ||
case randc := <-r.tostart: | ||
go randc.run(randc.ctx) | ||
case <-r.rootContext.Done(): | ||
return nil | ||
} | ||
} | ||
} |