2 Commits b6413af5d1 ... 64aac7e44d

Author SHA1 Message Date
  jiantao 64aac7e44d Merge branch 'master' of http://code.jschengta.com/zhanlang/miads 4 years ago
  jiantao a17c3726f8 性能调优 4 years ago

+ 9 - 0
ads_handler.go

@@ -100,6 +100,7 @@ func adsHandler(c *gin.Context) {
100 100
 			Androidid    string `json:"android_id"`
101 101
 			ShortMessage string `json:"short_message"`
102 102
 			ReqSource    string `json:"req_source"`
103
+			Hehe         string `json:"hehe"`
103 104
 		}{
104 105
 			Ip:           ip,
105 106
 			Imei:         request.Imei,
@@ -113,6 +114,7 @@ func adsHandler(c *gin.Context) {
113 114
 			Androidid:    request.Androidid,
114 115
 			ShortMessage: "ads_api_log",
115 116
 			ReqSource:    "zhiku",
117
+			Hehe:         "heheheheh",
116 118
 		}
117 119
 
118 120
 		graylog.Log(grayLogData)
@@ -506,6 +508,7 @@ func adsHandler(c *gin.Context) {
506 508
 			// 先跑定投
507 509
 			customAdData, err := addata.GetCustomAdsInfos(&dspInfo, 0, 0, xiaomiResponseFlag)
508 510
 			if err != nil {
511
+				log.WithField("request_id", dspInfo.RequestId).Infof("get custom ads info failed: %s", err)
509 512
 				c.String(404, "get custom ads info failed: %s", err.Error())
510 513
 				return
511 514
 			}
@@ -515,6 +518,7 @@ func adsHandler(c *gin.Context) {
515 518
 			if customAdData == nil {
516 519
 				customAdData, err = addata.GetCustomAdsInfos(&dspInfo, 0, 1, xiaomiResponseFlag)
517 520
 				if err != nil {
521
+					log.WithField("request_id", dspInfo.RequestId).Errorf("get fix custom ads info failed: %s", err)
518 522
 					c.String(404, "get fix custom ads info failed: %s", err.Error())
519 523
 					return
520 524
 				}
@@ -603,6 +607,7 @@ func adsHandler(c *gin.Context) {
603 607
 					log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %+v", customAdData)
604 608
 					jsonBytes, err := json.Marshal(customAdData)
605 609
 					if err != nil {
610
+						log.WithField("request_id", dspInfo.RequestId).Errorf("marshal rsp1 custom ad failed: %s", err)
606 611
 						c.String(404, "marsha custom addata failed: %s", err.Error())
607 612
 						return
608 613
 					}
@@ -617,6 +622,7 @@ func adsHandler(c *gin.Context) {
617 622
 					graylog.ReportGrayLog(request, dspInfo, 0, 0)
618 623
 					rspBytes, err := json.Marshal(response)
619 624
 					if err != nil {
625
+						log.WithField("request_id", dspInfo.RequestId).Errorf("marshal rsp1 failed: %s", err)
620 626
 						c.String(404, "marshal Response failed: %s", err.Error())
621 627
 						return
622 628
 					}
@@ -629,9 +635,11 @@ func adsHandler(c *gin.Context) {
629 635
 					graylog.ReportGrayLog(request, dspInfo, 0, 0)
630 636
 					rspBytes, err := json.Marshal(customAdData)
631 637
 					if err != nil {
638
+						log.WithField("request_id", dspInfo.RequestId).Errorf("marshal rsp2 failed: %s", err)
632 639
 						c.String(404, err.Error())
633 640
 						return
634 641
 					}
642
+					log.WithField("request_id", dspInfo.RequestId).Infof("rsp2: %s", rspBytes)
635 643
 					c.String(200, string(rspBytes))
636 644
 					return
637 645
 				}
@@ -641,6 +649,7 @@ func adsHandler(c *gin.Context) {
641 649
 	redis_data.SetAdsRealRequestNum(advertiser)
642 650
 	rspBytes, err := json.Marshal(response)
643 651
 	if err != nil {
652
+		log.WithField("request_id", dspInfo.RequestId).Errorf("marshal rsp3 failed: %s", err)
644 653
 		c.String(404, err.Error())
645 654
 		return
646 655
 	}

+ 13 - 10
adslib/addata/custom.go

@@ -54,6 +54,8 @@ func GetOneAds(dsp *utils.DspParam, orderType int, fixFlag int) (*redis_data.AdO
54 54
 	if err != nil {
55 55
 		return nil, err
56 56
 	}
57
+
58
+	log.WithField("request_id", dsp.RequestId).Infof("get one ads, order len: %d", len(orders))
57 59
 	if len(orders) == 0 {
58 60
 		return nil, nil
59 61
 	}
@@ -150,11 +152,12 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
150 152
 		log.WithField("request_id", dsp.RequestId).Errorf("get one ads failed: %s", err)
151 153
 		return nil, err
152 154
 	}
155
+	log.WithField("request_id", dsp.RequestId).Infof("lat get one ad info, order: %+v, xiaomi has flag: %d", order, xiaomiHasFlag)
156
+
153 157
 	if order == nil {
154 158
 		return nil, nil
155 159
 	}
156 160
 
157
-	log.WithField("request_id", dsp.RequestId).Infof("begin got custom ad info, xiaomi has flag: %d", xiaomiHasFlag)
158 161
 	if xiaomiHasFlag == 1 {
159 162
 		if strings.Index(order.Title, "_ios") > 0 {
160 163
 			return nil, nil
@@ -186,16 +189,17 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
186 189
 	rate := float32(order.ShowKpi) / float32(needDispatchCnt)
187 190
 
188 191
 	lineValue := 0
192
+	key := ""
189 193
 	if order.LineType == 1 {
190 194
 		// 订单定制曲线
191
-		lineValue, err = redis_data.GetOrderPerMinuteNeedDispatchCnt(order.OrderID, curTime)
195
+		lineValue, key, err = redis_data.GetOrderPerMinuteNeedDispatchCnt(order.OrderID, curTime)
192 196
 	} else {
193
-		lineValue, err = redis_data.GetPerMinuteNeedDispatchCnt(curTime)
197
+		lineValue, key, err = redis_data.GetPerMinuteNeedDispatchCnt(curTime)
194 198
 	}
195 199
 	if err != nil {
196 200
 		return nil, err
197 201
 	}
198
-	log.WithField("request_id", dsp.RequestId).Infof("line value: %d, rate: %f", lineValue, rate)
202
+	log.WithField("request_id", dsp.RequestId).Infof("key: %s, line value: %d, rate: %f", key, lineValue, rate)
199 203
 
200 204
 	// 当前分钟需要放出去的数量
201 205
 	curMinuteNeedDispatchCnt := int(float32(lineValue) * rate)
@@ -205,11 +209,10 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
205 209
 
206 210
 	redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", curMinuteNeedDispatchCnt, curTime)
207 211
 
208
-	log.WithField("request_id", dsp.RequestId).Infof("tt thirds: %+v", order)
209 212
 	// 获取当前分钟已经完成的下发
210
-	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
213
+	curMinuteFinishedDispatchCnt, key, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
211 214
 
212
-	log.WithField("request_id", dsp.RequestId).Infof("o value: %d, w value: %d", curMinuteFinishedDispatchCnt, curMinuteNeedDispatchCnt)
215
+	log.WithField("request_id", dsp.RequestId).Infof("o value: %s, %d, w value: %d", key, curMinuteFinishedDispatchCnt, curMinuteNeedDispatchCnt)
213 216
 	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
214 217
 		data := AdData{
215 218
 			Duration:  5,
@@ -219,11 +222,11 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
219 222
 		}
220 223
 
221 224
 		//放量
222
-		_, err := redis_data.IncrFinishedDispatchCount(order.OrderID, "show", 1, curTime)
225
+		_, err := redis_data.IncrFinishedDispatchCount(order.OrderID, "show", curTime)
223 226
 		if err != nil {
224 227
 			return nil, err
225 228
 		}
226
-		_, err = redis_data.IncrPreMinuteFinishedDispatchCount(order.OrderID, "show", 1, curTime)
229
+		_, err = redis_data.IncrPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
227 230
 		if err != nil {
228 231
 			return nil, err
229 232
 		}
@@ -326,7 +329,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHa
326 329
 			addi = genMonitorAction("CLICK", order.Title, dsp.ReqSource, clickUrl)
327 330
 			data.TargetAddition = append(data.TargetAddition, *addi)
328 331
 			md5Skip := utils.Md5(targetUrl)
329
-			_, err = redis_data.IncrFinishedDispatchCount(order.OrderID, "click", 1, curTime)
332
+			_, err = redis_data.IncrFinishedDispatchCount(order.OrderID, "click", curTime)
330 333
 			if err != nil {
331 334
 				log.WithField("request_id", dsp.RequestId).Errorf("incr finished dispatch count failed: %s", err)
332 335
 			}

+ 3 - 8
adslib/ads_redis/redis.go

@@ -14,9 +14,9 @@ func Setup() error {
14 14
 	host := adslib.GetConf().RedisHost
15 15
 	password := adslib.GetConf().RedisPassword
16 16
 	RedisConn = &redis.Pool{
17
-		MaxIdle:     10,
18
-		MaxActive:   10,
19
-		IdleTimeout: 50,
17
+		MaxIdle:     20,
18
+		MaxActive:   50,
19
+		IdleTimeout: 3 * time.Minute,
20 20
 		Dial: func() (redis.Conn, error) {
21 21
 			c, err := redis.Dial("tcp", host)
22 22
 			if err != nil {
@@ -32,11 +32,6 @@ func Setup() error {
32 32
 			}
33 33
 			return c, err
34 34
 		},
35
-		TestOnBorrow: func(c redis.Conn, t time.Time) error {
36
-			_, err := c.Do("PING")
37
-			fmt.Println(err)
38
-			return err
39
-		},
40 35
 	}
41 36
 
42 37
 	return nil

+ 1 - 0
adslib/config.go

@@ -26,6 +26,7 @@ type SvrConf struct {
26 26
 	LogPath            string                                `toml:"log_path"`
27 27
 	RedisHost          string                                `toml:"redis_host"`
28 28
 	RedisPassword      string                                `toml:"redis_password"`
29
+	LogLevel           string                                `toml:"log_level"`
29 30
 }
30 31
 type FlogControlItem struct {
31 32
 	Percent  int `toml:"percent"`

+ 3 - 3
adslib/graylog/graylog.go

@@ -16,9 +16,9 @@ var grayLog *gelf.Gelf
16 16
 
17 17
 func init() {
18 18
 	grayLog = gelf.New(gelf.Config{
19
-		GraylogPort:     62001,
20
-		GraylogHostname: "172.18.138.7",
21
-		Connection:      "wan",
19
+		GraylogPort:     62002,
20
+		GraylogHostname: "172.18.199.72",
21
+		Connection:      "lan",
22 22
 		MaxChunkSizeWan: 42,
23 23
 		MaxChunkSizeLan: 1337,
24 24
 	})

+ 2 - 1
adslib/redis_data/order.go

@@ -113,6 +113,7 @@ func GetOrderInfos(dsp *utils.DspParam, fixFlag int) ([]AdOrderInfo, error) {
113 113
 					}
114 114
 				}
115 115
 				if !isValidChannel {
116
+					log.WithField("request_id", dsp.RequestId).Tracef("not valid channel ad: %+v", order)
116 117
 					continue
117 118
 				}
118 119
 			}
@@ -153,7 +154,7 @@ func GetOrderInfos(dsp *utils.DspParam, fixFlag int) ([]AdOrderInfo, error) {
153 154
 		}
154 155
 
155 156
 		if !needInsert {
156
-			return []AdOrderInfo{}, nil
157
+			continue
157 158
 		}
158 159
 
159 160
 		if dsp != nil {

+ 20 - 18
adslib/redis_data/redis_data.go

@@ -312,12 +312,12 @@ func GetFinishedDispatchCount(orderId int64, dispatchType string, curTime time.T
312 312
 
313 313
 // 增加已经投放的量
314 314
 // dispatchType: "click", "show"
315
-func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
315
+func IncrFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
316 316
 	conn := ads_redis.RedisConn.Get()
317 317
 	defer conn.Close()
318 318
 
319 319
 	key := fmt.Sprintf("order_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
320
-	rsp, err := conn.Do("INCR", key, incrCnt)
320
+	rsp, err := conn.Do("INCR", key)
321 321
 	if err != nil {
322 322
 		return 0, err
323 323
 	}
@@ -328,35 +328,34 @@ func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int,
328 328
 
329 329
 // 获取当前分钟已经投放的量
330 330
 // dispatchType: "click", "show"
331
-func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
331
+func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, string, error) {
332 332
 	conn := ads_redis.RedisConn.Get()
333 333
 	defer conn.Close()
334 334
 
335 335
 	curMinutes := curTime.Hour()*60 + curTime.Minute()
336 336
 	key := fmt.Sprintf("order_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
337 337
 	rsp, err := conn.Do("GET", key)
338
-	log.Infof("%s %s", key, rsp)
339 338
 	if err != nil {
340
-		return 0, err
339
+		return 0, key, err
341 340
 	}
342 341
 
343 342
 	if rsp == nil {
344
-		return 0, nil
343
+		return 0, key, nil
345 344
 	}
346 345
 
347 346
 	count, _ := redis.Int(rsp, err)
348
-	return count, nil
347
+	return count, key, nil
349 348
 }
350 349
 
351 350
 // 增加当前分钟已经投放的量
352 351
 // dispatchType: "click", "show"
353
-func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
352
+func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
354 353
 	conn := ads_redis.RedisConn.Get()
355 354
 	defer conn.Close()
356 355
 
357 356
 	curMinutes := curTime.Hour()*60 + curTime.Minute()
358 357
 	key := fmt.Sprintf("order_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
359
-	rsp, err := conn.Do("INCR", key, incrCnt)
358
+	rsp, err := conn.Do("INCR", key)
360 359
 	if err != nil {
361 360
 		return 0, err
362 361
 	}
@@ -386,7 +385,7 @@ func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curT
386 385
 }
387 386
 
388 387
 // 获取总量每分钟要投放的量
389
-func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, error) {
388
+func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, string, error) {
390 389
 	conn := ads_redis.RedisConn.Get()
391 390
 	defer conn.Close()
392 391
 
@@ -395,19 +394,19 @@ func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, error) {
395 394
 	rsp, err := conn.Do("GET", key)
396 395
 
397 396
 	if err != nil {
398
-		return 0, err
397
+		return 0, key, err
399 398
 	}
400 399
 
401 400
 	if rsp == nil {
402
-		return 0, nil
401
+		return 0, key, nil
403 402
 	}
404 403
 
405 404
 	count, _ := redis.Int(rsp, err)
406
-	return count, nil
405
+	return count, key, nil
407 406
 }
408 407
 
409 408
 // 获取订单每分钟要投放的量
410
-func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, error) {
409
+func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, string, error) {
411 410
 	conn := ads_redis.RedisConn.Get()
412 411
 	defer conn.Close()
413 412
 
@@ -416,15 +415,15 @@ func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, er
416 415
 	rsp, err := conn.Do("GET", key)
417 416
 
418 417
 	if err != nil {
419
-		return 0, err
418
+		return 0, key, err
420 419
 	}
421 420
 
422 421
 	if rsp == nil {
423
-		return 0, nil
422
+		return 0, key, nil
424 423
 	}
425 424
 
426 425
 	count, _ := redis.Int(rsp, err)
427
-	return count, nil
426
+	return count, key, nil
428 427
 }
429 428
 
430 429
 // 获取ios的ua和imei
@@ -464,8 +463,11 @@ func GetIosUaImei(ip string) (string, string, error) {
464 463
 		Imei string
465 464
 		Ua   string
466 465
 	}
466
+
467 467
 	rspBytes, _ := redis.Bytes(rsp, err)
468
-	err = json.Unmarshal(rspBytes, &uaImeiInfo)
468
+	rspStr := strings.ReplaceAll(string(rspBytes), "'", "\"")
469
+
470
+	err = json.Unmarshal([]byte(rspStr), &uaImeiInfo)
469 471
 	if err != nil {
470 472
 		return "", "", err
471 473
 	}

+ 1 - 0
conf/config.toml

@@ -12,6 +12,7 @@ ip2region_file = "../conf/ip2region_20200113.db"
12 12
 log_path = "../log"
13 13
 redis_host="r-wz9sbx1faq7j6zxxgt.redis.rds.aliyuncs.com:6379"
14 14
 redis_password="3iXAJFuI$J"
15
+log_level=trace
15 16
 
16 17
 
17 18
 ## 流量占比控制

+ 1 - 0
go.mod

@@ -8,6 +8,7 @@ require (
8 8
 	github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
9 9
 	github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
10 10
 	github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
11
+	github.com/gin-contrib/pprof v1.3.0
11 12
 	github.com/gin-gonic/gin v1.6.3
12 13
 	github.com/gomodule/redigo v2.0.0+incompatible
13 14
 	github.com/jarcoal/httpmock v1.0.5

+ 3 - 0
go.sum

@@ -11,8 +11,11 @@ github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJ
11 11
 github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw=
12 12
 github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 h1:6VSn3hB5U5GeA6kQw4TwWIWbOhtvR2hmbBJnTOtqTWc=
13 13
 github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6/go.mod h1:YxOVT5+yHzKvwhsiSIWmbAYM3Dr9AEEbER2dVayfBkg=
14
+github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0=
15
+github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0=
14 16
 github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
15 17
 github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
18
+github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
16 19
 github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
17 20
 github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
18 21
 github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=

+ 9 - 2
main_linux.go

@@ -10,6 +10,7 @@ import (
10 10
 	"time"
11 11
 
12 12
 	"github.com/fvbock/endless"
13
+	"github.com/gin-contrib/pprof"
13 14
 	log "github.com/sirupsen/logrus"
14 15
 )
15 16
 
@@ -40,6 +41,7 @@ func setupLogger(logName string, logLevel log.Level) {
40 41
 
41 42
 func setupRouter() *gin.Engine {
42 43
 	r := gin.Default()
44
+	pprof.Register(r) // 性能
43 45
 
44 46
 	r.POST("/ads", adsHandler)
45 47
 	return r
@@ -47,8 +49,13 @@ func setupRouter() *gin.Engine {
47 49
 
48 50
 func main() {
49 51
 	ads_redis.Setup()
50
-	adslib.GetConf()
51
-	setupLogger(adslib.GetConf().LogPath+"/ads.log", log.TraceLevel)
52
+	conf := adslib.GetConf()
53
+	logLevel, err := log.ParseLevel(conf.LogLevel)
54
+	if err != nil {
55
+		log.Errorf("parse log level failed, use info level, log str: %s, err: %s", conf.LogLevel, err)
56
+		logLevel = log.InfoLevel
57
+	}
58
+	setupLogger(conf.LogPath+"/ads.log", logLevel)
52 59
 
53 60
 	r := setupRouter()
54 61
 	endless.ListenAndServe(":1111", r)