From 3b82f9e33c3f30e80e1625231235dd1837ab74ce Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Tue, 19 Apr 2022 14:25:31 -0700 Subject: [PATCH] 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 --- metadata/db.go | 17 +++++++++-------- metadata/gc.go | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/metadata/db.go b/metadata/db.go index 9d8a4f5c9..d746e1560 100644 --- a/metadata/db.go +++ b/metadata/db.go @@ -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,15 +293,10 @@ 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 + 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 diff --git a/metadata/gc.go b/metadata/gc.go index 4fadb40bf..a5551380b 100644 --- a/metadata/gc.go +++ b/metadata/gc.go @@ -44,6 +44,8 @@ const ( ResourceLease // ResourceIngest specifies a content ingest ResourceIngest + // resourceEnd is the end of specified resource types + resourceEnd ) const (