From bc12925d7c72f9c6afb8bd8ca8a22f66e8d6a756 Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Tue, 9 May 2023 07:59:44 -0700 Subject: [PATCH] fix loop variable scope issue Signed-off-by: Doug Fawley --- pkg/cache/v3/simple.go | 9 ++++++--- pkg/cache/v3/simple_test.go | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/cache/v3/simple.go b/pkg/cache/v3/simple.go index 5f7cf5d4c0..43fb6a24a6 100644 --- a/pkg/cache/v3/simple.go +++ b/pkg/cache/v3/simple.go @@ -321,7 +321,8 @@ func superset(names map[string]bool, resources map[string]types.ResourceWithTTL) return nil } -// CreateWatch returns a watch for an xDS request. +// CreateWatch returns a watch for an xDS request. A nil function may be +// returned if an error occurs. func (cache *snapshotCache) CreateWatch(request *Request, streamState stream.StreamState, value chan Response) func() { nodeID := cache.hash.ID(request.Node) @@ -365,8 +366,9 @@ func (cache *snapshotCache) CreateWatch(request *Request, streamState stream.Str if err := cache.respond(context.Background(), request, value, resources, version, false); err != nil { cache.log.Errorf("failed to send a response for %s%v to nodeID %q: %s", request.TypeUrl, request.ResourceNames, nodeID, err) + return nil } - return nil + return func() {} } } } @@ -387,9 +389,10 @@ func (cache *snapshotCache) CreateWatch(request *Request, streamState stream.Str if err := cache.respond(context.Background(), request, value, resources, version, false); err != nil { cache.log.Errorf("failed to send a response for %s%v to nodeID %q: %s", request.TypeUrl, request.ResourceNames, nodeID, err) + return nil } - return nil + return func() {} } func (cache *snapshotCache) nextWatchID() int64 { diff --git a/pkg/cache/v3/simple_test.go b/pkg/cache/v3/simple_test.go index d5e1f92711..cc7ea14218 100644 --- a/pkg/cache/v3/simple_test.go +++ b/pkg/cache/v3/simple_test.go @@ -342,6 +342,7 @@ func TestSnapshotCacheWatch(t *testing.T) { func TestConcurrentSetWatch(t *testing.T) { c := cache.NewSnapshotCache(false, group{}, logger{t: t}) for i := 0; i < 50; i++ { + i := i t.Run(fmt.Sprintf("worker%d", i), func(t *testing.T) { t.Parallel() id := fmt.Sprintf("%d", i%2) @@ -358,7 +359,6 @@ func TestConcurrentSetWatch(t *testing.T) { Node: &core.Node{Id: id}, TypeUrl: rsrc.EndpointType, }, streamState, value) - defer cancel() } })