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