From abfd401180d1eb09c8ed1c24797c3e503e45fa08 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期日, 07 二月 2021 15:02:50 +0800
Subject: [PATCH] 调整超时为3s

---
 hbusc.go |  102 +++++++++++++++++++++++++++++++--------------------
 1 files changed, 62 insertions(+), 40 deletions(-)

diff --git a/hbusc.go b/hbusc.go
index e5f7dd6..640b55c 100644
--- a/hbusc.go
+++ b/hbusc.go
@@ -7,7 +7,6 @@
 	"errors"
 	"fmt"
 	"os"
-	"strconv"
 	"sync"
 	"time"
 )
@@ -48,10 +47,9 @@
 	sockSub *sockClient  //璁㈤槄涓婚鐨剆ocket锛岀嚎绋嬪疄鏃舵帴鏀舵秷鎭紝闇�瑕佸崟鐙鐞�
 
 	sockWorker 	*sockClient  //鍙戠粰浠绘剰鐨剆erver,鐭殏鐨剅equest client
-	mtxWorker 	sync.Mutex	 //SendAndRecv鍙兘涓嶆槸绾跨▼瀹夊叏鐨�
+	//mtxWorker 	sync.Mutex	 //SendAndRecv鍙兘涓嶆槸绾跨▼瀹夊叏鐨�
 
 	chSub chan TransInfo
-	chReply chan TransInfo
 }
 
 //鑾峰彇鍏朵粬杩涚▼鍙戠粰姝ocket鐨勬秷鎭�,鍙互鏄痵ub鐨勬帴鏀讹紝涔熷彲浠ユ槸reply鐨勬帴鏀躲��
@@ -65,7 +63,7 @@
 			wg.Done()
 			return
 		default:
-			n := s.RecvfromTimeout(&data, &key, 100) //鐩墠10001杩斿洖鍊艰〃绀鸿秴鏃�
+			n := s.RecvfromTimeout(&data, &key, 1000) //鐩墠10001杩斿洖鍊艰〃绀鸿秴鏃�
 			if n == 0 {
 				var info MsgInfo
 				if err := json.Unmarshal(data, &info);err == nil {
@@ -80,7 +78,25 @@
 					logFn("unmarshal to MsgInfo err:", err)
 				}
 			} else {
-				time.Sleep(10 * time.Millisecond)
+				time.Sleep(100 * time.Millisecond)
+			}
+		}
+	}
+}
+
+func recvandsendRoutine(ctx context.Context, s *bhomebus.Socket, wg *sync.WaitGroup,serveFn func(rdata []byte, rkey int, sdata *[]byte) bool, logFn func(...interface{})) {
+	for {
+		select {
+		case <-ctx.Done():
+			logFn("recvandsendRoutine ctx.Done")
+			wg.Done()
+			return
+		default:
+			n := s.RecvandsendTimeout(1000, serveFn) //鐩墠10001杩斿洖鍊艰〃绀鸿秴鏃�
+			if n != 0 {
+				logFn("RecvandsendTimeout success")
+			} else {
+				//time.Sleep(100 * time.Millisecond)
 			}
 		}
 	}
@@ -88,11 +104,11 @@
 
 //Register
 func Register(ctx context.Context, q chan os.Signal, config *Config, ri *RegisterInfo) (*BHBus,error) {
-	handle := &BHBus{
+	handle := &BHBus {
+		ctx: ctx,
 		conf: config,
 		m: make(map[string]*sockServer),
 		chSub: make(chan TransInfo, config.chSize),
-		chReply: make(chan TransInfo, config.chSize),
 	}
 
 	var err error
@@ -154,7 +170,8 @@
 			}
 
 			var rMsg []bhomebus.Mesg
-			n := regSock.Sendandrecv(regAddr, msg, &rMsg) //n浠h〃鎴愬姛鍙戦�佺殑鑺傜偣鐨勪釜鏁�
+			n := regSock.SendandrecvTimeout(regAddr, msg, &rMsg, handle.conf.sendTimeOut) //n浠h〃鎴愬姛鍙戦�佺殑鑺傜偣鐨勪釜鏁�
+			handle.printLog("regSock.SendandrecvTimeout n:", n)
 			if n == 1 && len(rMsg) == 1 {
 				var cr Reply
 				if err = json.Unmarshal(rMsg[0].Data, &cr);err != nil {
@@ -176,10 +193,9 @@
 					} else {
 						handle.printLog("cr:", cr)
 					}
-
 				}
 			} else {
-				time.Sleep(100 * time.Millisecond)
+				time.Sleep(1 * time.Second)
 			}
 		}
 	}
@@ -199,6 +215,7 @@
 
 	//缁存寔蹇冭烦鐨剆ocket
 	sockHB := bhomebus.OpenSocket()
