AlexStocks commented on a change in pull request #1620:
URL: https://github.com/apache/dubbo-go/pull/1620#discussion_r760087089



##########
File path: registry/polaris/core.go
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//@Author: chuntaojun <[email protected]>
+//@Description:
+//@Time: 2021/11/26 00:28
+
+package polaris
+
+import (
+       "sync"
+       "time"
+)
+
+import (
+       gxchan "github.com/dubbogo/gost/container/chan"
+
+       "github.com/polarismesh/polaris-go/api"
+       "github.com/polarismesh/polaris-go/pkg/model"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/config_center"
+       "dubbo.apache.org/dubbo-go/v3/remoting"
+)
+
+// PolarisServiceWatcher
+type PolarisServiceWatcher struct {
+       consumer       api.ConsumerAPI
+       subscribeParam *api.WatchServiceRequest
+       events         *gxchan.UnboundedChan
+       lock           *sync.RWMutex
+       subscribers    []func(remoting.EventType, []model.Instance)
+       execOnce       *sync.Once
+}
+
+// newPolarisWatcher create PolarisServiceWatcher to do watch service action
+func newPolarisWatcher(param *api.WatchServiceRequest, consumer 
api.ConsumerAPI) (*PolarisServiceWatcher, error) {
+       watcher := &PolarisServiceWatcher{
+               subscribeParam: param,
+               consumer:       consumer,
+               events:         gxchan.NewUnboundedChan(1024),
+               lock:           &sync.RWMutex{},
+               subscribers:    make([]func(remoting.EventType, 
[]model.Instance), 0),
+               execOnce:       &sync.Once{},
+       }
+       return watcher, nil
+}
+
+// AddSubscriber add subscriber into watcher's subscribers
+func (watcher *PolarisServiceWatcher) AddSubscriber(subscriber 
func(remoting.EventType, []model.Instance)) {
+
+       watcher.lazyRun()
+
+       watcher.lock.Lock()
+       defer watcher.lock.Unlock()
+
+       watcher.subscribers = append(watcher.subscribers, subscriber)
+}
+
+// lazyRun Delayed execution, only triggered when AddSubscriber is called, and 
will only be executed once
+func (watcher *PolarisServiceWatcher) lazyRun() {
+       watcher.execOnce.Do(func() {
+               go watcher.startWatcher()

Review comment:
       这俩函数就这么直接异步,不考虑使用同步原语吗?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to