From 316b611ff39e038b763bb67c2335383ce29505e4 Mon Sep 17 00:00:00 2001 From: LHL Date: Sat, 21 Dec 2024 17:15:40 +0800 Subject: [PATCH] fix: Hang in watch when resouce list count is greater than channel length (#115) * fix: Hang in watch when resouce list count is greater than channel length * Update nacos_rest.go --------- Co-authored-by: Kent Dong --- src/apiserver/pkg/registry/file_rest.go | 16 +++++++++------- src/apiserver/pkg/registry/nacos_rest.go | 20 +++++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/apiserver/pkg/registry/file_rest.go b/src/apiserver/pkg/registry/file_rest.go index cd9a19a..238f023 100644 --- a/src/apiserver/pkg/registry/file_rest.go +++ b/src/apiserver/pkg/registry/file_rest.go @@ -653,15 +653,17 @@ func (f *fileREST) Watch(ctx context.Context, options *metainternalversion.ListO return nil, err } - danger := reflect.ValueOf(list).Elem() - items := danger.FieldByName("Items") + go func() { + danger := reflect.ValueOf(list).Elem() + items := danger.FieldByName("Items") - for i := 0; i < items.Len(); i++ { - fw.ch <- watch.Event{ - Type: watch.Added, - Object: listItemToRuntimeObject(items.Index(i)), + for i := 0; i < items.Len(); i++ { + fw.ch <- watch.Event{ + Type: watch.Added, + Object: listItemToRuntimeObject(items.Index(i)), + } } - } + }() f.fileWatchersMutex.Lock() f.fileWatchers[fw.id] = fw diff --git a/src/apiserver/pkg/registry/nacos_rest.go b/src/apiserver/pkg/registry/nacos_rest.go index 1a0a4c3..07e7951 100644 --- a/src/apiserver/pkg/registry/nacos_rest.go +++ b/src/apiserver/pkg/registry/nacos_rest.go @@ -461,15 +461,17 @@ func (n *nacosREST) Watch(ctx context.Context, options *metainternalversion.List return nil, err } - danger := reflect.ValueOf(list).Elem() - items := danger.FieldByName("Items") - - for i := 0; i < items.Len(); i++ { - nw.SendEvent(watch.Event{ - Type: watch.Added, - Object: listItemToRuntimeObject(items.Index(i)), - }, true) - } + go func() { + danger := reflect.ValueOf(list).Elem() + items := danger.FieldByName("Items") + + for i := 0; i < items.Len(); i++ { + nw.SendEvent(watch.Event{ + Type: watch.Added, + Object: listItemToRuntimeObject(items.Index(i)), + }, true) + } + }() n.watchersMutex.Lock() defer n.watchersMutex.Unlock()