From a4ea6380ed70468b1bbaca0328a65686960fcd52 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期一, 20 四月 2020 16:23:30 +0800
Subject: [PATCH] add svLock

---
 server.go |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/server.go b/server.go
index 5b46e50..f23728c 100644
--- a/server.go
+++ b/server.go
@@ -3,6 +3,7 @@
 import (
 	"encoding/json"
 	"log"
+	"sync"
 	"time"
 
 	"golang.org/x/net/context"
@@ -51,7 +52,8 @@
 	// publisher, we are going to publish the changes of the set here
 	publisher *Publisher
 
-	svInfo map[string]interface{}
+	svInfo map[string][]byte
+	svLock sync.RWMutex
 }
 
 type Publisher struct {
@@ -150,7 +152,7 @@
 	}
 
 	responses = NewStringSet()
-	d.services.svInfo = make(map[string]interface{}, 0)
+	d.services.svInfo = make(map[string][]byte, 0)
 	for {
 		msg, err = d.sock.Recv()
 		if err != nil {
@@ -162,7 +164,9 @@
 		} else {
 			if  json.Unmarshal(msg, &si) == nil {
 				responses.Add(si.ServiceId)
+				d.services.svLock.Lock()
 				d.services.svInfo[si.ServiceId] = si.Info
+				d.services.svLock.Unlock()
 			}
 		}
 	}
@@ -219,7 +223,7 @@
 	s := &Services{
 		nodes:     NewStringSet(),
 		publisher: publisher,
-		svInfo: make(map[string]interface{}, 0),
+		svInfo: make(map[string][]byte, 0),
 	}
 
 	return s
@@ -243,8 +247,10 @@
 	return d.services.nodes
 }
 
-func (d *DiscoveryServer) SvInfo() map[string]interface{} {
-	m := make(map[string]interface{})
+func (d *DiscoveryServer) SvInfo() map[string][]byte {
+	d.services.svLock.Lock()
+	defer d.services.svLock.Unlock()
+	m := make(map[string][]byte)
 	for k,v := range d.services.svInfo {
 		m[k] = v
 	}

--
Gitblit v1.8.0