Browse Source

wip:日志保存调整

fuwencai 1 year ago
parent
commit
f9f2aa9ced

+ 1 - 3
jyBXCore/api/init/logs.go

@@ -4,13 +4,11 @@ import (
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"jyBXCore/api/internal/config"
 	"jyBXCore/api/internal/config"
 	"jyBXCore/entity"
 	"jyBXCore/entity"
-	"log"
 )
 )
 
 
 var SearchLog *entity.SaveLogs
 var SearchLog *entity.SaveLogs
 
 
-func LogInit(sl *entity.SaveLogs,c config.SaveLogConfig, mgo mongodb.MongodbSim) {
+func LogInit(sl *entity.SaveLogs, c config.SaveLogConfig, mgo mongodb.MongodbSim) {
 	sl = entity.NewSaveLog(c.Name, c.CollName, c.MgoSaveCacheSize, c.SPSize, c.BulkSize, c.TimeAfter, c.Timeout, mgo)
 	sl = entity.NewSaveLog(c.Name, c.CollName, c.MgoSaveCacheSize, c.SPSize, c.BulkSize, c.TimeAfter, c.Timeout, mgo)
 	go sl.SaveMgo()
 	go sl.SaveMgo()
-	log.Println("初始化日志保存")
 }
 }

+ 4 - 1
jyBXCore/entity/logs.go

@@ -52,7 +52,10 @@ func (s *SaveLogs) SaveMgo() {
 	log.Println(fmt.Sprintf("%s Save...", s.Name))
 	log.Println(fmt.Sprintf("%s Save...", s.Name))
 	arr := make([]map[string]interface{}, s.BulkSize)
 	arr := make([]map[string]interface{}, s.BulkSize)
 	index := 0
 	index := 0
+	timer := time.NewTimer(time.Duration(s.TimeAfter) * time.Millisecond)
+	defer timer.Stop()
 	for {
 	for {
+		timer.Reset(time.Duration(s.TimeAfter) * time.Millisecond)
 		select {
 		select {
 		case v := <-s.MgoSaveCache:
 		case v := <-s.MgoSaveCache:
 			arr[index] = v
 			arr[index] = v
@@ -68,7 +71,7 @@ func (s *SaveLogs) SaveMgo() {
 				arr = make([]map[string]interface{}, s.BulkSize)
 				arr = make([]map[string]interface{}, s.BulkSize)
 				index = 0
 				index = 0
 			}
 			}
-		case <-time.After(time.Duration(s.TimeAfter) * time.Millisecond):
+		case <-timer.C:
 			if index > 0 {
 			if index > 0 {
 				s.SP <- true
 				s.SP <- true
 				go func(arru []map[string]interface{}) {
 				go func(arru []map[string]interface{}) {

+ 0 - 3
jyBXSubscribe/api/init/logs.go

@@ -4,8 +4,6 @@ import (
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"jyBXSubscribe/api/internal/config"
 	"jyBXSubscribe/api/internal/config"
 	"jyBXSubscribe/entity"
 	"jyBXSubscribe/entity"
-
-	"log"
 )
 )
 
 
 var SubscribeUpdateLog *entity.SaveLogs
 var SubscribeUpdateLog *entity.SaveLogs
@@ -13,5 +11,4 @@ var SubscribeUpdateLog *entity.SaveLogs
 func LogInit(sl *entity.SaveLogs, c config.SaveLogConfig, mgo mongodb.MongodbSim) {
 func LogInit(sl *entity.SaveLogs, c config.SaveLogConfig, mgo mongodb.MongodbSim) {
 	sl = entity.NewSaveLog(c.Name, c.CollName, c.MgoSaveCacheSize, c.SPSize, c.BulkSize, c.TimeAfter, c.Timeout, mgo)
 	sl = entity.NewSaveLog(c.Name, c.CollName, c.MgoSaveCacheSize, c.SPSize, c.BulkSize, c.TimeAfter, c.Timeout, mgo)
 	go sl.SaveMgo()
 	go sl.SaveMgo()
-	log.Println("初始化日志保存")
 }
 }

+ 4 - 1
jyBXSubscribe/entity/logs.go

@@ -52,7 +52,10 @@ func (s *SaveLogs) SaveMgo() {
 	log.Println(fmt.Sprintf("%s Save...", s.Name))
 	log.Println(fmt.Sprintf("%s Save...", s.Name))
 	arr := make([]map[string]interface{}, s.BulkSize)
 	arr := make([]map[string]interface{}, s.BulkSize)
 	index := 0
 	index := 0
+	timer := time.NewTimer(time.Duration(s.TimeAfter) * time.Millisecond)
+	defer timer.Stop()
 	for {
 	for {
+		timer.Reset(time.Duration(s.TimeAfter) * time.Millisecond)
 		select {
 		select {
 		case v := <-s.MgoSaveCache:
 		case v := <-s.MgoSaveCache:
 			arr[index] = v
 			arr[index] = v
@@ -68,7 +71,7 @@ func (s *SaveLogs) SaveMgo() {
 				arr = make([]map[string]interface{}, s.BulkSize)
 				arr = make([]map[string]interface{}, s.BulkSize)
 				index = 0
 				index = 0
 			}
 			}
-		case <-time.After(time.Duration(s.TimeAfter) * time.Millisecond):
+		case <-timer.C:
 			if index > 0 {
 			if index > 0 {
 				s.SP <- true
 				s.SP <- true
 				go func(arru []map[string]interface{}) {
 				go func(arru []map[string]interface{}) {