+	handle.printLog("open sockHB")
 	handle.sockHB = &sockClient{
 		sock: sockHB,
 		peer: int(regR.HeartbeatKey),
@@ -209,9 +226,10 @@
 	if ri.PubTopic != nil && len(ri.PubTopic) > 0 {
 		sockReply := bhomebus.OpenSocket()
 		sockReply.ForceBind(int(regR.ReplyKey))
-		handle.wg.Add(1)
+		handle.printLog("after pubTopic forceBind")
+		//handle.wg.Add(1)
 		//serve server reply
-		go recvRoutine(ctx, sockReply, handle.wg, handle.chReply, handle.printLog)
+		//go recvRoutine(ctx, sockReply, handle.wg, handle.chReply, handle.printLog)
 		handle.sockRep = &sockServer{
 			sock: sockReply,
 			info: &ri.Proc,
@@ -231,6 +249,7 @@
 		//璁㈤槄娑堟伅鐨剆ocket
 		sockSub := bhomebus.OpenSocket()
 		//璁㈤槄鎵�鏈変富棰�
+		handle.printLog("start Sub topics")
 		for _,v := range ri.SubTopic {
 			subN := sockSub.Sub(v)
 			handle.printLog("subTopic:", v, " ret n:", subN)
@@ -270,8 +289,8 @@
 		return err
 	}
 
-	h.mtxWorker.Lock()
-	defer h.mtxWorker.Unlock()
+	//h.mtxWorker.Lock()
+	//defer h.mtxWorker.Unlock()
 	netNodes := append([]bhomebus.NetNode{}, bhomebus.NetNode{
 		Key: h.conf.regKey,
 	})
@@ -405,7 +424,7 @@
 				if err == nil {
 					return nodes, nil
 				} else {
-					h.printLog("unmarshal err:", err, "nodes:", nodes)
+					h.printLog("unmarshal err:", err, "nodes:", nodes, " center reply.Data:", reply.Data)
 					return nil, fmt.Errorf("unmarshal to nodes err:%s", err.Error())
 				}
 			} else {
@@ -465,19 +484,19 @@
 	return nil, fmt.Errorf("request err")
 }
 
-func (h *BHBus) Reply(replyKey int, i *Reply) error {
-	data,err := json.Marshal(*i)
-	if err != nil {
-		return err
-	}
-
-	n := h.sockRep.sock.SendtoTimeout(data, replyKey, h.conf.sendTimeOut)
-	h.printLog("reply to key:", replyKey, " n:",n)
-	if n != 0 {
-		return errors.New("reply sendToTimeOut n:"+strconv.Itoa(n))
-	}
-	return nil
-}
+//func (h *BHBus) Reply(replyKey int, i *Reply) error {
+//	data,err := json.Marshal(*i)
+//	if err != nil {
+//		return err
+//	}
+//
+//	n := h.sockRep.sock.SendtoTimeout(data, replyKey, h.conf.sendTimeOut)
+//	h.printLog("reply to key:", replyKey, " n:",n)
+//	if n != 0 {
+//		return errors.New("reply sendToTimeOut n:"+strconv.Itoa(n))
+//	}
+//	return nil
+//}
 
 
 //鍙彂閫佽姹傦紝涓嶉渶瑕佸簲绛�.
@@ -487,8 +506,8 @@
 	if err != nil {
 		return err
 	}
-	h.mtxWorker.Lock()
-	defer h.mtxWorker.Unlock()
+	//h.mtxWorker.Lock()
+	//defer h.mtxWorker.Unlock()
 	n := h.sockWorker.sock.SendtoTimeout(data, key, h.conf.sendTimeOut)
 	if n != 0 {
 		return fmt.Errorf("sendOnly ret n:%d", n)
@@ -504,8 +523,8 @@
 	rNodes := append([]bhomebus.NetNode{}, bhomebus.NetNode{
 		Key: KEY_QUERY,
 	})
-	h.mtxWorker.Lock()
-	defer h.mtxWorker.Unlock()
+	//h.mtxWorker.Lock()
+	//defer h.mtxWorker.Unlock()
 	var ret []bhomebus.Mesg
 	n := h.sockWorker.sock.SendandrecvTimeout(rNodes, data,&ret, h.conf.sendTimeOut)
 	h.printLog("requestCenter n:", n, "len(ret):", len(ret))
@@ -535,6 +554,14 @@
 	return err
 }
 
+func (h *BHBus) PubTimeout(nodes []bhomebus.NetNode, msg *MsgInfo, timeout int) int {
+	data,err := json.Marshal(*msg)
+	if err == nil {
+		return h.sockPub.sock.PubTimeout(nodes, msg.Topic, data, timeout)
+	}
+	return -1
+}
+
 //杩藉姞璁㈤槄鐨勪富棰樻秷鎭�
 func (h *BHBus) Sub(topics []string) {
 	if topics != nil {
@@ -557,18 +584,13 @@
 
 
 //鑾峰彇sub 鎴栬�呴渶瑕乺eply鐨勬秷鎭�
-func (h *BHBus) GetMsg() (subMsg *MsgInfo, replyMsg *MsgInfo, replyKey int) {
-	if h.sockHB == nil && h.sockRep == nil && h.sockPub == nil && h.sockSub == nil && h.sockWorker == nil {
-		return nil,nil, -1
+func (h *BHBus) GetMsg() (subMsg *MsgInfo) {
+	if h.sockHB == nil && h.sockPub == nil && h.sockSub == nil && h.sockWorker == nil {
+		return nil
 	}
 	if len(h.chSub) >0 {
 		m  := <-h.chSub
 		subMsg = m.info
-	}
-	if len(h.chReply) >0 {
-		m := <-h.chReply
-		replyMsg = m.info
-		replyKey = m.port
 	}
 	return
 }
\ No newline at end of file

--
Gitblit v1.8.0