You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/08/24 02:50:33 UTC

[GitHub] [dubbo-go] wangxw666 commented on a change in pull request #1372: Ftr: Add config loader hooks for 1.5

wangxw666 commented on a change in pull request #1372:
URL: https://github.com/apache/dubbo-go/pull/1372#discussion_r694450364



##########
File path: config/config_loader_hook.go
##########
@@ -0,0 +1,367 @@
+/*
+ * 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 config
+
+import (
+	"fmt"
+	"reflect"
+)
+
+import (
+	"github.com/apache/dubbo-go/common/constant"
+)
+
+const (
+	hookParams = constant.HookParams
+)
+
+type LoaderHook interface {
+	emit() bool
+	// if you want to use emitWithParams, you must add HookParams field and make emit return false
+	emitWithParams(params interface{}) bool
+}
+
+func getLoaderHookType(t LoaderHook) reflect.Type {
+	hookType := reflect.TypeOf(t)
+	if reflect.Ptr == hookType.Kind() {
+		hookType = hookType.Elem()
+	}
+	return hookType
+}
+
+func getLoaderHookTypeString(t LoaderHook) string {
+	return getLoaderHookType(t).String()
+}
+
+func getLoaderHookInfo(v reflect.Value) reflect.Value {
+	if reflect.Ptr == v.Kind() {
+		v = v.Elem()
+	}
+	return v
+}
+
+func getLoaderHookParams(t LoaderHook) interface{} {
+	hookType := getLoaderHookType(t)
+	_, existed := hookType.FieldByName(hookParams)
+	if !existed {
+		panic(fmt.Sprintf("Hook field [%s] not existed for %s", hookParams, hookType.Name()))
+	}
+	hookParams := getLoaderHookInfo(reflect.ValueOf(t)).FieldByName(hookParams)
+	return getLoaderHookInfo(hookParams).Interface()
+}
+
+func AddLoaderHooks(hooks ...LoaderHook) {
+	if len(hooks) > 0 {
+		if loaderHooks == nil {
+			loaderHooks = map[string][]LoaderHook{}
+		}
+		for _, hook := range hooks {
+			hookType := getLoaderHookTypeString(hook)
+			var hooks []LoaderHook
+			if hs, ok := loaderHooks[hookType]; ok {
+				hooks = hs
+			}
+			if hooks == nil {
+				hooks = []LoaderHook{}
+			}
+			loaderHooks[hookType] = append(hooks, hook)
+		}
+	}
+}
+
+func RemoveLoaderHooks(hooks ...LoaderHook) {
+	if loaderHooks != nil && len(loaderHooks) > 0 {
+		for _, rmHook := range hooks {
+			hookType := getLoaderHookTypeString(rmHook)
+			if hooks, ok := loaderHooks[hookType]; ok {
+				for index, targetHook := range hooks {
+					if rmHook == targetHook {
+						newHooks := append(hooks[:index], hooks[index+1:]...)
+						if len(newHooks) == 0 {
+							delete(loaderHooks, hookType)
+						} else {
+							loaderHooks[hookType] = newHooks
+						}
+						break
+					}
+				}
+			}
+		}
+	}
+}
+
+func emitHook(t LoaderHook) {
+	if loaderHooks != nil && len(loaderHooks) > 0 {
+		hookType := getLoaderHookTypeString(t)
+		if hooks, ok := loaderHooks[hookType]; ok {
+			for _, hook := range hooks {
+				if !hook.emit() {
+					hook.emitWithParams(getLoaderHookParams(t))
+				}
+			}
+		}
+	}
+}
+
+// emit on before shutdown
+type BeforeShutdownHookEvent func()
+
+type beforeShutdownHook struct {
+	onEvent func()
+}
+
+func (h *beforeShutdownHook) emit() bool {
+	h.onEvent()
+	return true
+}
+
+func (h *beforeShutdownHook) emitWithParams(params interface{}) bool {
+	return params == nil
+}
+
+func NewBeforeShutdownHook(onEvent BeforeShutdownHookEvent) *beforeShutdownHook {
+	return &beforeShutdownHook{
+		onEvent,
+	}
+}
+
+// Consumer Hooks
+// emit on before export consumer
+type BeforeConsumerConnectHookEvent func(info *ConsumerConnectInfo)
+
+// emit on consumer export success
+type ConsumerConnectSuccessHookEvent func(info *ConsumerConnectInfo)
+
+// emit on consumer export fail

Review comment:
       consumer no "export"  ,  consumer is "reference"




-- 
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: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org