xuzhiheng il y a 4 ans
Parent
commit
00e084419b

+ 3 - 3
api/v1/projects.go

@@ -44,7 +44,7 @@ func getProjectsList(c *gin.Context) {
 	if projectName != "" || winner != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectListData(productID, appID, projectName, winner, bidTime, false)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	} else {
 		response.FailWithDetailed(response.ParamError, nil, "参数错误", c)
 	}
@@ -65,7 +65,7 @@ func getProjectsListDetail(c *gin.Context) {
 	if winner != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectListData(productID, appID, "", winner, "", true)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	}
 }
 
@@ -85,6 +85,6 @@ func getProjectDetail(c *gin.Context) {
 	if id != "" {
 		utils.Check(appID, productID, c, func() ([]map[string]interface{}, int, error) {
 			return service.ProjectDetailData(id)
-		}, param, requestIP)
+		}, param, requestIP, false)
 	}
 }

+ 34 - 0
manage/user/concurrentTest.go

@@ -0,0 +1,34 @@
+package user
+
+import (
+	"log"
+	"sfis/model/response"
+	"sfis/service"
+	"strconv"
+	"sync"
+
+	"github.com/gin-gonic/gin"
+)
+
+//并发测试
+func concurrentTest(c *gin.Context) {
+	count, _ := strconv.Atoi(c.PostForm("count"))
+	appid := c.PostForm("appid")
+	productId, _ := strconv.Atoi(c.PostForm("productId"))
+	var wg = sync.WaitGroup{}
+	pool := make(chan bool, 100000)
+	for i := 0; i < count; i++ {
+		wg.Add(1)
+		pool <- true
+		log.Println(i)
+		go func(i int) {
+			defer func() {
+				wg.Done()
+				<-pool
+			}()
+			service.GetConcurrentData(appid, productId, c)
+		}(i)
+	}
+	wg.Wait()
+	response.Ok(c)
+}

+ 4 - 2
manage/user/user.go

@@ -2,8 +2,6 @@ package user
 
 import (
 	"encoding/json"
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
 	"io/ioutil"
 	"net/http"
 	"sfbase/global"
@@ -14,6 +12,9 @@ import (
 	"sfis/utils"
 	"strconv"
 	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
 )
 
 func DevUserManageRegister(router *gin.Engine) {
@@ -26,6 +27,7 @@ func DevUserManageRegister(router *gin.Engine) {
 		userGroup.POST("/userProductList", userProductList)
 		userGroup.POST("/moneyRecharge", moneyRecharge)
 		userGroup.POST("/productRecharge", productRecharge)
+		userGroup.POST("/concurrentTest", concurrentTest)
 	}
 }
 

+ 3 - 0
router/pageRouter.go

@@ -26,6 +26,7 @@ func login(context *gin.Context) {
 func chooseProductPage(context *gin.Context) {
 	context.HTML(200, "chooseProduct.html", nil)
 }
+
 func createUser(context *gin.Context) {
 	context.HTML(200, "create_user.html", nil)
 }
@@ -41,9 +42,11 @@ func projectDetailsPage(context *gin.Context) {
 func projectListPage(context *gin.Context) {
 	context.HTML(200, "projectList.html", nil)
 }
+
 func projectListDetailPage(context *gin.Context) {
 	context.HTML(200, "projectListDetail.html", nil)
 }
+
 func interfaceListPage(context *gin.Context) {
 	context.HTML(200, "interfaceList.html", nil)
 }

+ 23 - 0
service/concurrentTest.go

@@ -0,0 +1,23 @@
+package service
+
+import (
+	"sfis/utils"
+
+	"github.com/gin-gonic/gin"
+)
+
+func GetConcurrentData(appid string, productId int, c *gin.Context) {
+	if productId == 1000 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectListData(productId, appid, "河南大学", "", "", false)
+		}, "", "", true)
+	} else if productId == 1001 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectDetailData("5f6bc00c499cb0822d7e3842")
+		}, "", "", true)
+	} else if productId == 1002 {
+		utils.Check(appid, productId, c, func() ([]map[string]interface{}, int, error) {
+			return ProjectListData(productId, appid, "", "中铁十五局集团有限公司", "", true)
+		}, "", "", true)
+	}
+}

+ 4 - 23
utils/api_util.go

@@ -18,7 +18,7 @@ import (
 	"go.uber.org/zap"
 )
 
-func Check(appID string, productID int, c *gin.Context, getData func() ([]map[string]interface{}, int, error), param, ip string) {
+func Check(appID string, productID int, c *gin.Context, getData func() ([]map[string]interface{}, int, error), param, ip string, concurrentTest bool) {
 	lock.MainLock.Lock()
 	userLock := lock.UserLockMap[appID]
 	lock.MainLock.Unlock()
@@ -65,27 +65,6 @@ func Check(appID string, productID int, c *gin.Context, getData func() ([]map[st
 	product := GetProductByID(productID)
 	userLock.Lock()
 	log.Println(param + "锁住......")
-	//
-	// ctx, _ := context.WithTimeout(c, 20*time.Second)
-	// go func() {
-	// 	var i = 0
-	// 	for {
-	// 		select {
-	// 		case <-ctx.Done():
-	// 			userLock.Unlock()
-	// 			return
-	// 		case <-time.After(time.Second * time.Duration(2)):
-	// 			i++
-	// 			if i == 10 {
-	// 				userLock.Unlock()
-	// 				return
-	// 			}
-	// 			continue
-	// 		}
-	// 	}
-	// }()
-	// time.Sleep(time.Second * time.Duration(25))
-	//
 	db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
 	// costModel = 0
 	switch costModel {
@@ -112,7 +91,9 @@ func Check(appID string, productID int, c *gin.Context, getData func() ([]map[st
 		})
 		limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), userProduct.ProductID, userProduct.AppID)
 		redis.Incr("limit", limittodaykey)
-		response.FailWithDetailed(response.SUCCESS, datas, "OK", c)
+		if !concurrentTest {
+			response.FailWithDetailed(response.SUCCESS, datas, "OK", c)
+		}
 	} else {
 		if strings.Contains(errStr, "不足") {
 			response.FailWithDetailed(response.LeftNumEmpty, nil, errStr, c)

+ 1 - 0
utils/cost_by_account_balance.go

@@ -44,6 +44,7 @@ func costByAccountBalance(getData func() ([]map[string]interface{}, int, error),
 			datas = data
 			if len(datas) == 0 {
 				global.Logger.Info("无数据", getUserProductError(appID, productID, err)...)
+				orderCode, err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
 			} else {
 				orderCode, err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
 			}