浏览代码

Merge branch 'dev/v4.8.40_dx' of qmx/jy into feature/v4.8.40

duxin 2 年之前
父节点
当前提交
3253cd10e9
共有 2 个文件被更改,包括 61 次插入0 次删除
  1. 2 0
      src/config.yaml
  2. 59 0
      src/jfw/front/tags.go

+ 2 - 0
src/config.yaml

@@ -7,3 +7,5 @@ resourceCenterKey: "resource.rpc" #资源中台
 entManageApplication: "entmanageapplication.rpc" #企业管理中台
 activityKey: "activity.rpc" #营销中台rpc
 publicserviceKey: "publicservice.rpc"
+executionNum: 10
+awaitNum: 10

+ 59 - 0
src/jfw/front/tags.go

@@ -11,6 +11,7 @@ import (
 	"net/http"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 
 	qu "app.yhyue.com/moapp/jybase/common"
@@ -35,10 +36,17 @@ type Tags struct {
 
 var LetterArr = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
 var bidField = `"_id","site","title","publishtime","toptype","subtype","type","area","href","bidopentime","winner","buyer","bidamount","budget","s_subscopeclass","projectname","detail"`
+var (
+	BeExecuting      int
+	NeedWait         chan int
+	NotificationWait = make(chan int, 1)
+	syw, rwm         = &sync.RWMutex{}, &sync.RWMutex{}
+)
 var seoBidField = `"_id","title","publishtime","subtype","area","href","bidamount","budget","s_subscopeclass","projectname"`
 
 func init() {
 	xweb.AddAction(&Tags{})
+	NeedWait = make(chan int, qu.IntAll(config.Sysconfig["awaitNum"]))
 }
 
 /*
@@ -600,6 +608,33 @@ func GetBiddingPlatformType() (nameToCode map[string]int64, codeToName map[int64
 	return
 }
 
+func LimitedConcurrency() int {
+	var ExecutionState int //执行状态 0 直接执行 1 等待执行 -1 已达上线
+	executionNum := qu.IntAll(config.Sysconfig["executionNum"])
+	awaitNum := qu.IntAll(config.Sysconfig["awaitNum"])
+	var wg sync.WaitGroup
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		if BeExecuting < executionNum { //并发数不足 直接执行
+			ExecutionState = 0
+			BeExecuting++
+			return
+		}
+		syw.Lock()
+		defer syw.Unlock()
+		if len(NeedWait) < awaitNum { //等待人数不足 加入等待队列
+			ExecutionState = 1
+			NeedWait <- 1
+			return
+		}
+		ExecutionState = -1 //并发数与等待数以达到上线 直接结束
+		return
+	}()
+	wg.Wait()
+	return ExecutionState
+}
+
 func (this *Tags) GetBidding(industry, area, city, stype, keyword string, request *http.Request, responseWriter http.ResponseWriter, session *httpsession.Session) ([]map[string]interface{}, int64, bool) {
 	rediskey := fmt.Sprintf("pcseo_getbidding_%s_%s_%s_%s_%s", industry, area, city, stype, keyword)
 	rediskeyCount := fmt.Sprintf("pcseo_getbidding_count_%s_%s_%s_%s_%s", industry, area, city, stype, keyword)
@@ -609,6 +644,30 @@ func (this *Tags) GetBidding(industry, area, city, stype, keyword string, reques
 		return qu.ObjArrToMapArr(l), int64(count), false
 	} else {
 		if area != "" || stype != "" || industry != "" || city != "" || keyword != "" {
+			// 限制并发数
+			executionState := LimitedConcurrency()
+		env:
+			switch executionState {
+			case 0:
+				defer func() {
+					BeExecuting-- //执行完成减去在执行数
+					rwm.Lock()
+					defer rwm.Unlock()
+					if len(NeedWait) > 0 { //查看是否有等待执行 有通知可以执行
+						NotificationWait <- 1
+					}
+				}()
+			case 1:
+				select { //监听等待
+				case <-NotificationWait:
+					<-NeedWait
+					executionState = 0
+					BeExecuting++
+					goto env //可以开始执行
+				}
+			default:
+				return nil, 0, true
+			}
 			if public.Lst.IsLimited(request, responseWriter, session, false) == 1 { //没有被限制
 				defer public.Lst.Limit()
 			} else {