tianxiaoliang commented on a change in pull request #695:
URL: 
https://github.com/apache/servicecomb-service-center/pull/695#discussion_r491845162



##########
File path: server/core/backend/storage.go
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+package backend
+
+import (
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/backoff"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/server/plugin"
+       "github.com/apache/servicecomb-service-center/server/plugin/storage"
+       "sync"
+       "time"
+)
+
+var (
+       storageInstance      *StorageEngine
+       storageSingletonLock sync.Mutex
+)
+
+// usage: backend.Storage().RegisterService()
+func Storage() storage.Storage {
+       return GetStorageEngine()
+}
+
+func GetStorageEngine() *StorageEngine {
+       if storageInstance == nil {

Review comment:
       in go, there is better singleton solution, plz googl it

##########
File path: server/core/backend/storage.go
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+package backend
+
+import (
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/backoff"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/server/plugin"
+       "github.com/apache/servicecomb-service-center/server/plugin/storage"
+       "sync"
+       "time"
+)
+
+var (
+       storageInstance      *StorageEngine
+       storageSingletonLock sync.Mutex
+)
+
+// usage: backend.Storage().RegisterService()
+func Storage() storage.Storage {
+       return GetStorageEngine()
+}
+
+func GetStorageEngine() *StorageEngine {
+       if storageInstance == nil {
+               storageSingletonLock.Lock()
+               for i := 0; engineInstance == nil; i++ {
+                       inst, err := NewStorageEngine()
+                       if err != nil {
+                               log.Errorf(err, "get storage engine failed")
+                       }
+                       storageInstance = inst
+
+                       if storageInstance != nil {
+                               storageSingletonLock.Unlock()
+                               return storageInstance
+                       }
+
+                       t := backoff.GetBackoff().Delay(i)
+                       log.Errorf(nil, "initialize service center failed, 
retry after %s", t)
+                       <-time.After(t)
+               }
+               storageSingletonLock.Unlock()
+       }
+       return storageInstance
+}
+
+func NewStorageEngine() (*StorageEngine, error) {
+       instance := plugin.Plugins().Storage()

Review comment:
       stop making plugin.Plugins() became a "god class", although, we haven't 
yet start to decouple different plugin into isolated package. we must make sure 
we do not add more plugin to this package, please refer to go chassis pulgin 
design, decouple it from plugin package

##########
File path: server/core/backend/storage.go
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+package backend
+
+import (
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/backoff"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/server/plugin"
+       "github.com/apache/servicecomb-service-center/server/plugin/storage"
+       "sync"
+       "time"
+)
+
+var (
+       storageInstance      *StorageEngine
+       storageSingletonLock sync.Mutex
+)
+
+// usage: backend.Storage().RegisterService()
+func Storage() storage.Storage {
+       return GetStorageEngine()
+}
+
+func GetStorageEngine() *StorageEngine {
+       if storageInstance == nil {
+               storageSingletonLock.Lock()
+               for i := 0; engineInstance == nil; i++ {
+                       inst, err := NewStorageEngine()
+                       if err != nil {
+                               log.Errorf(err, "get storage engine failed")
+                       }
+                       storageInstance = inst
+
+                       if storageInstance != nil {
+                               storageSingletonLock.Unlock()
+                               return storageInstance
+                       }
+
+                       t := backoff.GetBackoff().Delay(i)
+                       log.Errorf(nil, "initialize service center failed, 
retry after %s", t)
+                       <-time.After(t)
+               }
+               storageSingletonLock.Unlock()
+       }
+       return storageInstance
+}
+
+func NewStorageEngine() (*StorageEngine, error) {
+       instance := plugin.Plugins().Storage()
+       if instance == nil {
+               return nil, errors.New("store shim between discovery/registry 
and database does not exist")
+       }
+       select {
+       case err := <-instance.Err():
+               plugin.Plugins().Reload(plugin.STORAGE)
+               return nil, err
+       case <-instance.Ready():

Review comment:
       too complicated to use channel for that simple function, remove them

##########
File path: server/core/backend/storage.go
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+package backend
+
+import (
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/backoff"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/server/plugin"
+       "github.com/apache/servicecomb-service-center/server/plugin/storage"
+       "sync"
+       "time"
+)
+
+var (
+       storageInstance      *StorageEngine
+       storageSingletonLock sync.Mutex
+)
+
+// usage: backend.Storage().RegisterService()
+func Storage() storage.Storage {
+       return GetStorageEngine()
+}
+
+func GetStorageEngine() *StorageEngine {
+       if storageInstance == nil {
+               storageSingletonLock.Lock()
+               for i := 0; engineInstance == nil; i++ {
+                       inst, err := NewStorageEngine()
+                       if err != nil {
+                               log.Errorf(err, "get storage engine failed")
+                       }
+                       storageInstance = inst
+
+                       if storageInstance != nil {
+                               storageSingletonLock.Unlock()
+                               return storageInstance
+                       }
+
+                       t := backoff.GetBackoff().Delay(i)
+                       log.Errorf(nil, "initialize service center failed, 
retry after %s", t)
+                       <-time.After(t)
+               }
+               storageSingletonLock.Unlock()
+       }
+       return storageInstance
+}
+
+func NewStorageEngine() (*StorageEngine, error) {
+       instance := plugin.Plugins().Storage()
+       if instance == nil {
+               return nil, errors.New("store shim between discovery/registry 
and database does not exist")

Review comment:
       fixed err must have a err type
   
   var ErrNotExist =  errors.New("store shim between discovery/registry and 
database does not exist")




----------------------------------------------------------------
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.

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


Reply via email to