refactor: 收到请求和isbn=0日志每20次打印一次

This commit is contained in:
ShenQiLun 2026-06-26 17:18:01 +08:00
parent 0c5908543a
commit 3975846f77

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"sync/atomic"
"kfz-goods-pricing/internal/service" "kfz-goods-pricing/internal/service"
) )
@ -14,6 +15,7 @@ import (
// GoodsHandler 商品处理器 // GoodsHandler 商品处理器
type GoodsHandler struct { type GoodsHandler struct {
goodsService *service.GoodsService goodsService *service.GoodsService
reqCount int64
} }
// NewGoodsHandler 创建商品处理器实例 // NewGoodsHandler 创建商品处理器实例
@ -25,11 +27,15 @@ func NewGoodsHandler(goodsService *service.GoodsService) *GoodsHandler {
// QueryGoods 查询商品接口 // QueryGoods 查询商品接口
func (h *GoodsHandler) QueryGoods(w http.ResponseWriter, r *http.Request) { func (h *GoodsHandler) QueryGoods(w http.ResponseWriter, r *http.Request) {
count := atomic.AddInt64(&h.reqCount, 1)
clientIP := r.RemoteAddr clientIP := r.RemoteAddr
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" { if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
clientIP = forwarded clientIP = forwarded
} }
if count%20 == 1 {
log.Printf("[QueryGoods] 收到请求, 来源IP: %s, Method: %s", clientIP, r.Method) log.Printf("[QueryGoods] 收到请求, 来源IP: %s, Method: %s", clientIP, r.Method)
}
// 只支持POST请求 // 只支持POST请求
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
@ -56,7 +62,9 @@ func (h *GoodsHandler) QueryGoods(w http.ResponseWriter, r *http.Request) {
isbn := r.FormValue("isbn") isbn := r.FormValue("isbn")
if isbn == "0" { if isbn == "0" {
if count%20 == 1 {
log.Printf("[QueryGoods] 收到请求 isbn=0") log.Printf("[QueryGoods] 收到请求 isbn=0")
}
var req service.QueryRequest var req service.QueryRequest
req.ISBN = isbn req.ISBN = isbn
resp := h.goodsService.QueryGoods(&req) resp := h.goodsService.QueryGoods(&req)