1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-13 22:09:31 +00:00

Merge pull request #58 from MbolotSuse/count-diff

Changing count watch to only return changed counts
This commit is contained in:
Michael Bolot
2022-12-14 10:15:18 -06:00
committed by GitHub
4 changed files with 464 additions and 26 deletions

View File

@@ -24,6 +24,7 @@ var (
}
)
// Register registers a new count schema. This schema isn't a true resource but instead returns counts for other resources
func Register(schemas *types.APISchemas, ccache clustercache.ClusterCache) {
schemas.MustImportAndCustomize(Count{}, func(schema *types.APISchema) {
schema.CollectionMethods = []string{http.MethodGet}
@@ -110,9 +111,10 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.AP
}, nil
}
// Watch creates a watch for the Counts schema. This returns only the counts which have changed since the watch was established
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest) (chan types.APIEvent, error) {
var (
result = make(chan types.APIEvent, 100)
result = make(chan Count, 100)
counts map[string]ItemCount
gvkToSchema = map[schema2.GroupVersionKind]*types.APISchema{}
countLock sync.Mutex
@@ -178,18 +180,13 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.
}
counts[schema.ID] = itemCount
countsCopy := map[string]ItemCount{}
for k, v := range counts {
countsCopy[k] = *v.DeepCopy()
changedCount := map[string]ItemCount{
schema.ID: itemCount,
}
result <- types.APIEvent{
Name: "resource.change",
ResourceType: "counts",
Object: toAPIObject(Count{
ID: "count",
Counts: countsCopy,
}),
result <- Count{
ID: "count",
Counts: changedCount,
}
return nil
@@ -205,7 +202,8 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.
return onChange(false, gvk, key, obj, nil)
})
return buffer(result), nil
// buffer the counts so that we don't spam the consumer with constant updates
return countsBuffer(result), nil
}
func (s *Store) schemasToWatch(apiOp *types.APIRequest) (result []*types.APISchema) {