You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/01/13 06:55:47 UTC

[servicecomb-kie] branch master updated: refactor (#64)

This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new 463852e  refactor (#64)
463852e is described below

commit 463852ec342e8e07375955e98bcad4720e5106e4
Author: Shawn <xi...@gmail.com>
AuthorDate: Mon Jan 13 14:55:42 2020 +0800

    refactor (#64)
---
 server/pubsub/event_handler.go | 53 ++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/server/pubsub/event_handler.go b/server/pubsub/event_handler.go
index 338c5a7..941e143 100644
--- a/server/pubsub/event_handler.go
+++ b/server/pubsub/event_handler.go
@@ -33,31 +33,38 @@ func (h *EventHandler) HandleEvent(e serf.Event) {
 	switch e.EventType().String() {
 	case "user":
 		if strings.Contains(e.String(), EventKVChange) {
-			ue := e.(serf.UserEvent)
-			ke, err := NewKVChangeEvent(ue.Payload)
-			if err != nil {
-				openlogging.Error("invalid json:" + string(ue.Payload))
-			}
-			openlogging.Debug("kv event:" + ke.Key)
-			topics.Range(func(key, value interface{}) bool { //range all topics
-				t, err := ParseTopicString(key.(string))
-				if err != nil {
-					openlogging.Error("can not parse topic:" + key.(string))
-					return true
-				}
-				if t.Match(ke) {
-					observers := value.(map[string]*Observer)
-					mutexObservers.Lock()
-					defer mutexObservers.Unlock()
-					for k, v := range observers {
-						v.Event <- ke
-						delete(observers, k)
-					}
-				}
-				return true
-			})
+			handleKVEvent(e)
 		}
+	}
+
+}
 
+func handleKVEvent(e serf.Event) {
+	ue := e.(serf.UserEvent)
+	ke, err := NewKVChangeEvent(ue.Payload)
+	if err != nil {
+		openlogging.Error("invalid json:" + string(ue.Payload))
 	}
+	openlogging.Debug("kv event:" + ke.Key)
+	topics.Range(func(key, value interface{}) bool { //range all topics
+		t, err := ParseTopicString(key.(string))
+		if err != nil {
+			openlogging.Error("can not parse topic:" + key.(string))
+			return true
+		}
+		if t.Match(ke) {
+			fireEvent(value, ke)
+		}
+		return true
+	})
+}
 
+func fireEvent(value interface{}, ke *KVChangeEvent) {
+	observers := value.(map[string]*Observer)
+	mutexObservers.Lock()
+	defer mutexObservers.Unlock()
+	for k, v := range observers {
+		v.Event <- ke
+		delete(observers, k)
+	}
 }