Commit 7f202186 by huangchengyi

1.0

parent abbed5e0
...@@ -2,24 +2,28 @@ package main ...@@ -2,24 +2,28 @@ package main
import ( import (
"fmt" "fmt"
"github.com/syyongx/php2go" "sync"
) )
var wg sync.WaitGroup
func main() { func main() {
userCount := 10000
userCount := 1
ch := make(chan bool, 2) ch := make(chan bool, 2)
for i := 0; i < userCount; i++ { for i := 0; i < userCount; i++ {
php2go.Time() wg.Add(1)
//fmt.Println(i)
ch <- true ch <- true
//fmt.Println("ddd")
go send(ch, i) go send(ch, i)
} }
wg.Wait()
//time.Sleep(time.Second) //time.Sleep(time.Second)
} }
func send(ch chan bool, i int) { func send(ch chan bool, i int) {
defer wg.Done()
//time.Sleep(time.Second*i) //time.Sleep(time.Second*i)
fmt.Println(php2go.Time()) fmt.Println("dddd",i)
fmt.Printf("go func: %d\n", i) //fmt.Printf("go func: %d\n", i)
<- ch <- ch
} }
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"search_server/model" "search_server/model"
"search_server/pkg/common" "search_server/pkg/common"
"strings" "strings"
"sync"
"time" "time"
) )
...@@ -62,7 +63,7 @@ const SourceSkuPriceUrl = "https://catalog.sourcengine.com/api/parts/%s/offers/s ...@@ -62,7 +63,7 @@ const SourceSkuPriceUrl = "https://catalog.sourcengine.com/api/parts/%s/offers/s
请求外链 请求外链
但是价格和库存有很多条记录,我们过滤掉交期大于14天的期货之后,找一条库存大于0,价格最便宜的一条记录作为这个SKU的价格和库存 但是价格和库存有很多条记录,我们过滤掉交期大于14天的期货之后,找一条库存大于0,价格最便宜的一条记录作为这个SKU的价格和库存
*/ */
func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap { func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
if *goodsName == "" { if *goodsName == "" {
return nil return nil
...@@ -84,14 +85,17 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap { ...@@ -84,14 +85,17 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
//协程并发请求 //协程并发请求
//接收通道所有的值 //接收通道所有的值
A := orderedmap.New() A := orderedmap.New()
ch := make(chan bool,1)
wg := sync.WaitGroup{} //协程等待
ch := make(chan bool,1) //管道
p := 0; p := 0;
for _,a := range skuRsults { for _,a := range skuRsults {
goods_sn := a.Get("sku").String() //goods_sn goods_sn := a.Get("sku").String() //goods_sn
goods_name := a.Get("mpn").String() //型号 goods_name := a.Get("mpn").String() //型号
brand_name := a.Get("manufacturer").String() //品牌 brand_name := a.Get("manufacturer").String() //品牌
desc := a.Get("description").String() //描述 desc := a.Get("description").String() //描述
wg.Add(1)
ch <- true ch <- true
go func() { go func() {
B := orderedmap.New() B := orderedmap.New()
...@@ -99,7 +103,7 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap { ...@@ -99,7 +103,7 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
B.Set("goods_name",goods_name) B.Set("goods_name",goods_name)
B.Set("brand_name",brand_name) B.Set("brand_name",brand_name)
B.Set("desc",desc) B.Set("desc",desc)
flag := GetSourceInfo(ctx,B) flag := GetSourceInfo(ctx,B,&wg)
//res,_ := json.Marshal(A) //res,_ := json.Marshal(A)
if flag { if flag {
A.Set(goods_sn,B) A.Set(goods_sn,B)
...@@ -109,27 +113,14 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap { ...@@ -109,27 +113,14 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
p ++; p ++;
} }
wg.Wait()
//接收通道所有的值
//A := orderedmap.New();
//for i := 0; i < p; i++ {
// select {
// case x := <-ch:
// if x == nil {
// continue
// }
// goods_sn,_ := x.Get("goods_sn")
// A.Set(gconv.String(goods_sn),x)
// case <- time.After(time.Second *2):
// fmt.Println("超时退出等待")
// }
//}
//allRes,_ := json.Marshal(A)
return A; return A;
} }
//请求价格和库存 //请求价格和库存
func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool { func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap,wg *sync.WaitGroup) bool {
defer wg.Done()
goods_sn,_ := B.Get("goods_sn") goods_sn,_ := B.Get("goods_sn")
skuInfo := GetSourceGineData(ctx,fmt.Sprintf(SourceSkuPriceUrl,goods_sn)) skuInfo := GetSourceGineData(ctx,fmt.Sprintf(SourceSkuPriceUrl,goods_sn))
...@@ -148,7 +139,7 @@ func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool { ...@@ -148,7 +139,7 @@ func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool {
B.Set("goods_img","") B.Set("goods_img","")
B.Set("cat","") B.Set("cat","")
//todo 确定: 分类 , 来源网站sku的url ,图片路径 这三个字段留空,然后价格取: 梯度最后一个价格判断最低 && 库存大于0 && 交期<14 天 //todo 确定: 分类 , 来源网站sku的url ,图片路径 这三个字段留空,然后价格取: 梯度最后一个价格判断最低 && 库存大于0 && 交期<14 天
var LowerPrice float64 = 0 //最低价格 var LowerPrice float64 = 0 //最低价格
flag := false; flag := false;
for _, goods := range priceGoodsList { for _, goods := range priceGoodsList {
...@@ -199,7 +190,7 @@ func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool { ...@@ -199,7 +190,7 @@ func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool {
LowerPrice = apiLowerPrice; LowerPrice = apiLowerPrice;
}else if LowerPrice < apiLowerPrice { //不是最低价格,跳过 }else if LowerPrice < apiLowerPrice { //不是最低价格,跳过
common.PrintDebugHtml(ctx,"不是最低价格跳过") common.PrintDebugHtml(ctx,"不是最低价格跳过")
continue; continue;
} }
//写入数据 //写入数据
mpq := goods.Get("mpq").Int() mpq := goods.Get("mpq").Int()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment