From 41a5fcf7fedf91ae9d01a12d18fbb098df8b4e54 Mon Sep 17 00:00:00 2001
From: zhangmeng <zhangmeng@aiotlink.com>
Date: 星期二, 14 一月 2020 17:29:38 +0800
Subject: [PATCH] add chan cache size

---
 run.go |   66 ++++++++++++++++++++++++++++++---
 1 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/run.go b/run.go
index f915794..353c8ab 100644
--- a/run.go
+++ b/run.go
@@ -2,6 +2,7 @@
 
 import (
 	"context"
+	"os"
 	"sync"
 	"time"
 
@@ -11,6 +12,8 @@
 	"basic.com/pubsub/protomsg.git"
 	"basic.com/valib/gogpu.git"
 )
+
+const maxTryBeforeReboot = 10
 
 type face struct {
 	handle *SDKFace
@@ -29,6 +32,40 @@
 	ipc2Rule            string
 	ruleMsgMaxCacheSize int
 	reserved            map[string]interface{}
+
+	running     bool
+	rebootUntil int
+	mtxRunning  sync.Mutex
+}
+
+func (f *face) maybeReboot(ctx context.Context) {
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		default:
+			f.mtxRunning.Lock()
+			running := f.running
+			f.mtxRunning.Unlock()
+
+			if running {
+				f.rebootUntil = 0
+
+				f.mtxRunning.Lock()
+				f.running = false
+				f.mtxRunning.Unlock()
+
+			} else {
+				f.rebootUntil++
+				f.fnLogger("Face No Running: ", f.rebootUntil)
+				if f.rebootUntil > maxTryBeforeReboot {
+					f.fnLogger("Face Too Long Running, Reboot")
+					os.Exit(0)
+				}
+			}
+			time.Sleep(time.Second)
+		}
+	}
 }
 
 // Create create sdk
@@ -114,6 +151,15 @@
 		ipc2Rule:            ipc2Rule,
 		ruleMsgMaxCacheSize: ruleMaxSize,
 		reserved:            reserved,
+
+		running:     true,
+		rebootUntil: maxTryBeforeReboot,
+	}
+}
+
+func (f *face) release() {
+	if f.handle != nil {
+		f.handle.Free()
 	}
 }
 
@@ -127,8 +173,8 @@
 	)
 	ipcRcv := sdkhelper.GetIpcAddress(s.shm, s.id+postPull)
 	ipcSnd := sdkhelper.GetIpcAddress(s.shm, s.id+postPush)
-	chRcv := make(chan []byte)
-	chSnd := make(chan sdkstruct.MsgSDK)
+	chRcv := make(chan []byte, s.maxChannel)
+	chSnd := make(chan sdkstruct.MsgSDK, s.maxChannel)
 
 	rcver := sdkhelper.NewReciever(ipcRcv, chRcv, s.shm, s.fnLogger)
 	snder := sdkhelper.NewSender(ipcSnd, chSnd, s.shm, s.fnLogger)
@@ -141,11 +187,12 @@
 	go torule.Run(ctx)
 
 	go s.run(ctx, chRcv, chSnd)
+
+	go s.maybeReboot(ctx)
 }
 
 //////////////////////////////////////////////////////////////////
 const (
-	cacheFrameNum   = 3
 	trackChnTimeout = time.Duration(10)
 )
 
@@ -178,7 +225,7 @@
 
 func (f *face) run(ctx context.Context, in <-chan []byte, out chan<- sdkstruct.MsgSDK) {
 
-	chMsg := make(chan protomsg.SdkMessage)
+	chMsg := make(chan protomsg.SdkMessage, f.maxChannel)
 	go sdkhelper.UnserilizeProto(ctx, in, chMsg, f.fnLogger)
 
 	for {
@@ -194,10 +241,11 @@
 			}
 
 			if _, ok := f.ftrackChans[rMsg.Cid]; ok {
+				f.fnLogger("Face Cache Size: ", len(f.ftrackChans))
 				f.ftrackChans[rMsg.Cid] <- rMsg
 			} else {
 
-				f.ftrackChans[rMsg.Cid] = make(chan protomsg.SdkMessage, cacheFrameNum)
+				f.ftrackChans[rMsg.Cid] = make(chan protomsg.SdkMessage, f.maxChannel)
 				chn := f.getAvailableChn()
 				if chn < 0 {
 					f.fnLogger("TOO MUCH CHANNEL")
@@ -253,14 +301,20 @@
 			// conv to bgr24 and resize
 			imgW, imgH := int(i.Width), int(i.Height)
 
+			f.fnLogger("Face Start Run:", dtchn, "CAMERAID: ", rMsg.Cid)
+
 			count, data, _ := f.handle.Run(i.Data, imgW, imgH, 3, dtchn)
 
 			sdkhelper.EjectResult(data, rMsg, out)
+			f.mtxRunning.Lock()
+			f.running = true
+			f.mtxRunning.Unlock()
+
 			var id, name string
 			if rMsg.Tasklab != nil {
 				id, name = rMsg.Tasklab.Taskid, rMsg.Tasklab.Taskname
 			}
-			f.fnLogger("CAMERAID: ", rMsg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", count)
+			f.fnLogger("Chan:", dtchn, "CAMERAID: ", rMsg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", count)
 
 			sc++
 			if sc == 25 {

--
Gitblit v1.8.0