metadata: use resource max and end on registration

Ensure the registered resource type does not conflict
with existing resource types or over the max.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2022-04-19 14:25:31 -07:00
parent 8367f69fb5
commit 3b82f9e33c
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
2 changed files with 11 additions and 8 deletions

View File

@ -280,6 +280,12 @@ func (m *DB) RegisterMutationCallback(fn func(bool)) {
// - Collectible Resources must track whether the resource is active and/or
// lease membership.
func (m *DB) RegisterCollectibleResource(t gc.ResourceType, c Collector) {
if t < resourceEnd {
panic("cannot re-register metadata resource")
} else if t >= gc.ResourceMax {
panic("resource type greater than max")
}
m.wlock.Lock()
defer m.wlock.Unlock()
@ -287,16 +293,11 @@ func (m *DB) RegisterCollectibleResource(t gc.ResourceType, c Collector) {
m.collectors = map[gc.ResourceType]Collector{}
}
switch t {
case ResourceContainer:
panic("cannot re-register metadata resource")
default:
if _, ok := m.collectors[t]; ok {
panic("cannot register collectible type twice")
}
m.collectors[t] = c
}
}
// GCStats holds the duration for the different phases of the garbage collector
type GCStats struct {

View File

@ -44,6 +44,8 @@ const (
ResourceLease
// ResourceIngest specifies a content ingest
ResourceIngest
// resourceEnd is the end of specified resource types
resourceEnd
)
const (