A 层搬运(整包,源=poc-final 5e09a4cc): 协议四栈/pkg 标准件 12 包/util 计费纯函数/middleware/迁移链 repository·mysql v1~v19+clickhouse/model·entity/config/service·audit·balance/app/cmd·{migrate,reconcile,cmppbench}/tools·gen-dict/_bench/test·integration/openapi·yaml/监控三件/运维资产(compose+kind+CI 两套+Makefile+runbook)/scripts 种子/前端基建(根配置+src 基建目录,views 留 B 层)
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
# 缩小构建上下文:与运行镜像无关的文件不进入 Docker daemon
|
||||
.git
|
||||
.gitignore
|
||||
.idea
|
||||
.vscode
|
||||
bin
|
||||
dist
|
||||
test
|
||||
docs
|
||||
deployments
|
||||
scripts
|
||||
*.md
|
||||
docker-compose*.yaml
|
||||
Makefile
|
||||
@@ -0,0 +1,53 @@
|
||||
# go-sms CI(Gitea Actions,语法兼容 GitHub Actions)(Day 4)
|
||||
# 与 .gitlab-ci.yml 同语义:test → build → deploy-dev。
|
||||
# 需 Gitea 服务端启用 Actions 并注册 Runner;未启用时本文件不会执行,不影响仓库。
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
SERVICE: gateway
|
||||
GOPROXY: https://goproxy.cn,direct
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.25"
|
||||
- name: Vet & Test
|
||||
run: |
|
||||
go vet ./...
|
||||
go test -race -cover ./...
|
||||
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build image
|
||||
run: |
|
||||
SHA=${GITHUB_SHA::7}
|
||||
docker build --build-arg SERVICE=${SERVICE} --build-arg VERSION=${SHA} \
|
||||
--build-arg GOPROXY=${GOPROXY} -t go-sms-${SERVICE}:${SHA} .
|
||||
# 推送镜像仓库(配置 secrets 后取消注释)
|
||||
# - name: Push image
|
||||
# run: |
|
||||
# echo "${{ secrets.REGISTRY_PASS }}" | docker login ${{ secrets.REGISTRY }} -u ${{ secrets.REGISTRY_USER }} --password-stdin
|
||||
# docker push ${{ secrets.REGISTRY }}/go-sms/go-sms-${SERVICE}:${GITHUB_SHA::7}
|
||||
|
||||
deploy-dev:
|
||||
runs-on: [self-hosted, dev-kind] # 需要 kind Dev 集群所在的 self-hosted Runner
|
||||
needs: build
|
||||
steps:
|
||||
- name: Load & Rollout
|
||||
run: |
|
||||
SHA=${GITHUB_SHA::7}
|
||||
kind load docker-image go-sms-${SERVICE}:${SHA} --name go-sms-dev
|
||||
kubectl -n go-sms-dev set image deployment/go-sms-${SERVICE} ${SERVICE}=go-sms-${SERVICE}:${SHA}
|
||||
kubectl -n go-sms-dev rollout status deployment/go-sms-${SERVICE} --timeout=180s
|
||||
@@ -0,0 +1,70 @@
|
||||
# ============================================================
|
||||
# go-sms CI/CD 流水线(Day 4)
|
||||
# Stage:test → build → deploy(dev 自动,prod 手动)
|
||||
# 说明:当前托管平台为 Gitea,同语义流水线见 .gitea/workflows/ci.yml;
|
||||
# 本文件为标准 GitLab 产物,迁移 GitLab 时直接可用。
|
||||
# ============================================================
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
- deploy
|
||||
|
||||
variables:
|
||||
SERVICE: gateway
|
||||
# GitLab 内置变量回退(本地 act 模拟时使用)
|
||||
CI_COMMIT_SHORT_SHA: ${CI_COMMIT_SHORT_SHA:-dev}
|
||||
IMAGE_NAME: go-sms-${SERVICE}
|
||||
GOPROXY: https://goproxy.cn,direct
|
||||
|
||||
# ---------- Stage 1:测试(每次 push)----------
|
||||
test:
|
||||
stage: test
|
||||
image: golang:1.25-alpine
|
||||
variables:
|
||||
CGO_ENABLED: "0"
|
||||
script:
|
||||
- go vet ./...
|
||||
- go test -race -cover ./...
|
||||
|
||||
# ---------- Stage 2:构建镜像(测试通过后)----------
|
||||
build:
|
||||
stage: build
|
||||
image: docker:27
|
||||
services:
|
||||
- docker:27-dind
|
||||
script:
|
||||
- docker build
|
||||
--build-arg SERVICE=${SERVICE}
|
||||
--build-arg VERSION=${CI_COMMIT_SHORT_SHA}
|
||||
--build-arg GOPROXY=${GOPROXY}
|
||||
-t ${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA} .
|
||||
# 推送镜像仓库(启用后取消注释)
|
||||
# - docker login -u $REGISTRY_USER -p $REGISTRY_PASS $REGISTRY
|
||||
# - docker tag ${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA} $REGISTRY/go-sms/${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
|
||||
# - docker push $REGISTRY/go-sms/${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
|
||||
|
||||
# ---------- Stage 3:部署 Dev(kind/K8s)----------
|
||||
deploy-dev:
|
||||
stage: deploy
|
||||
image: bitnami/kubectl:1.30
|
||||
script:
|
||||
- kubectl -n go-sms-dev set image deployment/go-sms-${SERVICE}
|
||||
${SERVICE}=${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
|
||||
- kubectl -n go-sms-dev rollout status deployment/go-sms-${SERVICE} --timeout=180s
|
||||
environment:
|
||||
name: dev
|
||||
url: http://127.0.0.1:30080/health
|
||||
|
||||
# ---------- 生产部署(手动,阶段 7 灰度上线时启用)----------
|
||||
deploy-prod:
|
||||
stage: deploy
|
||||
image: bitnami/kubectl:1.30
|
||||
script:
|
||||
- kubectl -n go-sms-prod set image deployment/go-sms-${SERVICE}
|
||||
${SERVICE}=${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
|
||||
- kubectl -n go-sms-prod rollout status deployment/go-sms-${SERVICE} --timeout=300s
|
||||
environment:
|
||||
name: prod
|
||||
when: manual
|
||||
only:
|
||||
- main
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
# ============================================================
|
||||
# 构建阶段:编译指定服务二进制(默认 gateway)
|
||||
# 用法:docker build --build-arg SERVICE=gateway -t go-sms-gateway:dev .
|
||||
# ============================================================
|
||||
FROM golang:1.25-alpine AS builder
|
||||
ARG SERVICE=gateway
|
||||
ARG VERSION=dev
|
||||
# GOPROXY 构建时可覆盖:make docker-build GOPROXY=https://goproxy.cn,direct
|
||||
ARG GOPROXY=https://proxy.golang.org,direct
|
||||
WORKDIR /app
|
||||
ENV CGO_ENABLED=0 GOOS=linux GOPROXY=${GOPROXY}
|
||||
COPY go.mod go.sum ./
|
||||
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
||||
COPY . .
|
||||
RUN --mount=type=cache,target=/go/pkg/mod,ro \
|
||||
--mount=type=cache,target=/root/.cache/go-build \
|
||||
go build -trimpath \
|
||||
-ldflags="-w -s -X main.version=${VERSION}" \
|
||||
-o /out/go-sms ./cmd/${SERVICE}
|
||||
|
||||
# ============================================================
|
||||
# 运行阶段:最小化镜像(非 root、上海时区、CA 证书)
|
||||
# ============================================================
|
||||
FROM alpine:3.20
|
||||
RUN apk add --no-cache tzdata ca-certificates \
|
||||
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
|
||||
&& echo "Asia/Shanghai" > /etc/timezone \
|
||||
&& addgroup -S go-sms && adduser -S go-sms -G go-sms -h /home/go-sms \
|
||||
&& mkdir -p /var/log/go-sms && chown -R go-sms:go-sms /var/log/go-sms
|
||||
USER go-sms
|
||||
WORKDIR /home/go-sms
|
||||
COPY --from=builder --chown=go-sms:go-sms /out/go-sms /usr/local/bin/go-sms
|
||||
COPY --chown=go-sms:go-sms configs/ /etc/go-sms/
|
||||
EXPOSE 8080 7890
|
||||
ENTRYPOINT ["go-sms", "-config", "/etc/go-sms/config.dev.yaml"]
|
||||
@@ -0,0 +1,190 @@
|
||||
# ============================================================
|
||||
# go-sms(Go-sms)开发工具集(Day 1)
|
||||
# 常用:make up | make down | make logs | make test | make build
|
||||
# ============================================================
|
||||
COMPOSE_FILE := docker-compose.dev.yaml
|
||||
COMPOSE := docker compose -f $(COMPOSE_FILE)
|
||||
|
||||
GO ?= go
|
||||
SERVICE ?= gateway
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
|
||||
GOPROXY ?= $(shell $(GO) env GOPROXY 2>/dev/null || echo https://proxy.golang.org,direct)
|
||||
|
||||
.DEFAULT_GOAL := help
|
||||
.PHONY: help up wait-healthy down restart logs ps tidy build test vet fmt lint config docker-build run run-consumer dev dev-stop
|
||||
|
||||
help: ## 显示帮助信息
|
||||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-16s\033[0m %s\n", $$1, $$2}'
|
||||
|
||||
up: ## 启动全部开发中间件容器并等待 healthy
|
||||
$(COMPOSE) up -d
|
||||
@$(MAKE) --no-print-directory wait-healthy
|
||||
|
||||
wait-healthy: ## 等待全部容器健康检查通过(最长 3 分钟;仅统计定义了 healthcheck 的容器,prometheus/alertmanager 无则不阻塞)
|
||||
@echo ">> 等待所有服务通过健康检查..."
|
||||
@ok=0; \
|
||||
for i in $$(seq 1 90); do \
|
||||
total=$$($(COMPOSE) ps -a --format json 2>/dev/null | grep -c '"Health":"[a-z]' || true); \
|
||||
healthy=$$($(COMPOSE) ps -a --format json 2>/dev/null | grep -c '"Health":"healthy"' || true); \
|
||||
if [ "$$total" -gt 0 ] && [ "$$healthy" -eq "$$total" ]; then ok=1; break; fi; \
|
||||
sleep 2; \
|
||||
done; \
|
||||
if [ "$$ok" -ne 1 ]; then \
|
||||
echo "!! 超时:部分容器未达到 healthy,请执行 make logs 排查"; \
|
||||
$(COMPOSE) ps; exit 1; \
|
||||
fi; \
|
||||
echo ">> 全部 $$healthy 个容器均已 healthy"
|
||||
|
||||
down: ## 停止并完全清理开发环境(含数据卷)
|
||||
$(COMPOSE) down -v --remove-orphans
|
||||
@echo ">> 开发环境已完全清理"
|
||||
|
||||
restart: ## 重启开发环境容器
|
||||
$(COMPOSE) restart
|
||||
@$(MAKE) --no-print-directory wait-healthy
|
||||
|
||||
logs: ## 跟踪查看全部容器日志(最近 100 行)
|
||||
$(COMPOSE) logs -f --tail=100
|
||||
|
||||
ps: ## 查看容器运行状态
|
||||
$(COMPOSE) ps
|
||||
|
||||
tidy: ## 整理 Go 依赖(go mod tidy)
|
||||
$(GO) mod tidy
|
||||
|
||||
build: ## 编译全部包(go build ./...)
|
||||
$(GO) build ./...
|
||||
|
||||
test: ## 运行单元测试(竞态检测 + 覆盖率)
|
||||
$(GO) test -race -cover ./...
|
||||
|
||||
vet: ## Go 官方静态检查
|
||||
$(GO) vet ./...
|
||||
|
||||
fmt: ## 格式化代码
|
||||
$(GO) fmt ./...
|
||||
|
||||
lint: ## golangci-lint v2 检查(安装: GOTOOLCHAIN=go1.25.0 go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1.6)
|
||||
golangci-lint run
|
||||
|
||||
config: ## 校验 docker-compose 配置语法
|
||||
$(COMPOSE) config -q && echo ">> docker-compose 配置语法正确"
|
||||
|
||||
run: ## 本地编译并运行网关(固定路径 bin/gateway,便于进程管理)
|
||||
@mkdir -p bin logs
|
||||
$(GO) build -o bin/gateway ./cmd/gateway
|
||||
GOSMS_LOG_FILE_PATH=$${GOSMS_LOG_FILE_PATH:-./logs/app.log} ./bin/gateway
|
||||
|
||||
run-consumer: ## 本地编译并运行 submit 消费者(Day48;bin/consumer,日志 logs/consumer.log)
|
||||
@mkdir -p bin logs
|
||||
$(GO) build -o bin/consumer ./cmd/consumer
|
||||
GOSMS_LOG_FILE_PATH=$${GOSMS_LOG_FILE_PATH:-./logs/consumer.log} ./bin/consumer
|
||||
|
||||
init-topics: ## 预创建四个核心 Kafka Topic(A-0.4)
|
||||
sh scripts/init_kafka_topics.sh
|
||||
|
||||
init-mysql: ## 初始化 MySQL 基础库(建库 gosms)
|
||||
docker exec -i go-sms-mysql mysql -uroot -proot123 < scripts/init_mysql.sql && \
|
||||
echo ">> MySQL gosms 库初始化完成"
|
||||
|
||||
init-schema: ## 执行 36 张表 DDL(幂等,IF NOT EXISTS)
|
||||
docker exec -i go-sms-mysql mysql -uroot -proot123 gosms < scripts/init_schema.sql && \
|
||||
echo ">> 36 张表 DDL 执行完成"
|
||||
|
||||
ci-local: ## 本地模拟 CI 流水线(test → build → deploy-dev,需 kind 集群 go-sms-dev)
|
||||
@echo ">> [1/3] test:go vet + go test"
|
||||
$(GO) vet ./... && $(GO) test -race -cover ./...
|
||||
@echo ">> [2/3] build:docker build(镜像 go-sms-$(SERVICE):$(VERSION))"
|
||||
docker build --build-arg SERVICE=$(SERVICE) --build-arg VERSION=$(VERSION) \
|
||||
--build-arg GOPROXY=$(GOPROXY) -t go-sms-$(SERVICE):$(VERSION) .
|
||||
@echo ">> [3/3] deploy-dev:kind load + kubectl rollout"
|
||||
kind load docker-image go-sms-$(SERVICE):$(VERSION) --name go-sms-dev
|
||||
kubectl -n go-sms-dev set image deployment/go-sms-$(SERVICE) $(SERVICE)=go-sms-$(SERVICE):$(VERSION)
|
||||
kubectl -n go-sms-dev rollout restart deployment/go-sms-$(SERVICE)
|
||||
kubectl -n go-sms-dev rollout status deployment/go-sms-$(SERVICE) --timeout=180s
|
||||
@echo ">> CI 流水线本地模拟完成:curl http://127.0.0.1:30080/health 验证"
|
||||
@echo ">> 注意:deployment.yaml 的 image 基线为 go-sms-\$$(SERVICE):dev;"
|
||||
@echo ">> 使用其他 VERSION 部署后,请勿直接 apply 覆盖(会回退基线镜像)"
|
||||
|
||||
docker-build: ## 构建服务镜像(默认 gateway,可 SERVICE=business 等)
|
||||
docker build --build-arg SERVICE=$(SERVICE) --build-arg VERSION=$(VERSION) \
|
||||
--build-arg GOPROXY=$(GOPROXY) -t go-sms-$(SERVICE):$(VERSION) .
|
||||
|
||||
# ---- Web 端线上测试(Day 36 增补;Mock 模式零依赖,无需后端/中间件) ----
|
||||
WEB_PORT ?= 5173
|
||||
|
||||
web-dev: ## 启动 Web 线上测试(Mock 模式,后台常驻;WEB_PORT=5173 可覆盖)
|
||||
@mkdir -p logs
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(WEB_PORT) "; then \
|
||||
echo ">> 端口 $(WEB_PORT) 已被占用(可能已在运行): make web-log 查看 / make web-stop 停止"; exit 1; fi
|
||||
@nohup ./web/node_modules/.bin/vite web --host 0.0.0.0 --port $(WEB_PORT) --strictPort > logs/web-dev.log 2>&1 &
|
||||
@ok=0; for i in $$(seq 1 30); do sleep 1; \
|
||||
curl -sf -o /dev/null http://127.0.0.1:$(WEB_PORT)/ && { ok=1; break; }; done; \
|
||||
if [ "$$ok" != 1 ]; then echo ">> 启动失败,查看 logs/web-dev.log"; exit 1; fi
|
||||
@ss -tlnp 2>/dev/null | grep ":$(WEB_PORT) " | grep -oP 'pid=\K[0-9]+' | head -1 > logs/web-dev.pid
|
||||
@echo ">> Web 线上测试已启动(Mock 模式,pid=$$(cat logs/web-dev.pid))"
|
||||
@echo ">> 本机: http://127.0.0.1:$(WEB_PORT)"
|
||||
@echo ">> 局域网: http://<服务器IP>:$(WEB_PORT) (Mock 登录: admin/admin123 或 customer/123456)"
|
||||
@echo ">> 日志: make web-log | 停止: make web-stop"
|
||||
|
||||
web-stop: ## 停止 Web 线上测试(端口占用兜底清理)
|
||||
@if [ -f logs/web-dev.pid ] && kill -0 $$(cat logs/web-dev.pid) 2>/dev/null; then \
|
||||
kill $$(cat logs/web-dev.pid); fi
|
||||
@sleep 1
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(WEB_PORT) "; then \
|
||||
pkill -f "vite web --host .*--port $(WEB_PORT)" && echo ">> 已停止(兜底 pkill)"; \
|
||||
else echo ">> 已停止"; fi
|
||||
@rm -f logs/web-dev.pid
|
||||
|
||||
web-log: ## 查看 Web 启动日志(尾部 30 行)
|
||||
@tail -n 30 logs/web-dev.log 2>/dev/null || echo ">> 无日志(尚未启动?)"
|
||||
|
||||
web-test: ## Web 质量门:lint + Vitest + build(与 L1 DoD 一致)
|
||||
cd web && ./node_modules/.bin/eslint . --ext .ts,.vue --max-warnings 0 \
|
||||
&& ./node_modules/.bin/vitest run && npm run build
|
||||
|
||||
# ---- 真实模式联调(Day 31:网关 :8081 + Web real;首启先 make seed-admin) ----
|
||||
GW_PORT ?= 8081
|
||||
|
||||
seed-admin: ## 首启引导:迁移 + 内置超管 admin(密码仅打印一次;已有账号则跳过)
|
||||
go run ./cmd/migrate -config configs/config.dev.yaml seed-admin
|
||||
|
||||
gw-start: ## 启动网关(后台,:8081;8080 被宿主旧进程占用故用 8081,可用 GW_PORT 覆盖)
|
||||
@mkdir -p logs
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(GW_PORT) "; then echo ">> 网关已在 :$(GW_PORT) 运行"; exit 0; fi
|
||||
@go build -o bin/gateway ./cmd/gateway
|
||||
@nohup env GOSMS_LOG_FILE_PATH=./logs/app.log GOSMS_SERVER_HTTP_ADDR=:$(GW_PORT) ./bin/gateway > logs/gateway-run.log 2>&1 & echo $! > logs/gateway.pid
|
||||
@sleep 2
|
||||
@curl -sf http://127.0.0.1:$(GW_PORT)/health || { echo ">> 启动失败,查看 logs/gateway-run.log"; exit 1; }
|
||||
@echo ""
|
||||
@echo ">> 网关已启动(:$(GW_PORT),pid=$$(cat logs/gateway.pid))"
|
||||
|
||||
gw-stop: ## 停止网关
|
||||
@if [ -f logs/gateway.pid ] && kill -0 $$(cat logs/gateway.pid) 2>/dev/null; then \
|
||||
kill $$(cat logs/gateway.pid); echo ">> 已停止"; else echo ">> 未在运行"; fi
|
||||
|
||||
web-real: ## 启动 Web 真实模式(依赖 gw-start;vite proxy 已指向 :8081)
|
||||
@$(MAKE) gw-start
|
||||
@if [ ! -f web/.env.development.local ]; then \
|
||||
echo 'VITE_API_MODE=real' > web/.env.development.local; \
|
||||
echo ">> 已创建 web/.env.development.local(不入库)"; fi
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(WEB_PORT) "; then echo ">> Web 已在 :$(WEB_PORT) 运行"; \
|
||||
else $(MAKE) web-dev; fi
|
||||
@echo ">> 真实模式就绪: http://<服务器IP>:$(WEB_PORT) (登录: make seed-admin 打印的 admin 密码)"
|
||||
|
||||
svc-status: ## 三服务健康一览(网关/Web/MySQL)
|
||||
@printf '%-12s' 'gateway:'; curl -sf -m 2 http://127.0.0.1:$(GW_PORT)/health > /dev/null \
|
||||
&& echo "OK (:$(GW_PORT))" || echo "DOWN"
|
||||
@printf '%-12s' 'web(5173):'; curl -sf -m 2 -o /dev/null http://127.0.0.1:$(WEB_PORT)/ \
|
||||
&& echo "OK" || echo "DOWN"
|
||||
@printf '%-12s' 'mysql:'; docker exec go-sms-mysql mysql -uroot -proot123 -e 'SELECT 1' > /dev/null 2>&1 \
|
||||
&& echo "OK" || echo "DOWN"
|
||||
|
||||
# ---- 一键开发环境(scripts/dev_up.sh:中间件→DB→seed-admin→网关→Web,全程幂等) ----
|
||||
|
||||
dev: ## 一键启动前后端(详见 scripts/dev_up.sh;API_MODE/GW_PORT/WEB_PORT/WITH_CONSUMER/SKIP_INIT 可覆盖)
|
||||
@bash scripts/dev_up.sh
|
||||
|
||||
dev-stop: ## 一键停止前后端(Web+网关+consumer;中间件不动)
|
||||
@bash scripts/dev_down.sh
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# 压测产物不进库:备份含密钥哈希/报告与日志已在 day50.md 归档摘要
|
||||
*.sql
|
||||
*.log
|
||||
*.json
|
||||
tmp/
|
||||
@@ -0,0 +1,290 @@
|
||||
// _bench/loadtest.go Day50:入口压测工具(go run ./_bench/loadtest.go ...)。
|
||||
// 模拟真实客户批量提交:sendMessageMass,N worker×1rps×每请求 phones 条号码;
|
||||
// 号池全异号单调递增(13800000000+n),统计 P50/P95/P99/P999/成功率/429/错误分布,
|
||||
// 每 10s 进度快照,终局输出 JSON 行报告(供 day50.md 归档)。
|
||||
// Day52 内容口径更正:默认随机模式=【-sign 签名】+随机中文正文的长短信,计费条数
|
||||
// 按号随机落 -segs 区间(默认 1-9);计费口径锁 util.SmsLength(UCS2 ≤70=1 条,>70 按 67/段),
|
||||
// 启动预热自校验防口径漂移;新增 billed_total/segs_dist/billed_tps 供下发侧负载与对账。
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/protocol/cmpp"
|
||||
"go-sms/internal/util"
|
||||
)
|
||||
|
||||
// hanziPool 随机正文字池(常用汉字;随机单字长串不构成整词,policy_keyword 按词匹配不命中)。
|
||||
const hanziPool = "的一是了我不人在他有这上们来到时大地为子中你说生国年着就那和要她出也得里后自以会家可下而过天去能对小多然于心学么之都好看起发当没成只如事把还用第样道想作种开美总从无情己面最女但现前些所同日手又行意动方期它头经长儿回位分爱老因很给名法间斯知世什两次使身者被高已亲其进此话常与活正感见明问力理尔点文几定本公特做外孩相西果走将月十实向声车全信重三机工物气每并别真打太新比才便夫再书部水像眼等体却加电主界门利海受听表德少克代员许稜先口由死安写性马光白或住难望教命花结乐色更拉东神记处让母父应直字场平报友关放至张认接告入笑内英军候民岁往何度山觉路带万男边风解叫任金快原吃妈变通师立象数四失满战远格士音轻目条呢病始达深完今提求清王化空业思切怎非找片罗钱吗语元喜曾离飞科言干流欢约各即指合反题必该论交终林请医晚制球决传画保读运及则房早院量苦火布品近坐产答星精视五连司巴奇管类未朋且婚台夜青北队久乎越观落尽形影红爸百令周吧识步希亚术留市半热送兴造谈容极随演收首根讲整式取照办强石古华拿计您装似足双妻尼转诉米称丽客南领节衣站黑刻统断福城故历惊脸选包紧争另建维绝树系伤示愿持千史谁准联妇纪基买志静阿诗独复痛消社算义竟确酒需单治卡幸兰念举仅钟怕共毛句息功官待究跟穿室易游程号居考突皮哪费倒价图具刚脑永歌响商礼细专黄块脚味灵改据般破引食仍存众注笔甚某沉血备习校默务土微娘须试怀料调广蜖苏显赛查密议底列富梦错座参八除跑亮假印设线温虽掉京初养香停际致阳纸李纳验助激够严证帝饭忘趣支春集丈木研班普导顿睡展跳获艺六波察群皇段急庭创区奥器谢弟店否害草排背止组州朝封睛板角况曲馆育忙质河续哥呼若推境遇雨标姐充围案伦护冷警贝著雪索剧啊船险烟依斗值帮汉慢佛肯闻唱沙局固甚员"
|
||||
|
||||
type apiResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
MsgID int64 `json:"msgId"`
|
||||
}
|
||||
|
||||
type report struct {
|
||||
URL string `json:"url"`
|
||||
Workers int `json:"workers"`
|
||||
PhonesPerReq int `json:"phones_per_req"`
|
||||
Duration string `json:"duration"`
|
||||
ReqTotal int64 `json:"req_total"`
|
||||
ReqOK int64 `json:"req_ok"` // HTTP 200 且 code=0
|
||||
ReqHTTPFail int64 `json:"req_http_fail"`
|
||||
Req429 int64 `json:"req_429"` // 限流超限(全局/账号桶)
|
||||
ReqBizFail int64 `json:"req_biz_fail"` // HTTP 200 但 code!=0
|
||||
SmsTotal int64 `json:"sms_total"` // 成功受理条数(号数)
|
||||
Sms429 int64 `json:"sms_429"` // 429 请求折算条数
|
||||
BilledTotal int64 `json:"billed_total"` // 计费条数累计(util.SmsLength 口径,=Σ号数×该内容条数)
|
||||
SegsDist map[string]int `json:"segs_dist"` // 各计费档号数分布(1..9)
|
||||
Sign string `json:"sign"` // 随机模式内容签名
|
||||
SegsRange string `json:"segs_range"` // 随机计费条数区间(fixed=固定内容模式)
|
||||
BilledTPS float64 `json:"billed_tps"` // 计费条/秒(下发侧负载参考)
|
||||
Errors map[string]int `json:"errors"`
|
||||
P50ms float64 `json:"p50_ms"`
|
||||
P95ms float64 `json:"p95_ms"`
|
||||
P99ms float64 `json:"p99_ms"`
|
||||
P999ms float64 `json:"p999_ms"`
|
||||
Maxms float64 `json:"max_ms"`
|
||||
RecvTPS float64 `json:"recv_tps"` // 实际受理 TPS
|
||||
AskTPS float64 `json:"ask_tps"` // 目标 TPS
|
||||
Elapsed string `json:"elapsed"`
|
||||
}
|
||||
|
||||
func pct(lat []float64, p float64) float64 {
|
||||
if len(lat) == 0 {
|
||||
return 0
|
||||
}
|
||||
idx := int(float64(len(lat)-1)*p + 0.5)
|
||||
if idx >= len(lat) {
|
||||
idx = len(lat) - 1
|
||||
}
|
||||
return lat[idx]
|
||||
}
|
||||
|
||||
// genContent 生成「【签名】+随机中文正文」内容,计费条数命中 segN(口径 util.SmsLength:
|
||||
// UCS2 ≤70 字=1 条,>70 按 67 字/段):n=1 ⇒ 总字数∈[签名6+1,70];n=2 ⇒ ∈[71,134](68~70 被
|
||||
// 「≤70=1 条」分支占用);n≥3 ⇒ ∈[(n-1)*67+1, n*67]。
|
||||
func genContent(sign string, segN int) string {
|
||||
const signLen = 6 // 【x x x x】
|
||||
lo, hi := signLen+1, 70
|
||||
if segN > 1 {
|
||||
lo, hi = (segN-1)*67+1, segN*67
|
||||
if lo <= 70 {
|
||||
lo = 71
|
||||
}
|
||||
}
|
||||
total := lo + rand.Intn(hi-lo+1)
|
||||
pool := []rune(hanziPool) // rune 索引(Day52 修复:原字节索引把汉字拆成无效码点,GBK 编码失败→下发全灭)
|
||||
body := make([]rune, total-signLen)
|
||||
for i := range body {
|
||||
body[i] = pool[rand.Intn(len(pool))]
|
||||
}
|
||||
c := "【" + sign + "】" + string(body)
|
||||
if _, segs := util.SmsLength(c); segs != segN {
|
||||
// 口径漂移防御:util.SmsLength 规则变更时立刻暴露,避免产出错误计费数据。
|
||||
fmt.Fprintf(os.Stderr, "genContent 口径不符: 目标 %d 条,util.SmsLength=%d (len=%d)\n", segN, segs, len([]rune(c)))
|
||||
os.Exit(3)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
url := flag.String("url", "http://127.0.0.1:8080/sms/api/sendMessageMass", "目标地址")
|
||||
user := flag.String("user", "test", "账号名")
|
||||
secret := flag.String("secret", "", "明文密钥(库中存其 MD5)")
|
||||
workers := flag.Int("workers", 50, "并发 worker 数(每 worker 1 rps)")
|
||||
phones := flag.Int("phones", 100, "每请求号码数")
|
||||
dur := flag.Duration("dur", 300*time.Second, "压测时长")
|
||||
base := flag.Int64("base", 13800000000, "号池基数,单调递增")
|
||||
content := flag.String("content", "", "固定短信内容(空=随机模式:【-sign】+中文长短信,-segs 条数随机;Day52 口径)")
|
||||
signName := flag.String("sign", "盈讯网络", "随机模式内容签名(报备口径)")
|
||||
segsRange := flag.String("segs", "1-9", "随机模式计费条数区间 min-max(1≤min≤max≤9)")
|
||||
out := flag.String("out", "", "报告 JSON 输出文件")
|
||||
flag.Parse()
|
||||
if *secret == "" {
|
||||
fmt.Fprintln(os.Stderr, "须提供 -secret")
|
||||
os.Exit(2)
|
||||
}
|
||||
segMin, segMax := 1, 9
|
||||
if _, err := fmt.Sscanf(*segsRange, "%d-%d", &segMin, &segMax); err != nil || segMin < 1 || segMax < segMin || segMax > 9 {
|
||||
fmt.Fprintf(os.Stderr, "-segs 须为 min-max 且 1≤min≤max≤9,当前 %q\n", *segsRange)
|
||||
os.Exit(2)
|
||||
}
|
||||
randomMode := *content == ""
|
||||
if randomMode {
|
||||
// 预热自校验:1000 条生成+计费口径+GBK 可编码核对(不匹配即退出,防口径漂移产出脏数据;
|
||||
// GBK 校验防字节索引类 bug——无效码点下发期 SplitContent 才爆,预热期即拦)。
|
||||
dist := map[int]int{}
|
||||
gbkOK := 0
|
||||
for i := 0; i < 1000; i++ {
|
||||
n := segMin + rand.Intn(segMax-segMin+1)
|
||||
c := genContent(*signName, n)
|
||||
if _, _, err := cmpp.EncodeContent(c); err == nil {
|
||||
gbkOK++
|
||||
}
|
||||
dist[n]++
|
||||
}
|
||||
if gbkOK != 1000 {
|
||||
fmt.Fprintf(os.Stderr, "precheck: GBK 可编码校验失败 %d/1000\n", gbkOK)
|
||||
os.Exit(3)
|
||||
}
|
||||
fmt.Printf("[precheck] 随机模式就绪:1000 条内容生成+SmsLength 口径+GBK 编码校验通过,dist=%v\n", dist)
|
||||
}
|
||||
secretMD5 := fmt.Sprintf("%x", md5.Sum([]byte(*secret)))
|
||||
|
||||
tr := &http.Transport{
|
||||
MaxIdleConns: 128,
|
||||
MaxIdleConnsPerHost: 128,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
}
|
||||
client := &http.Client{Timeout: 10 * time.Second, Transport: tr}
|
||||
|
||||
var seq atomic.Int64
|
||||
var mu sync.Mutex
|
||||
rep := &report{
|
||||
URL: *url, Workers: *workers, PhonesPerReq: *phones,
|
||||
Duration: dur.String(), Errors: map[string]int{}, SegsDist: map[string]int{},
|
||||
Sign: *signName, SegsRange: *segsRange,
|
||||
}
|
||||
if !randomMode {
|
||||
rep.SegsRange = "fixed(content)"
|
||||
}
|
||||
var lat []float64
|
||||
start := time.Now()
|
||||
deadline := start.Add(*dur)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < *workers; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
tk := time.NewTicker(time.Second)
|
||||
defer tk.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-tk.C:
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return
|
||||
}
|
||||
// 构造全异号 phoneList(单调递增,零碰撞)。
|
||||
n := seq.Add(1) * int64(*phones)
|
||||
list := make([]string, *phones)
|
||||
for i := 0; i < *phones; i++ {
|
||||
list[i] = strconv.FormatInt(*base+n+int64(i), 10)
|
||||
}
|
||||
ts := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
apiSign := fmt.Sprintf("%x", md5.Sum([]byte(*user+ts+secretMD5)))
|
||||
// 内容:固定模式用 -content;随机模式每请求生成一条(批内 phones 号共享,同真实批量提交口径)。
|
||||
c := *content
|
||||
if randomMode {
|
||||
c = genContent(*signName, segMin+rand.Intn(segMax-segMin+1))
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"userName": *user, "timestamp": ts, "sign": apiSign,
|
||||
"phoneList": list, "content": c,
|
||||
})
|
||||
t0 := time.Now()
|
||||
resp, err := client.Post(*url, "application/json", bytes.NewReader(body))
|
||||
ms := float64(time.Since(t0).Microseconds()) / 1000.0
|
||||
mu.Lock()
|
||||
rep.ReqTotal++
|
||||
if err != nil {
|
||||
rep.ReqHTTPFail++
|
||||
rep.Errors["transport:"+err.Error()]++
|
||||
} else {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == 429 {
|
||||
rep.Req429++
|
||||
rep.Sms429 += int64(*phones)
|
||||
} else if resp.StatusCode != 200 {
|
||||
rep.ReqHTTPFail++
|
||||
rep.Errors["http:"+strconv.Itoa(resp.StatusCode)]++
|
||||
} else {
|
||||
var r apiResp
|
||||
_ = json.Unmarshal(raw, &r)
|
||||
if r.Code == 0 {
|
||||
rep.ReqOK++
|
||||
rep.SmsTotal += int64(*phones)
|
||||
_, segs := util.SmsLength(c)
|
||||
rep.BilledTotal += int64(*phones) * int64(segs)
|
||||
rep.SegsDist[strconv.Itoa(segs)] += *phones
|
||||
} else {
|
||||
rep.ReqBizFail++
|
||||
rep.Errors[fmt.Sprintf("code:%d:%s", r.Code, r.Message)]++
|
||||
}
|
||||
}
|
||||
}
|
||||
lat = append(lat, ms)
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 进度快照协程。
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
tk := time.NewTicker(10 * time.Second)
|
||||
defer tk.Stop()
|
||||
lastReq, lastSms, lastBilled := int64(0), int64(0), int64(0)
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-tk.C:
|
||||
mu.Lock()
|
||||
l := make([]float64, len(lat))
|
||||
copy(l, lat)
|
||||
curReq, curSms := rep.ReqTotal, rep.SmsTotal
|
||||
cur429, curBiz := rep.Req429, rep.ReqBizFail
|
||||
curBilled := rep.BilledTotal
|
||||
mu.Unlock()
|
||||
el := time.Since(start).Seconds()
|
||||
fmt.Printf("[snapshot] t=%.0fs req=%d(req/s=%.1f) sms_ok=%d(TPS=%.0f) billed=%d(条/s=%.0f) p99=%.1fms 429=%d bizfail=%d\n",
|
||||
el, curReq, float64(curReq-lastReq)/10.0, curSms, float64(curSms-lastSms)/10.0,
|
||||
curBilled, float64(curBilled-lastBilled)/10.0, pct(l, 0.99), cur429, curBiz)
|
||||
lastReq, lastSms, lastBilled = curReq, curSms, curBilled
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
close(done)
|
||||
|
||||
mu.Lock()
|
||||
sorted := make([]float64, len(lat))
|
||||
copy(sorted, lat)
|
||||
mu.Unlock()
|
||||
sort.Float64s(sorted)
|
||||
rep.P50ms, rep.P95ms, rep.P99ms, rep.P999ms = pct(sorted, 0.50), pct(sorted, 0.95), pct(sorted, 0.99), pct(sorted, 0.999)
|
||||
if len(sorted) > 0 {
|
||||
rep.Maxms = sorted[len(sorted)-1]
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
rep.Elapsed = elapsed.Truncate(time.Millisecond).String()
|
||||
rep.RecvTPS = float64(rep.SmsTotal) / elapsed.Seconds()
|
||||
rep.AskTPS = float64(*workers) * float64(*phones)
|
||||
rep.BilledTPS = float64(rep.BilledTotal) / elapsed.Seconds()
|
||||
|
||||
raw, _ := json.Marshal(rep)
|
||||
fmt.Println("==== FINAL ====")
|
||||
fmt.Println(string(raw))
|
||||
if *out != "" {
|
||||
_ = os.WriteFile(*out, raw, 0644)
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# Day50 阶梯加压:500/2000/5000 TPS 各 60s,号段隔离,报告落 _bench/
|
||||
S=$(cat /tmp/day50_secret)
|
||||
run(){ # $1=workers $2=phones $3=tag
|
||||
go run ./_bench/loadtest.go -secret $S -workers $1 -phones $2 -dur 60s -base $3 -out _bench/stair-$3.json > _bench/stair-$3.log 2>&1
|
||||
tail -1 _bench/stair-$3.log
|
||||
}
|
||||
run 5 100 13910000000
|
||||
run 20 100 13920000000
|
||||
run 50 100 13930000000
|
||||
echo STAIRS_DONE
|
||||
@@ -0,0 +1,825 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: go-sms API v1(阶段 2:账号与权限)
|
||||
version: 1.2.0
|
||||
description: |
|
||||
阶段 2(Day 21-30)交付的账号与权限域全部端点。
|
||||
统一响应 {code,msg,data}:code=ISC:000 成功;错误码三位制(ISC:4xx/5xx)。
|
||||
认证:Authorization Bearer JWT(除 /auth/login);敏感路由另挂 Casbin 细粒度门禁。
|
||||
servers:
|
||||
- url: /api/v1
|
||||
security:
|
||||
- bearerAuth: []
|
||||
tags:
|
||||
- {name: auth, description: 认证(Day 24)}
|
||||
- {name: accounts, description: 账号 CRUD 与配置(Day 21-23)}
|
||||
- {name: hierarchy, description: 上级切换/标星/团队/实名(Day 28)}
|
||||
- {name: recharge, description: 充值与余额(Day 26)}
|
||||
- {name: warning, description: 余额预警(Day 27)}
|
||||
- {name: permissions, description: 二级权限与导出(Day 25)}
|
||||
- {name: audit, description: 审计查询(Day 29)}
|
||||
- {name: channel, description: 通道 CRUD/测试工具/拦截配置(Day 31-36)}
|
||||
paths:
|
||||
/auth/login:
|
||||
post:
|
||||
tags: [auth]
|
||||
summary: 登录(公开;5 次失败锁 15 分钟)
|
||||
security: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [username, password]
|
||||
properties:
|
||||
username: {type: string}
|
||||
password: {type: string, minLength: 6}
|
||||
responses:
|
||||
"200": {description: "data: token/expires_in/username/role + user{account_id,username,roles[],permissions[]}(Day 31 前端契约)"}
|
||||
"401": {description: 用户名或密码错误}
|
||||
"429": {description: 连续错误锁定}
|
||||
/auth/logout:
|
||||
post: {tags: [auth], summary: 登出(令牌入黑名单), responses: {"200": {description: ok}}}
|
||||
/auth/refresh:
|
||||
post: {tags: [auth], summary: 刷新令牌(轮换,旧令牌失效), responses: {"200": {description: 新 token}}}
|
||||
/accounts:
|
||||
post:
|
||||
tags: [accounts]
|
||||
summary: 开户(业务位掩码+bcrypt+事务;自动绑定 Casbin 角色)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: "#/components/schemas/CreateAccount"}
|
||||
responses:
|
||||
"200": {description: 账号已创建}
|
||||
"400": {description: 参数/角色/掩码非法}
|
||||
get:
|
||||
tags: [accounts]
|
||||
summary: 分页列表(标星置顶;username/protocol/status 过滤)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: username, in: query, schema: {type: string}}
|
||||
- {name: status, in: query, schema: {type: integer}}
|
||||
- {name: protocol, in: query, schema: {type: string}}
|
||||
- {name: starred, in: query, schema: {type: boolean}}
|
||||
responses:
|
||||
"200": {description: 分页结果}
|
||||
/accounts/{id}:
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer, format: int64}}]
|
||||
get: {tags: [accounts], summary: 详情(账号+业务配置), responses: {"200": {description: ok}, "404": {description: 不存在}}}
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 部分更新(real_name/role/parent_id/svc_team/remark;改角色自动重绑 Casbin)
|
||||
requestBody: {content: {application/json: {schema: {type: object}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
delete: {tags: [accounts], summary: 删除(守卫:有发送记录拒删;同步解绑 Casbin), responses: {"200": {description: ok}, "429": {description: 有发送记录不可删}}}
|
||||
/accounts/{id}/status:
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 启用/禁用(禁用即存量 Token 失效)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {status: {type: integer, enum: [0, 1]}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/{id}/ip-bindings:
|
||||
get: {tags: [accounts], summary: 查询 IP 白名单, parameters: [{name: id, in: path, required: true, schema: {type: integer}}], responses: {"200": {description: ips 数组}}}
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 设置 IP 白名单(JSON 数组/CIDR)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [ips], properties: {ips: {type: array, items: {type: string}}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/{id}/protocol:
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 设置接入协议(A-2.1 四协议 http/cmpp/smpp/web)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [business, protocol], properties: {business: {type: string}, protocol: {type: string, enum: [http, cmpp, smpp, web]}}}}}}
|
||||
responses: {"200": {description: ok}, "404": {description: 业务线不存在}}
|
||||
/accounts/{id}/audit-mode:
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 审核模式(0 免审/1 手动/2 自动拒绝)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [business, mode], properties: {business: {type: string}, mode: {type: integer, enum: [0, 1, 2]}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/{id}/business/{biz}/enable:
|
||||
post: {tags: [accounts], summary: 开通业务线, parameters: [{name: id, in: path, required: true, schema: {type: integer}}, {name: biz, in: path, required: true, schema: {type: string}}], responses: {"200": {description: ok}}}
|
||||
/accounts/{id}/business/{biz}/disable:
|
||||
post: {tags: [accounts], summary: 关闭业务线(硬删配置行), parameters: [{name: id, in: path, required: true, schema: {type: integer}}, {name: biz, in: path, required: true, schema: {type: string}}], responses: {"200": {description: ok}}}
|
||||
/accounts/{id}/business-status:
|
||||
get: {tags: [accounts], summary: 业务线开关状态, parameters: [{name: id, in: path, required: true, schema: {type: integer}}], responses: {"200": {description: "map[business]bool"}}}
|
||||
/accounts/{id}/parent:
|
||||
post:
|
||||
tags: [hierarchy]
|
||||
summary: 切换上级(物化路径防环+子孙级联重写;account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [parent_id], properties: {parent_id: {type: integer}}}}}}
|
||||
responses: {"200": {description: ok}, "400": {description: 成环}, "404": {description: 上级不存在}}
|
||||
/accounts/{id}/star:
|
||||
post:
|
||||
tags: [hierarchy]
|
||||
summary: 标星/取消(列表置顶;account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {starred: {type: boolean}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/{id}/svc-team:
|
||||
put:
|
||||
tags: [hierarchy]
|
||||
summary: 绑定服务团队(空串清除;account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {svc_team: {type: string, maxLength: 64}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/{id}/realname:
|
||||
get:
|
||||
tags: [hierarchy]
|
||||
summary: 实名信息(证件号脱敏;本人或 admin)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: real_name/id_card(脱敏)/status}}
|
||||
post:
|
||||
tags: [hierarchy]
|
||||
summary: 提交实名资料(状态置 0 待审;已通过不可重提)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [real_name, id_card], properties: {real_name: {type: string}, id_card: {type: string, minLength: 15}}}}}}
|
||||
responses: {"200": {description: ok}, "400": {description: 已通过/参数非法}}
|
||||
/accounts/{id}/realname/review:
|
||||
post:
|
||||
tags: [hierarchy]
|
||||
summary: 实名审核(仅待审态;account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {approve: {type: boolean}, reason: {type: string}}}}}}
|
||||
responses: {"200": {description: status=1 通过/2 驳回}, "400": {description: 非待审态}}
|
||||
/accounts/{id}/balance:
|
||||
get:
|
||||
tags: [recharge]
|
||||
summary: 余额查询(余额/授信+近 20 条流水;非 admin 仅本人)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}, "403": {description: 越权}, "404": {description: 不存在}}
|
||||
/accounts/{id}/recharge:
|
||||
post:
|
||||
tags: [recharge]
|
||||
summary: 单笔充值(事务:余额自增+流水;account:recharge 二级权限)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [amount], properties: {amount: {type: integer, minimum: 1, maximum: 10000000000}, remark: {type: string}}}}}}
|
||||
responses: {"200": {description: 返回新余额与流水号}, "403": {description: 无 recharge 权限}}
|
||||
/accounts/recharge/batch:
|
||||
post:
|
||||
tags: [recharge]
|
||||
summary: CSV 批量充值(username,amount[,remark];multipart file 或原文;行级结果)
|
||||
requestBody:
|
||||
content:
|
||||
text/csv: {schema: {type: string}}
|
||||
multipart/form-data: {schema: {type: object, properties: {file: {type: string, format: binary}}}}
|
||||
responses:
|
||||
"200": {description: total/success/failed/rows(行号+原因)}
|
||||
"400": {description: 空 CSV/超 1000 行/坏格式}
|
||||
/accounts/{id}/warning:
|
||||
get:
|
||||
tags: [warning]
|
||||
summary: 预警配置(本人或 admin)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: config(未配置为 null)}}
|
||||
put:
|
||||
tags: [warning]
|
||||
summary: 设置预警(阈值/冷却分钟/渠道/接收 webhook)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [threshold], properties: {threshold: {type: number, exclusiveMinimum: 0}, cooldown_min: {type: integer, maximum: 1440}, notify_type: {type: string, enum: [wechat, dingtalk]}, notify_to: {type: string}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
delete:
|
||||
tags: [warning]
|
||||
summary: 停用预警(保留参数)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}}
|
||||
/accounts/export:
|
||||
get:
|
||||
tags: [permissions]
|
||||
summary: 全量账号 CSV 导出(BOM;上限 1 万行;account:export 二级权限)
|
||||
responses:
|
||||
"200": {description: text/csv}
|
||||
"403": {description: 无 export 权限}
|
||||
/accounts/{id}/permissions:
|
||||
get:
|
||||
tags: [permissions]
|
||||
summary: 二级权限列表
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: permissions 数组}}
|
||||
/accounts/{id}/permissions/{perm}/grant:
|
||||
post:
|
||||
tags: [permissions]
|
||||
summary: 授予二级权限(recharge/proxy_send/export/send;account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}, {name: perm, in: path, required: true, schema: {type: string}}]
|
||||
responses: {"200": {description: ok}, "400": {description: 非法标记}}
|
||||
/accounts/{id}/permissions/{perm}/revoke:
|
||||
post:
|
||||
tags: [permissions]
|
||||
summary: 撤销二级权限(account:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}, {name: perm, in: path, required: true, schema: {type: string}}]
|
||||
responses: {"200": {description: ok}}
|
||||
/audit/logs:
|
||||
get:
|
||||
tags: [audit]
|
||||
summary: 操作日志查询(system:read;操作人/账号/模块/动作/IP/时间范围)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer}}
|
||||
- {name: page_size, in: query, schema: {type: integer}}
|
||||
- {name: operator, in: query, schema: {type: string}}
|
||||
- {name: account_id, in: query, schema: {type: integer}}
|
||||
- {name: module, in: query, schema: {type: string}}
|
||||
- {name: action, in: query, schema: {type: string}}
|
||||
- {name: ip, in: query, schema: {type: string}}
|
||||
- {name: start, in: query, schema: {type: string, example: "2026-09-01"}}
|
||||
- {name: end, in: query, schema: {type: string}}
|
||||
responses: {"200": {description: total+list}, "403": {description: 无 system:read}}
|
||||
/audit/logins:
|
||||
get:
|
||||
tags: [audit]
|
||||
summary: 登录审计查询(system:read;username/ip/only_failed/时间范围)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer}}
|
||||
- {name: username, in: query, schema: {type: string}}
|
||||
- {name: ip, in: query, schema: {type: string}}
|
||||
- {name: only_failed, in: query, schema: {type: string, enum: ["0", "1"]}}
|
||||
responses: {"200": {description: total+list}}
|
||||
/channels:
|
||||
post:
|
||||
tags: [channel]
|
||||
summary: 创建通道(channel:write;Day 31 扩展协议接入+运营属性 16 字段)
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [name, code, protocol, host]
|
||||
properties:
|
||||
name: {type: string, maxLength: 64}
|
||||
code: {type: string, description: 通道号/接入码,全局唯一}
|
||||
protocol: {type: string, enum: [cmpp, http, sgip, smpp]}
|
||||
host: {type: string}
|
||||
port: {type: integer, minimum: 1, maximum: 65535}
|
||||
username: {type: string}
|
||||
password: {type: string}
|
||||
sp_id: {type: string, maxLength: 6, description: 企业代码(CMPP SourceAddr ≤6B/SGIP CorpID)}
|
||||
protocol_version: {type: integer, description: "cmpp:20(3.0 未实现拒绝) sgip:12 smpp:34;缺省按协议填充"}
|
||||
heartbeat_interval: {type: integer, description: 心跳周期秒,0 取默认 30}
|
||||
reconnect_max: {type: integer, description: 重连退避上限秒,0 取默认 30}
|
||||
service_id: {type: string, description: 服务代码,空用 TEST}
|
||||
carrier: {type: string, description: "运营商逗号分隔: cmcc,cucc,ctcc"}
|
||||
billing_length: {type: integer, description: 计费长度字/条,0 取默认 70}
|
||||
payment_type: {type: string, enum: ["", prepay_monthly, postpaid_monthly, prepay_realtime]}
|
||||
billing_mode: {type: string, enum: ["", success_unknown, success, all]}
|
||||
price: {type: number, description: 通道价格元/条}
|
||||
category: {type: string}
|
||||
mo_forward_channel_id: {type: integer, description: 上行组合通道,0 不转发}
|
||||
number_rule: {type: integer, enum: [0, 1, 2, 3]}
|
||||
code_truncate: {type: integer, description: 通道号截断位数 0~20}
|
||||
ext_params: {type: string}
|
||||
service_remark: {type: string}
|
||||
responses: {"200": {description: 创建成功}, "400": {description: 校验失败(协议/sp_id≤6B/CMPP3.0/白名单)}}
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 通道列表(name/protocol/status 过滤,分页)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer}}
|
||||
- {name: page_size, in: query, schema: {type: integer}}
|
||||
- {name: name, in: query, schema: {type: string}}
|
||||
- {name: protocol, in: query, schema: {type: string}}
|
||||
- {name: status, in: query, schema: {type: integer, enum: [0, 1]}}
|
||||
responses: {"200": {description: total+list}}
|
||||
/channels/{id}:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 通道详情(含 Day 31 扩展字段)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 通道}, "404": {description: 不存在}}
|
||||
put:
|
||||
tags: [channel]
|
||||
summary: 更新通道(channel:write;白名单字段;code/protocol/status 不可改;sp_id≤6B/CMPP3.0 纵深防御)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object}}}}
|
||||
responses: {"200": {description: 更新成功}, "400": {description: 越权字段/校验失败}, "404": {description: 不存在}}
|
||||
delete:
|
||||
tags: [channel]
|
||||
summary: 删除通道(channel:write;须先停用且不被通道组引用)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 删除成功}, "400": {description: 启用中/被组引用}, "404": {description: 不存在}}
|
||||
/channels/{id}/status:
|
||||
put:
|
||||
tags: [channel]
|
||||
summary: 启停通道(channel:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [status], properties: {status: {type: integer, enum: [0, 1]}}}}}}
|
||||
responses: {"200": {description: 成功}, "400": {description: 状态非法}}
|
||||
/channels/{id}/test:
|
||||
post:
|
||||
tags: [channel]
|
||||
summary: 通道测试工具(channel:write;真实发送返回上游 MsgID;CMPP 用 sp_id 映射 SourceAddr)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [mobile, content], properties: {mobile: {type: string}, content: {type: string}}}}}}
|
||||
responses: {"200": {description: message_id+protocol+elapsed_ms}, "400": {description: 停用/CMPP3.0}}
|
||||
/channels/{id}/intercept:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 拦截配置查询(夜发/日限/屏蔽地区/黑关键字)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: intercept}}
|
||||
put:
|
||||
tags: [channel]
|
||||
summary: 拦截配置设置(channel:write;night_limit>0 即 22:00-08:00 禁发)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 成功}, "400": {description: 校验失败}}
|
||||
/channels/{id}/prices:
|
||||
post:
|
||||
tags: [channel]
|
||||
summary: 写入价格时间线(channel:write;Day 38 分时成本价)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [price]
|
||||
properties:
|
||||
price: {type: number, minimum: 0, description: 元/条}
|
||||
effective_at: {type: string, format: date-time, description: RFC3339;空=立即生效}
|
||||
responses: {"200": {description: id+price+effective_at}, "400": {description: 价格为负/时间格式}, "404": {description: 通道不存在}}
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 价格时间线(生效时间升序)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: total+list}}
|
||||
/channels/{id}/prices/current:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 当前时间点单价(EffectiveAt<=now 最新一条;无时间线回退通道实体 price)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: channel_id+price}}
|
||||
/channels/{id}/prices/{pid}:
|
||||
delete:
|
||||
tags: [channel]
|
||||
summary: 删除价格时间线记录(channel:write;须归属该通道)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
- {name: pid, in: path, required: true, schema: {type: integer}}
|
||||
responses: {"200": {description: 删除成功}, "404": {description: 记录不存在}}
|
||||
/channels/{id}/link-status:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 常驻链路状态(未建链返回 running=false;启用中 cmpp 通道展示池连接/健康数)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: channel_id/running/total/healthy/started_at}, "404": {description: 通道不存在}}
|
||||
/channels/{id}/receipts:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 最近回执/上行(内存环形≤100条倒序;状态报告含 stat,上行含 src/content;重启清空,持久化待 Day 51)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
- {name: limit, in: query, required: false, schema: {type: integer, default: 50, maximum: 100}}
|
||||
responses: {"200": {description: channel_id+total+list}, "404": {description: 通道不存在}}
|
||||
/send/estimate:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 发送预估(Day 41;D17 群发工作台实时条数——号码归一去重+长度拆分同源 SmsLength;template_id>0 时服务端取模板渲染)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [mobiles], properties: {task_no: {type: string}, account_id: {type: integer}, send_type: {type: integer, enum: [1, 2, 3, 4], default: 1}, template_id: {type: integer}, mobiles: {type: array, items: {type: string}, maxItems: 10000}, content: {type: string, maxLength: 1000}, variables: {type: object, additionalProperties: {type: string}}, sign: {type: string, maxLength: 32, description: "可选;不传时内容须自带签名(如【公司名】);计费=sign+content 合并口径"}, send_at: {type: string, description: "YYYY-MM-DD HH:mm(定时必填)"}, extend: {type: string}}}}}}
|
||||
responses: {"200": {description: "Estimate: valid_count/chars/segments/total_fee(=valid_count×segments;Day41 验收:chars/segments 为 签名+正文 合并口径,原样拼接零包装)"}, "400": {description: "号码/内容/变量/签名拒绝路径(ISC:400+中文 msg;长度上限 1000 亦按合并口径)"}, "404": {description: "template_id 不存在"}}
|
||||
/send/precheck:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 发送预检(Day 42;D18 个性短信提交前逐号过拦截器链 DryRun——黑名单三级/频次滑窗/签名/退订/地区;频次只读不占配额,拦截不落库;提交主链 Day 46 复用同链非 DryRun 且命中落 send_block_log)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [mobiles], properties: {task_no: {type: string}, account_id: {type: integer}, send_type: {type: integer, enum: [1, 2, 3, 4], default: 1}, template_id: {type: integer}, mobiles: {type: array, items: {type: string}, maxItems: 10000}, content: {type: string, maxLength: 1000}, variables: {type: object, additionalProperties: {type: string}}, sign: {type: string, maxLength: 32}, send_at: {type: string}, extend: {type: string}}}}}}
|
||||
responses:
|
||||
"200":
|
||||
description: "PrecheckResp: total/passed/blocked[]({mobile,interceptor: blacklist|frequency|sign|unsubscribe|region,code,msg})"
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
code: {type: string, example: "ISC:000"}
|
||||
data:
|
||||
type: object
|
||||
properties:
|
||||
total: {type: integer}
|
||||
passed: {type: integer}
|
||||
blocked:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
properties:
|
||||
mobile: {type: string}
|
||||
interceptor: {type: string, enum: [blacklist, frequency, sign, unsubscribe, region]}
|
||||
code: {type: string, example: "ISC:006"}
|
||||
msg: {type: string}
|
||||
"400": {description: "基础校验拒绝(号码/内容/变量/模板未过审;ISC:400)"}
|
||||
"404": {description: "template_id 不存在"}
|
||||
/send/submit:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 发送提交受理(Day 43 修复循环;最小提交链——校验→计费→余额只读校验(ISC:007)→事务落 message_task+message_detail 雪花ID→审核分流;task_no 幂等(重复 ISC:400);限流/冻结扣费/真实下发 Day45/46)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [mobiles], properties: {task_no: {type: string, description: "幂等键;缺省服务端生成 T+时间戳+随机"}, account_id: {type: integer, description: "缺省取 JWT 本人"}, send_type: {type: integer, enum: [1, 2, 3, 4], default: 1}, template_id: {type: integer}, mobiles: {type: array, items: {type: string}, maxItems: 10000}, content: {type: string, maxLength: 1000}, variables: {type: object, additionalProperties: {type: string}}, sign: {type: string, maxLength: 32}, send_at: {type: string, description: "YYYY-MM-DD HH:mm(send_type=3 必填且须未来)"}, extend: {type: string}}}}}}
|
||||
responses:
|
||||
"200":
|
||||
description: "SubmitResp: task_no/task_id/audit_status(0待审/1过/2拒/3免审)/audit_hint/valid_count/segments/total_fee/schedule_at(定时)"
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
code: {type: string, example: "ISC:000"}
|
||||
data:
|
||||
type: object
|
||||
properties:
|
||||
task_no: {type: string}
|
||||
task_id: {type: integer}
|
||||
audit_status: {type: integer, enum: [0, 1, 2, 3]}
|
||||
audit_hint: {type: string}
|
||||
valid_count: {type: integer}
|
||||
segments: {type: integer}
|
||||
total_fee: {type: integer}
|
||||
schedule_at: {type: string, description: RFC3339}
|
||||
"400": {description: "参数/号码/内容/定时时刻过去/任务号重复(ISC:400)"}
|
||||
"402": {description: "余额不足(ISC:007;口径 Balance<TotalFee 条数,不冻结)"}
|
||||
"404": {description: "template_id/账号不存在"}
|
||||
/audits/messages:
|
||||
get:
|
||||
tags: [message]
|
||||
summary: 待审消息列表(Day 43;审核中心——audit_status 过滤 0待/1过/2拒/3免/4超时,默认 0 待审,-1 全部)
|
||||
parameters:
|
||||
- {name: page, in: query, required: false, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, required: false, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: account_id, in: query, required: false, schema: {type: integer}}
|
||||
- {name: audit_status, in: query, required: false, schema: {type: integer, default: 0, enum: [-1, 0, 1, 2, 3, 4]}}
|
||||
- {name: status, in: query, required: false, schema: {type: integer, default: -1}}
|
||||
responses: {"200": {description: "total+list(message_task:task_no/content/sign/mobile_count/audit_status/created_at)"}}
|
||||
/audits/messages/counts:
|
||||
get:
|
||||
tags: [message]
|
||||
summary: 审核态计数(Day 45/D21 审核中心 Tab 徽标;GROUP BY audit_status)
|
||||
responses: {"200": {description: "total+by_status(map: 审核态→数量,0待/1过/2拒/3免/4超时)"}}
|
||||
/audits/messages/batch-review:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 批量审核(Day 45/D21;逐条原子翻转,已审跳过幂等;拒绝原因必填;单批≤100)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [ids, pass]
|
||||
properties:
|
||||
ids: {type: array, items: {type: integer}, maxItems: 100}
|
||||
pass: {type: boolean}
|
||||
reason: {type: string, maxLength: 255}
|
||||
responses: {"200": {description: "ok(成功流转数)+total(提交数)"}}
|
||||
/audits/messages/{id}/approve:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 审核通过(audit:write;原子翻转——仅 audit_status=0 可流转,审核人取 JWT 用户名)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {reason: {type: string}}}}}}
|
||||
responses: {"200": {description: "task+record(AuditRecord 留痕)"}, "409": {description: "非待审状态(ISC:037)"}}
|
||||
/audits/messages/{id}/reject:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 审核拒绝(audit:write;reason 必填;原子翻转同上)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [reason], properties: {reason: {type: string}}}}}}
|
||||
responses: {"200": {description: "task+record"}, "400": {description: "拒绝原因必填(ISC:400)"}, "409": {description: "非待审状态(ISC:037)"}}
|
||||
/send/tasks:
|
||||
get:
|
||||
tags: [message]
|
||||
summary: 发送任务列表(D19;状态/审核态/类型/账号过滤分页——定时任务列表数据源)
|
||||
parameters:
|
||||
- {name: page, in: query, required: false, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, required: false, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: account_id, in: query, required: false, schema: {type: integer}}
|
||||
- {name: status, in: query, required: false, schema: {type: integer, default: -1, enum: [-1, 0, 1, 2, 3]}}
|
||||
- {name: audit_status, in: query, required: false, schema: {type: integer, default: -1}}
|
||||
- {name: send_type, in: query, required: false, schema: {type: integer}}
|
||||
responses: {"200": {description: "total+list(message_task 全字段;send_type=3 为定时)"}}
|
||||
/send/tasks/{id}/cancel:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 取消任务(D19;仅 status=0 待发可取消→3;幂等拒绝)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: "取消后任务"}, "409": {description: "仅待发任务可取消(ISC:038)"}}
|
||||
/send/tasks/{id}/reschedule:
|
||||
put:
|
||||
tags: [message]
|
||||
summary: 定时任务改期(D19;仅 send_type=3 且待发;send_at 须未来时刻)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [send_at], properties: {send_at: {type: string, example: "2026-09-12 10:00"}}}}}}
|
||||
responses: {"200": {description: "改期后任务(schedule_at 已更新)"}, "400": {description: "时刻格式/过去时刻(ISC:400)"}, "409": {description: "仅定时待发任务可改期(ISC:038)"}}
|
||||
/templates:
|
||||
get:
|
||||
tags: [message]
|
||||
summary: 模板列表(Day 41;status 过滤 0待审/1通过/-1拒绝,指针语义缺省=全量)
|
||||
parameters:
|
||||
- {name: page, in: query, required: false, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, required: false, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: account_id, in: query, required: false, schema: {type: integer}}
|
||||
- {name: status, in: query, required: false, schema: {type: integer, enum: [-1, 0, 1]}}
|
||||
- {name: name, in: query, required: false, schema: {type: string}}
|
||||
responses: {"200": {description: "total+list(id/name/content/variables/status/audit_at/reason)"}}
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 新建模板(message:write;变量≤10,新建即待审,Variables 自动提取)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [account_id, name, content], properties: {account_id: {type: integer}, name: {type: string}, content: {type: string, maxLength: 500}}}}}}
|
||||
responses: {"200": {description: 创建后的实体(status=0 待审)}, "400": {description: 内容空/超长/变量超限}}
|
||||
/templates/{id}:
|
||||
get:
|
||||
tags: [message]
|
||||
summary: 模板详情
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 实体}, "404": {description: 不存在}}
|
||||
put:
|
||||
tags: [message]
|
||||
summary: 编辑模板(message:write;内容变更自动回待审并清空过审时间)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {name: {type: string}, content: {type: string, maxLength: 500}}}}}}
|
||||
responses: {"200": {description: "{id}"}, "400": {description: 无可更新字段/内容校验失败}}
|
||||
delete:
|
||||
tags: [message]
|
||||
summary: 删除模板(message:write;通过态须先拒审)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: "{id}"}, "400": {description: 通过态不可删}}
|
||||
/templates/{id}/review:
|
||||
put:
|
||||
tags: [message]
|
||||
summary: 审核模板(message:write;Day 43 审核服务接入前的模板态审核端点)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [pass], properties: {pass: {type: boolean}, reason: {type: string}}}}}}
|
||||
responses: {"200": {description: "{id}"}, "404": {description: 不存在}}
|
||||
/templates/batch-review:
|
||||
put:
|
||||
tags: [message]
|
||||
summary: 批量审核模板(Day 44/D20;事务内逐条原子翻转 WHERE status=0,已审跳过,返回成功数)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [ids, pass], properties: {ids: {type: array, items: {type: integer}, maxItems: 100}, pass: {type: boolean}, reason: {type: string}}}}}}
|
||||
responses: {"200": {description: "{ok,total}(ok=成功翻转数,已审条目不计)"}, "400": {description: ids 空/超 100}}
|
||||
/files/numbers/import:
|
||||
post:
|
||||
tags: [message]
|
||||
summary: 号码文件导入(Day 44;multipart file=TXT/CSV/XLSX≤10MB→解析第一列→归一校验去重→MinIO 落对象,返回计数/预览50/对象键)
|
||||
requestBody:
|
||||
content:
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
required: [file]
|
||||
properties:
|
||||
file: {type: string, format: binary, description: "号码文件(.txt 每行一号/.csv 第一列/.xlsx 首 sheet 第一列;首行表头自动识别)"}
|
||||
account_id: {type: integer, description: 缺省取 JWT 账号}
|
||||
responses:
|
||||
"200": {description: "{total,valid,duplicated,invalid,header_skipped,preview[],object_key}"}
|
||||
"400": {description: 格式不支持/空文件/全非法/超 10000 上限}
|
||||
/channels/monitor:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 通道实时监控快照(Day 40 · A-3.13;60s 滑窗 submit_ps/deliver_ps/fail_rate/latency_p50_ms+链路 running/healthy,与 WS 帧同构)
|
||||
responses: {"200": {description: "ChannelSnapshot[]: channel_id/name/protocol/status/running/total/healthy/submit_ps/deliver_ps/fail_rate/latency_p50_ms/updated_at"}}
|
||||
/channels/upstreams:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 上家(通道方)列表(Day 41 清债③;FE-1 UpstreamList 既有对接)
|
||||
parameters:
|
||||
- {name: page, in: query, required: false, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, required: false, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: name, in: query, required: false, schema: {type: string}}
|
||||
- {name: status, in: query, required: false, schema: {type: integer, enum: [0, 1]}}
|
||||
responses: {"200": {description: total+list(id/name/contact/phone/category/status/remark)}}
|
||||
post:
|
||||
tags: [channel]
|
||||
summary: 新建上家(channel:write;name 必填唯一)
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [name], properties: {name: {type: string}, contact: {type: string}, phone: {type: string}, category: {type: string}, status: {type: integer, enum: [0, 1], default: 1}, remark: {type: string}}}}}}
|
||||
responses: {"200": {description: 创建后的实体}, "400": {description: name 缺失/重名}}
|
||||
/channels/upstreams/{id}:
|
||||
put:
|
||||
tags: [channel]
|
||||
summary: 更新上家(channel:write;白名单 name/contact/phone/category/status/remark)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
requestBody: {content: {application/json: {schema: {type: object, properties: {name: {type: string}, contact: {type: string}, phone: {type: string}, category: {type: string}, status: {type: integer, enum: [0, 1]}, remark: {type: string}}}}}}
|
||||
responses: {"200": {description: "{id}"}, "400": {description: 无可更新字段/重名}}
|
||||
delete:
|
||||
tags: [channel]
|
||||
summary: 删除上家(channel:write;被通道引用则拒绝)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
responses: {"200": {description: "{id}"}, "400": {description: 被通道引用,须先解除关联}}
|
||||
/channels/monitor/ws:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 通道实时监控 WebSocket(Day 40 · A-3.13;每 2s 推送全通道快照数组,实时通讯曲线刷新;单 Hub 广播,写失败自动摘除)
|
||||
responses: {"101": {description: "升级成功后每 2s 一帧 ChannelSnapshot[] JSON"}, "400": {description: 非 WebSocket 握手}}
|
||||
/channels/{id}/logs:
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 通道日志(Day 40 · A-3.14;连接事件逐条+提交/回执窗口聚合行,kind 过滤;kind=connect|disconnect|stop|submit|submit_fail|deliver|deliver_fail|deliver_mo|alert)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
- {name: kind, in: query, required: false, schema: {type: string}}
|
||||
- {name: page, in: query, required: false, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, required: false, schema: {type: integer, default: 20, maximum: 100}}
|
||||
responses: {"200": {description: channel_id+total+list(kind/level/msg_id/detail/count/fail_count/latency_ms)}, "404": {description: 通道不存在}}
|
||||
/route-rules:
|
||||
post:
|
||||
tags: [channel]
|
||||
summary: 创建导流规则(channel:write;按账号/内容关键字指定通道,优先于四步路由)
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [channel_id, rule_type]
|
||||
properties:
|
||||
account_id: {type: integer, description: "账号维度;0=全局(keyword 规则允许)"}
|
||||
channel_id: {type: integer}
|
||||
rule_type: {type: string, enum: [keyword, account]}
|
||||
rule_config: {type: string, description: keyword 类型的匹配字(内容包含)}
|
||||
priority: {type: integer, description: 大者优先}
|
||||
responses: {"200": {description: 规则}, "400": {description: 类型/参数/通道校验失败}}
|
||||
get:
|
||||
tags: [channel]
|
||||
summary: 导流规则列表(优先级倒序)
|
||||
responses: {"200": {description: total+list}}
|
||||
/route-rules/{id}:
|
||||
put:
|
||||
tags: [channel]
|
||||
summary: 更新导流规则(channel:write;白名单 priority/status/rule_config)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object}}}}
|
||||
responses: {"200": {description: 成功}, "400": {description: 越权字段}, "404": {description: 不存在}}
|
||||
delete:
|
||||
tags: [channel]
|
||||
summary: 删除导流规则(channel:write;软删)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 删除成功}, "404": {description: 不存在}}
|
||||
/sms/api/sendMessageMass:
|
||||
post:
|
||||
tags: [openapi]
|
||||
summary: 外部群发提交(Day 46;SMS_HTTP_1.6 协议,MD5 验签,无 JWT;双桶限流)
|
||||
description: |
|
||||
独立协议域(不走 /api/v1 JWT)。鉴权:sign=MD5(userName+timestamp+MD5(secret)),
|
||||
timestamp 秒级 ±5min 窗口。中间件链:全局 IP 桶→验签(401)→账号桶(speed_limit)。
|
||||
响应码表:0 成功/1 用户名空/2 鉴权失败/3 禁用/4 未开通/5 缺参/6 号码空/7 超万/
|
||||
8 内容空/9 扩展码超长/10 callData 超长/12 定时不合法/16 时间戳失效/99 其他。
|
||||
security: []
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [userName, timestamp, sign, phoneList]
|
||||
properties:
|
||||
userName: {type: string}
|
||||
timestamp: {type: string, description: 秒级 Unix 时间戳}
|
||||
sign: {type: string, description: MD5(userName+timestamp+MD5(secret))}
|
||||
phoneList: {type: array, items: {type: string}, maxItems: 10000}
|
||||
content: {type: string}
|
||||
templateId: {type: string, description: 模板 ID(content 为空时使用,须已过审)}
|
||||
signName: {type: string}
|
||||
sendTime: {type: string, description: "yyyy-MM-dd HH:mm:ss;空=立即;≤15 天"}
|
||||
extCode: {type: string, maxLength: 32}
|
||||
callData: {type: string, maxLength: 64, description: 回执推送原样带回}
|
||||
responses:
|
||||
"200": {description: "code=0:{code,message,msgId,smsCount,taskNo}"}
|
||||
"401": {description: "code=1/2/3/4/16 鉴权类失败"}
|
||||
"429": {description: 限流(全局 IP 桶或账号桶)}
|
||||
/sms/api/sendMessageOne:
|
||||
post:
|
||||
tags: [openapi]
|
||||
summary: 外部个性提交(Day 46;phoneList/contentList 等长逐号配对,单次≤1000)
|
||||
security: []
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [userName, timestamp, sign, phoneList, contentList]
|
||||
properties:
|
||||
userName: {type: string}
|
||||
timestamp: {type: string}
|
||||
sign: {type: string}
|
||||
phoneList: {type: array, items: {type: string}, maxItems: 1000}
|
||||
contentList: {type: array, items: {type: string}, maxItems: 1000}
|
||||
signName: {type: string}
|
||||
sendTime: {type: string}
|
||||
extCode: {type: string, maxLength: 32}
|
||||
callData: {type: string, maxLength: 64}
|
||||
responses:
|
||||
"200": {description: "code=0:{code,message,msgId,smsCount,taskNo};14=数量不匹配/15=超限"}
|
||||
"401": {description: 鉴权类失败}
|
||||
"429": {description: 限流}
|
||||
/send/tasks/{id}/details:
|
||||
get:
|
||||
tags: [send]
|
||||
summary: 任务明细分页(Day 46/D22 结果反馈页;status/error_code 过滤;含 ext_code/call_data)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 50, maximum: 200}}
|
||||
- {name: status, in: query, schema: {type: integer, description: "-1=全部"}}
|
||||
- {name: error_code, in: query, schema: {type: string}}
|
||||
responses: {"200": {description: total+list}}
|
||||
/accounts/{id}/api-config:
|
||||
get:
|
||||
tags: [account]
|
||||
summary: 查看外部 API 配置(Day 46;Day 62 增 CMPP 密码/接入配额/交付块)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses:
|
||||
"200":
|
||||
description: "report_url/uplink_url/speed_limit/ext_code/secret_issued/secret_fingerprint;Day 62:cmpp_password(库值即密码,CMPP/SMPP/HTTP 客户端直接填它)/cmpp_max_conns(0=系统默认)/cmpp_speed/cmpp_*_effective(生效值)/cmpp_access{port,version,default_max_conns,default_speed}"
|
||||
"404": {description: 短信线不存在}
|
||||
put:
|
||||
tags: [account]
|
||||
summary: 更新外部 API 配置(Day 46;account:write;reset_secret 重发密钥仅返回一次;Day 62 增 CMPP 配额/自定义密钥)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
report_url: {type: string}
|
||||
uplink_url: {type: string}
|
||||
speed_limit: {type: integer, description: "条/秒;0=不修改"}
|
||||
ext_code: {type: string, maxLength: 32}
|
||||
reset_secret: {type: boolean, description: true=重新生成 32hex 密钥}
|
||||
custom_secret: {type: string, minLength: 6, maxLength: 64, description: "Day 62:自定义密码(优先于 reset_secret;原样入库零转换,CMPP/SMPP/HTTP 客户端均直接填它)"}
|
||||
cmpp_max_conns: {type: integer, description: "Day 62:每账号最大 CMPP 连接数(0=不修改;-1=恢复系统默认;1~1024=设置)"}
|
||||
cmpp_speed: {type: integer, description: "Day 62:单链提交限速 条/s(0=不修改;-1=恢复系统默认;1~100000=设置)"}
|
||||
responses: {"200": {description: "secret(=库值原文,重发/自定义时返回);cmpp_password(库值即密码)"}}
|
||||
components:
|
||||
securitySchemes:
|
||||
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}
|
||||
schemas:
|
||||
CreateAccount:
|
||||
type: object
|
||||
required: [username, password, role, business]
|
||||
properties:
|
||||
username: {type: string, minLength: 3, maxLength: 64}
|
||||
password: {type: string, minLength: 6, maxLength: 64}
|
||||
real_name: {type: string}
|
||||
role: {type: string, enum: [admin, ops, service, group, customer]}
|
||||
parent_id: {type: integer}
|
||||
business: {type: integer, description: "业务位掩码:1 短信/2 国际/4 视频/8 CSP/16 空号/32 短链"}
|
||||
credit_balance: {type: integer}
|
||||
svc_team: {type: string}
|
||||
remark: {type: string}
|
||||
|
||||
# x-isc-dict:ISC 错误码→中文 契约字典(Day 41 清债② codegen 源)。
|
||||
# 与后端 internal/pkg/errors/codes.go 由 openapi_sync_test 双向锁定,改动任一侧须跑 go run ./tools/gen-dict 再生成。
|
||||
x-isc-dict:
|
||||
ISC:000: 成功
|
||||
ISC:001: 网关错误
|
||||
ISC:002: 账号被禁用
|
||||
ISC:003: 签名被禁用
|
||||
ISC:004: 签名非法
|
||||
ISC:005: 通道非法
|
||||
ISC:006: 黑名单
|
||||
ISC:007: 欠费
|
||||
ISC:009: 短信网关内部错误
|
||||
ISC:010: 发送回执超时
|
||||
ISC:011: 用户不在线
|
||||
ISC:012: 用户停机
|
||||
ISC:013: 没有路由,携号转网
|
||||
ISC:014: 用户不在服务区
|
||||
ISC:015: 端口下线
|
||||
ISC:016: 等待5G消息终端响应超时
|
||||
ISC:017: 消息编码非法
|
||||
ISC:018: 用户未开通5G消息
|
||||
ISC:019: 彩信下发成功,下载超时
|
||||
ISC:020: 终端存储空间满
|
||||
ISC:021: 回落彩信延迟获取
|
||||
ISC:022: 不支持彩信
|
||||
ISC:023: 码号局数据错误
|
||||
ISC:024: 短信已下发,但是终端故障
|
||||
ISC:025: 找不到归属运营商,可能是新号段
|
||||
ISC:026: 无法识别彩信回执
|
||||
ISC:027: 空号
|
||||
ISC:029: 无通道路由
|
||||
ISC:030: 通道拒绝
|
||||
ISC:031: 内容不合规拦截
|
||||
ISC:032: 发送限制
|
||||
ISC:033: 超出账号设置的最大文本长度
|
||||
ISC:034: 超出账号每天发送量限制
|
||||
ISC:035: 超出账号授信额限制
|
||||
ISC:036: 地区屏蔽
|
||||
ISC:037: 审核状态冲突(非待审)
|
||||
ISC:038: 任务状态冲突(不可取消/改期)
|
||||
ISC:040: 夜间频控
|
||||
ISC:043: 5G消息拦截
|
||||
ISC:400: 参数错误
|
||||
ISC:401: 未认证
|
||||
ISC:402: Token 已过期
|
||||
ISC:403: 无权限
|
||||
ISC:404: 资源不存在
|
||||
ISC:405: 方法不允许
|
||||
ISC:429: 请求过于频繁
|
||||
ISC:500: 其他错误,需要人工核查
|
||||
ISC:501: 数据库错误
|
||||
ISC:502: 缓存错误
|
||||
ISC:503: 消息队列错误
|
||||
ISC:504: 服务暂不可用
|
||||
@@ -0,0 +1,331 @@
|
||||
// cmd/cmppbench/main.go CMPP 压力测试:随机 1[3-9] 手机号,大批量提交(默认 1 万条),
|
||||
// 测每秒提交峰值 TPS 与延迟分布——为后续性能限流规则准备基线数据。
|
||||
// 虚拟测试通道,不产生真实下发费用。
|
||||
//
|
||||
// 用法(压峰值): go run ./cmd/cmppbench -addr host:7890 -sp xx -secret xx \
|
||||
//
|
||||
// -count 10000 -links 4 -concurrency 8 -csv bench-1w.csv
|
||||
//
|
||||
// 限速阶梯(-rate N 条/s)配合递增 rate 可绘容量曲线、找网关限流拐点。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/protocol/cmpp"
|
||||
)
|
||||
|
||||
// rec 一笔提交的完整结果(CSV/统计数据源)。
|
||||
type rec struct {
|
||||
Ts time.Time // 发送时刻
|
||||
Mobile string
|
||||
MsgID uint64
|
||||
Result uint8
|
||||
Latency time.Duration // 提交→Submit_Resp
|
||||
Err string
|
||||
}
|
||||
|
||||
// Matency 延迟毫秒(rec 辅助)。
|
||||
func (r rec) Matency() float64 { return float64(r.Latency.Microseconds()) / 1000 }
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.LstdFlags)
|
||||
addr := flag.String("addr", "", "网关地址 host:port(必填)")
|
||||
sp := flag.String("sp", "", "企业代码 SourceAddr ≤6B(必填)")
|
||||
secret := flag.String("secret", "", "共享密钥(必填)")
|
||||
src := flag.String("src", "", "源号码 SrcID(默认=sp)")
|
||||
service := flag.String("service", "TEST", "Service_Id ≤10B")
|
||||
feeCode := flag.String("feecode", "", "FeeCode 6B(默认全 0)")
|
||||
content := flag.String("content", "bench", "短信内容(ASCII 原样/中文 GBK)")
|
||||
links := flag.Int("links", 4, "连接池连接数 1~16")
|
||||
conc := flag.Int("concurrency", 8, "并发提交 goroutine 数(即最大在途窗口)")
|
||||
count := flag.Int("count", 10000, "总提交条数")
|
||||
rate := flag.Int("rate", 0, "限速 条/s(0=不限速压峰值)")
|
||||
duration := flag.Duration("duration", 10*time.Minute, "安全兜底时长")
|
||||
expectReceipt := flag.Int("expect-receipt", 0, "验收模式:等待收到≥N 条 CMPP Deliver 回执再退出(0=不等;超时=duration)")
|
||||
receiptWait := flag.Duration("receipt-wait", 30*time.Second, "expect-receipt 等待窗口(发送完成后起算)")
|
||||
csvPath := flag.String("csv", "", "CSV 输出路径(默认 bench-<时间戳>.csv)")
|
||||
seed := flag.Int64("seed", 1, "随机种子(同种子号码序列可复现)")
|
||||
flag.Parse()
|
||||
if *addr == "" || *sp == "" || *secret == "" {
|
||||
log.Fatal("[压测] -addr/-sp/-secret 必填")
|
||||
}
|
||||
srcID := *src
|
||||
if srcID == "" {
|
||||
srcID = *sp
|
||||
}
|
||||
|
||||
body, msgFmt, err := cmpp.EncodeContent(*content)
|
||||
if err != nil {
|
||||
log.Fatalf("[压测] 内容编码: %v", err)
|
||||
}
|
||||
if len(body) > 140 {
|
||||
log.Fatalf("[压测] 内容 %d 字节超 140(CMPP 2.0 Msg_Length 1B)", len(body))
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
// Ctrl-C 可中断:已发部分照常输出汇总。
|
||||
go func() {
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
|
||||
<-sig
|
||||
log.Println("[压测] 收到中断信号,停止发送并汇总已发部分")
|
||||
cancel()
|
||||
}()
|
||||
|
||||
pool := cmpp.NewPool(cmpp.Config{Addr: *addr, SourceAddr: *sp, Secret: *secret}, *links)
|
||||
var deliverN atomic.Int64
|
||||
pool.OnDeliver(func(d *cmpp.Deliver) { deliverN.Add(1) })
|
||||
pool.Start(ctx)
|
||||
|
||||
// 等全部连接就绪(最多 15s)。
|
||||
readyDeadline := time.Now().Add(15 * time.Second)
|
||||
for pool.Healthy() < *links && time.Now().Before(readyDeadline) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
if pool.Healthy() == 0 {
|
||||
pool.Stop()
|
||||
log.Fatal("[压测] 15s 内无连接就绪,退出")
|
||||
}
|
||||
log.Printf("[压测] 就绪 连接=%d/%d 并发=%d 总量=%d 限速=%d/s 内容=%d字节 fmt=0x%02x(纯 CMPP 2.0)",
|
||||
pool.Healthy(), *links, *conc, *count, *rate, len(body), msgFmt)
|
||||
|
||||
// 限速令牌(rate>0):每秒放 rate 个。
|
||||
var tokenCh chan struct{}
|
||||
if *rate > 0 {
|
||||
tokenCh = make(chan struct{}, *rate*2)
|
||||
go func() {
|
||||
t := time.NewTicker(time.Second)
|
||||
defer t.Stop()
|
||||
for i := 0; i < *rate; i++ {
|
||||
select {
|
||||
case tokenCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
for i := 0; i < *rate; i++ {
|
||||
select {
|
||||
case tokenCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
var (
|
||||
issued atomic.Int64 // 已领取序号(1..count)
|
||||
doneN atomic.Int64 // 已完成(含失败)
|
||||
mu sync.Mutex
|
||||
recs = make([]rec, 0, *count)
|
||||
bucket = map[int64]int{} // 发送秒 → 条数(峰值 TPS 分桶)
|
||||
startT = time.Now()
|
||||
)
|
||||
doneCh := make(chan struct{})
|
||||
|
||||
// 进度打点:每 5s。
|
||||
go func() {
|
||||
t := time.NewTicker(5 * time.Second)
|
||||
defer t.Stop()
|
||||
var last int64
|
||||
for {
|
||||
select {
|
||||
case <-doneCh:
|
||||
return
|
||||
case <-t.C:
|
||||
d := doneN.Load()
|
||||
log.Printf("[进度] 完成 %d/%d(近5s %d/s) 健康=%d 回执=%d",
|
||||
d, *count, (d-last)/5, pool.Healthy(), deliverN.Load())
|
||||
last = d
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < *conc; w++ {
|
||||
wg.Add(1)
|
||||
go func(wid int) {
|
||||
defer wg.Done()
|
||||
rng := rand.New(rand.NewSource(*seed + int64(wid)))
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if int(issued.Add(1)) > *count {
|
||||
return
|
||||
}
|
||||
if tokenCh != nil {
|
||||
select {
|
||||
case <-tokenCh:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
mobile := randMobile(rng)
|
||||
start := time.Now()
|
||||
resp, err := pool.Submit(&cmpp.SubmitReq{
|
||||
ServiceID: *service, MsgFmt: msgFmt, MsgSrc: *sp,
|
||||
FeeType: "01", FeeCode: *feeCode, SrcID: srcID,
|
||||
DestTerminalIDs: []string{mobile},
|
||||
Content: body, RegisteredDelivery: 1,
|
||||
})
|
||||
lat := time.Since(start)
|
||||
r := rec{Ts: start, Mobile: mobile, Latency: lat}
|
||||
if err != nil {
|
||||
r.Err = err.Error()
|
||||
} else {
|
||||
r.MsgID, r.Result = resp.MsgID, resp.Result
|
||||
}
|
||||
mu.Lock()
|
||||
recs = append(recs, r)
|
||||
bucket[start.Unix()]++
|
||||
mu.Unlock()
|
||||
doneN.Add(1)
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
|
||||
// 兜底:duration 到期停止发送。
|
||||
go func() {
|
||||
select {
|
||||
case <-time.After(*duration):
|
||||
log.Printf("[压测] 兜底时长 %s 到期,停止发送", *duration)
|
||||
cancel()
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
// Day 61:-expect-receipt 验收模式:发送完成后保持连接等待 Deliver 回执
|
||||
// (提交→上游→回执→平台终态→PushDeliver 全链端到端验收)。
|
||||
if *expectReceipt > 0 {
|
||||
deadline := time.Now().Add(*receiptWait)
|
||||
for deliverN.Load() < int64(*expectReceipt) && time.Now().Before(deadline) {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
elapsed := time.Since(startT)
|
||||
close(doneCh)
|
||||
pool.Stop()
|
||||
delivers := deliverN.Load()
|
||||
if *expectReceipt > 0 && delivers < int64(*expectReceipt) {
|
||||
log.Printf("[验收] 回执 %d 条 < 期望 %d 条(P0.5 全链验收失败)", delivers, *expectReceipt)
|
||||
os.Exit(2)
|
||||
}
|
||||
if *expectReceipt > 0 {
|
||||
log.Printf("[验收] 回执 %d 条 ≥ 期望 %d 条(P0.5 全链验收通过:Submit→上游→回执→终态→Deliver)", delivers, *expectReceipt)
|
||||
}
|
||||
|
||||
// ---- 统计汇总 ----
|
||||
mu.Lock()
|
||||
total := len(recs)
|
||||
var okN, failSend int
|
||||
failResult := map[uint8]int{}
|
||||
lats := make([]float64, 0, total)
|
||||
peak := 0
|
||||
for _, r := range recs {
|
||||
if r.Err != "" {
|
||||
failSend++
|
||||
continue
|
||||
}
|
||||
if r.Result != 0 {
|
||||
failResult[r.Result]++
|
||||
continue
|
||||
}
|
||||
okN++
|
||||
lats = append(lats, r.Matency())
|
||||
}
|
||||
for _, n := range bucket {
|
||||
if n > peak {
|
||||
peak = n
|
||||
}
|
||||
}
|
||||
if *csvPath == "" {
|
||||
*csvPath = fmt.Sprintf("bench-%s.csv", time.Now().Format("0102-150405"))
|
||||
}
|
||||
writeCSV(*csvPath, recs)
|
||||
mu.Unlock()
|
||||
|
||||
avg := float64(total) / elapsed.Seconds()
|
||||
sort.Float64s(lats)
|
||||
pct := func(p float64) float64 {
|
||||
if len(lats) == 0 {
|
||||
return 0
|
||||
}
|
||||
i := int(float64(len(lats)) * p)
|
||||
if i >= len(lats) {
|
||||
i = len(lats) - 1
|
||||
}
|
||||
return lats[i]
|
||||
}
|
||||
maxMs := float64(0)
|
||||
if len(lats) > 0 {
|
||||
maxMs = lats[len(lats)-1]
|
||||
}
|
||||
failN := total - okN
|
||||
summary := map[string]any{
|
||||
"total": total, "ok": okN, "fail": failN,
|
||||
"fail_send": failSend, "fail_result": failResult,
|
||||
"peak_tps": peak, "avg_tps": round2(avg),
|
||||
"p50_ms": round2(pct(0.50)), "p90_ms": round2(pct(0.90)),
|
||||
"p99_ms": round2(pct(0.99)), "max_ms": round2(maxMs),
|
||||
"deliver": delivers, "elapsed_s": round2(elapsed.Seconds()),
|
||||
"links": *links, "concurrency": *conc, "rate_limit": *rate,
|
||||
}
|
||||
jb, _ := json.Marshal(summary)
|
||||
log.Printf("[汇总] 总量=%d 成功=%d 失败=%d(发送错=%d result≠0=%v) 峰值TPS=%d 均值TPS=%.1f 延迟p50/p90/p99/max=%.1f/%.1f/%.1f/%.1fms 耗时=%s 回执=%d",
|
||||
total, okN, failN, failSend, failResult, peak, avg,
|
||||
pct(0.50), pct(0.90), pct(0.99), maxMs, elapsed.Round(time.Millisecond), delivers)
|
||||
log.Printf("[摘要JSON] %s", jb)
|
||||
log.Printf("[产物] CSV=%s(%d 行)", *csvPath, total)
|
||||
}
|
||||
|
||||
// randMobile 随机 11 位手机号:1[3-9] + 10 位数字。
|
||||
func randMobile(rng *rand.Rand) string {
|
||||
b := make([]byte, 11)
|
||||
b[0] = '1'
|
||||
b[1] = byte('3' + rng.Intn(7))
|
||||
for i := 2; i < 11; i++ {
|
||||
b[i] = byte('0' + rng.Intn(10))
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func writeCSV(path string, recs []rec) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Printf("[产物] CSV 创建失败: %v", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
f.WriteString("ts,mobile,msg_id,result,latency_ms,error\n")
|
||||
for _, r := range recs {
|
||||
errStr := r.Err
|
||||
if errStr == "" && r.Result != 0 {
|
||||
errStr = fmt.Sprintf("result=%d", r.Result)
|
||||
}
|
||||
fmt.Fprintf(f, "%s,%s,%d,%d,%.3f,%s\n",
|
||||
r.Ts.Format("15:04:05.000"), r.Mobile, r.MsgID, r.Result, r.Matency(), errStr)
|
||||
}
|
||||
}
|
||||
|
||||
func round2(v float64) float64 { return float64(int(v*100+0.5)) / 100 }
|
||||
@@ -0,0 +1,150 @@
|
||||
// migrate 数据库模式迁移工具(Day 14):
|
||||
//
|
||||
// go run ./cmd/migrate -config configs/config.dev.yaml up # 迁移到最新
|
||||
// go run ./cmd/migrate down # 回滚最近一个版本
|
||||
// go run ./cmd/migrate version # 查看当前版本
|
||||
// go run ./cmd/migrate status # 待执行迁移列表
|
||||
// go run ./cmd/migrate seed-admin # Day 31:首次启动引导——迁移+内置超管(账户1,随机密码打印一次)
|
||||
//
|
||||
// 版本记录:schema_migrations 表(ID + 时间戳);环境变量 GOSMS_CONFIG 同样生效。
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/config"
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/repository/mysql"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", os.Getenv(config.EnvKeyFile), "配置文件路径")
|
||||
flag.Parse()
|
||||
cmd := flag.Arg(0)
|
||||
if cmd == "" {
|
||||
cmd = "up"
|
||||
}
|
||||
|
||||
cfg, err := config.Load(*configPath)
|
||||
if err != nil {
|
||||
fatal("加载配置失败: %v", err)
|
||||
}
|
||||
db, err := mysql.Open(cfg.MySQL.DSN, mysql.PoolOptions{
|
||||
MaxOpenConns: cfg.MySQL.MaxOpenConns, MaxIdleConns: cfg.MySQL.MaxIdleConns,
|
||||
ConnMaxLifetime: cfg.MySQL.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
fatal("连接 MySQL 失败: %v", err)
|
||||
}
|
||||
|
||||
// seed-admin 先迁移再引导(幂等:AutoMigrate 补列 + 种子 + 超管)。
|
||||
if cmd == "seed-admin" {
|
||||
seedAdmin(db)
|
||||
return
|
||||
}
|
||||
|
||||
m := mysql.NewMigrator(db)
|
||||
version := func() string {
|
||||
v, err := mysql.CurrentMigrationVersion(db)
|
||||
if err != nil {
|
||||
fatal("查询版本失败: %v", err)
|
||||
}
|
||||
return orDefault(v)
|
||||
}
|
||||
switch cmd {
|
||||
case "up":
|
||||
if err := m.Migrate(); err != nil {
|
||||
fatal("迁移失败: %v", err)
|
||||
}
|
||||
fmt.Printf(">> 迁移完成,当前版本: %s\n", version())
|
||||
case "down":
|
||||
if err := m.RollbackLast(); err != nil {
|
||||
fatal("回滚失败: %v", err)
|
||||
}
|
||||
fmt.Printf(">> 回滚完成,当前版本: %s\n", version())
|
||||
case "version":
|
||||
fmt.Printf(">> %s\n", version())
|
||||
case "status":
|
||||
cur, err := mysql.CurrentMigrationVersion(db)
|
||||
if err != nil {
|
||||
fatal("查询版本失败: %v", err)
|
||||
}
|
||||
fmt.Printf(">> 当前版本: %s\n", orDefault(cur))
|
||||
pending := 0
|
||||
for _, id := range migrationIDs() {
|
||||
if cur == nil || id > *cur {
|
||||
pending++
|
||||
fmt.Printf(">> 待执行: %s\n", id)
|
||||
}
|
||||
}
|
||||
if pending == 0 {
|
||||
fmt.Println(">> 无待执行迁移")
|
||||
}
|
||||
default:
|
||||
fatal("未知命令 %q(可用: up | down | version | status)", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// migrationIDs 已排序的迁移 ID(status 展示用)。
|
||||
func migrationIDs() []string {
|
||||
var ids []string
|
||||
for _, m := range mysql.Migrations() {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return ids
|
||||
}
|
||||
|
||||
// seedAdmin Day 31 首次启动引导:迁移(AutoMigrate 补列+种子) → 内置超管。
|
||||
// 账户 ID 固定 1,与 Casbin 种子 g("1","admin") 对齐;密码随机生成仅打印一次。
|
||||
func seedAdmin(db *gorm.DB) {
|
||||
if err := mysql.NewMigrator(db).Migrate(); err != nil {
|
||||
fatal("迁移失败: %v", err)
|
||||
}
|
||||
var n int64
|
||||
db.Model(&entity.AccountUser{}).Count(&n)
|
||||
if n > 0 {
|
||||
log.Printf("[seed-admin] account_user 已有 %d 个账号,跳过引导(如需重置密码请走账号管理)", n)
|
||||
return
|
||||
}
|
||||
raw := make([]byte, 8)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
fatal("生成随机密码失败: %v", err)
|
||||
}
|
||||
password := hex.EncodeToString(raw)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
fatal("bcrypt 哈希失败: %v", err)
|
||||
}
|
||||
admin := &entity.AccountUser{
|
||||
Model: entity.Model{ID: 1},
|
||||
Username: "admin", PasswordHash: string(hash), RealName: "内置超管",
|
||||
Role: "admin", Path: "/1/", Status: 1,
|
||||
}
|
||||
if err := db.Create(admin).Error; err != nil {
|
||||
fatal("写入超管失败: %v", err)
|
||||
}
|
||||
fmt.Println(">> 内置超管已创建(仅本次打印,请立即保存):")
|
||||
fmt.Printf(">> 用户名: admin\n>> 密 码: %s\n", password)
|
||||
}
|
||||
|
||||
func orDefault(v *string) string {
|
||||
if v == nil {
|
||||
return "(未初始化)"
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func fatal(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, "[migrate] "+format+"\n", args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// reconcile/refund_lost 2026-09-28:宿主机休眠事故对账退款——41.1 万压测中
|
||||
// gateway producer 在途缓冲丢失的 sms-submit 事件(预扣已入账、明细永不落库),
|
||||
// 按清单表 reconcile_lost_20260928 逐 task 经 balance.RedisStore.Refund 原子
|
||||
// 加回余额+异步退款流水(biz_type=refund),口径与业务退款完全一致。
|
||||
// 幂等:先置 state=1 再退款(崩溃重跑只会少退不会多退,终局对账兜底补退)。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/app"
|
||||
"go-sms/internal/service/balance"
|
||||
"go-sms/internal/util"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type lostRow struct {
|
||||
TaskNo string `gorm:"column:task_no"`
|
||||
RefundAmt int64 `gorm:"column:refund_amt"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
a, err := app.New(app.Options{ServiceName: "reconcile-refund"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
db, rc := a.DB(), a.Redis()
|
||||
lg := a.Lg()
|
||||
accountID := int64(3)
|
||||
if v := os.Getenv("RECON_ACCOUNT"); v != "" {
|
||||
fmt.Sscan(v, &accountID)
|
||||
}
|
||||
|
||||
sf, err := util.NewSnowflake(9)
|
||||
if err != nil {
|
||||
lg.Error("snowflake_init_failed", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
store := balance.NewRedisStore(rc, db, sf, lg, balance.RedisOptions{})
|
||||
defer store.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var rows []lostRow
|
||||
if err := db.WithContext(ctx).
|
||||
Table("reconcile_lost_20260928").
|
||||
Select("task_no, refund_amt").
|
||||
Where("state = 0").Find(&rows).Error; err != nil {
|
||||
lg.Error("load_lost_list_failed", zap.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
lg.Info("reconcile_refund_start", zap.Int("pending", len(rows)))
|
||||
|
||||
var okSum, failCnt int64
|
||||
for i, r := range rows {
|
||||
// 先标记(保守:崩溃重跑少退可补,多退不可逆)
|
||||
if err := db.Exec("UPDATE reconcile_lost_20260928 SET state=1, refunded_at=NOW() WHERE task_no=? AND state=0", r.TaskNo).Error; err != nil {
|
||||
lg.Error("mark_failed", zap.String("task_no", r.TaskNo), zap.Error(err))
|
||||
failCnt++
|
||||
continue
|
||||
}
|
||||
if err := store.Refund(ctx, accountID, r.RefundAmt, r.TaskNo, "休眠丢件对账退款"); err != nil {
|
||||
lg.Error("refund_failed", zap.String("task_no", r.TaskNo), zap.Error(err))
|
||||
db.Exec("UPDATE reconcile_lost_20260928 SET state=0 WHERE task_no=?", r.TaskNo)
|
||||
failCnt++
|
||||
continue
|
||||
}
|
||||
okSum += r.RefundAmt
|
||||
if (i+1)%5000 == 0 {
|
||||
lg.Info("reconcile_progress", zap.Int("done", i+1), zap.Int64("refunded", okSum))
|
||||
}
|
||||
}
|
||||
time.Sleep(2 * time.Second) // 等异步流水 flush
|
||||
lg.Info("reconcile_refund_done",
|
||||
zap.Int("total", len(rows)), zap.Int64("refunded_amount", okSum),
|
||||
zap.Int64("failed", failCnt))
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
# ============================================================
|
||||
# go-sms(Go-sms)开发环境配置(Day 2)
|
||||
# 说明:
|
||||
# 1. 中间件地址对应 docker-compose.dev.yaml 的宿主机端口映射
|
||||
# 2. 所有键均可被环境变量覆盖:层级以 "_" 连接,加前缀 GOSMS_
|
||||
# 示例:mysql.dsn -> GOSMS_MYSQL_DSN;redis.addrs -> GOSMS_REDIS_ADDRS
|
||||
# ============================================================
|
||||
|
||||
server:
|
||||
name: go-sms-gateway
|
||||
http:
|
||||
addr: ":8080" # HTTP API 监听地址
|
||||
read_timeout: 30s
|
||||
write_timeout: 30s
|
||||
cmpp: # CMPP 2.0 服务端(Day 47 客户接入;addr 空=不启用)
|
||||
addr: ":7890"
|
||||
max_connections_per_account: 50 # Day 62:每账号最大连接数系统默认(账号级可配,0=回退此值;0=不限)
|
||||
idle_timeout: 60s
|
||||
window_size: 32
|
||||
submit_rate_per_conn: 50 # Day 62:单链提交限速 条/s 系统默认(账号级可配;0=不限)
|
||||
longsms_window: 72h # Day 71:入向长短信挂起窗(不齐不发,到期失败终态+UNDELIV 回执)
|
||||
smpp: # SMPP 3.4 服务端(Day 47 客户接入;addr 空=不启用)
|
||||
addr: ":27775"
|
||||
idle_timeout: 60s
|
||||
|
||||
log: # zap + lumberjack(Day 5 实现)
|
||||
level: info # debug | info | warn | error
|
||||
format: json # json | console
|
||||
output: # 同时输出到 stdout 与文件
|
||||
- stdout
|
||||
- file
|
||||
file:
|
||||
path: /var/log/go-sms/app.log
|
||||
max_size_mb: 100
|
||||
max_age_days: 30
|
||||
max_backups: 10
|
||||
compress: true
|
||||
|
||||
mysql: # 宿主机端口 13306(make up)
|
||||
dsn: "gosms:gosms123@tcp(127.0.0.1:13306)/gosms?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
max_open_conns: 100
|
||||
max_idle_conns: 20
|
||||
conn_max_lifetime: 1h
|
||||
slow_threshold_ms: 500
|
||||
|
||||
redis: # 宿主机端口 16379
|
||||
mode: single # single | cluster | sentinel
|
||||
addrs: ["127.0.0.1:16379"]
|
||||
password: ""
|
||||
pool_size: 100
|
||||
|
||||
kafka: # 宿主机端口 19092
|
||||
brokers: ["127.0.0.1:19092"]
|
||||
group_id: go-sms
|
||||
max_retry: 3
|
||||
topics: # 与实施计划 A-0.4 验收项一致
|
||||
submit: sms-submit # 发送任务
|
||||
report: sms-report # 状态回执
|
||||
up: sms-up # 上行消息
|
||||
stats: stats # 统计事件
|
||||
consumer: # Day 48:submit 消费域(cmd/consumer)
|
||||
group_id: go-sms-consumer
|
||||
reclaim_every: 5s # DB 认领补偿扫描周期(事件丢失/审核通过兜底)
|
||||
reclaim_batch: 100 # 单轮认领上限
|
||||
bp_sample_every: 10s # 背压 lag 采样周期(三级水位 10万/100万/500万)
|
||||
enable: true # pacer 限速+背压联动总开关
|
||||
offset_mode: oldest # Day58:起步 offset(仅无已提交 offset 的组生效;dev 止血 env 覆盖 GOSMS_KAFKA_CONSUMER_OFFSET_MODE=latest)
|
||||
pace_every: 500 # 消费限速阀门:每 500 条歇 100ms(压测 09-27 临时置 0 已恢复;P2 若再压先权衡 HDD)
|
||||
pace_sleep: 100ms
|
||||
workers: 4 # Day65:每分区并行 worker(1=串行;Deferred 慢事务链吞吐=并发÷延迟,dev HDD 实测串行 16 分区≈60/s 封顶)
|
||||
|
||||
etcd: # 宿主机端口 12379
|
||||
endpoints: ["127.0.0.1:12379"]
|
||||
dial_timeout: 5s
|
||||
|
||||
clickhouse: # Native 19000 / HTTP 18123
|
||||
addrs: ["127.0.0.1:19000"]
|
||||
database: gosms
|
||||
username: gosms
|
||||
password: gosms123
|
||||
dial_timeout: 10s
|
||||
|
||||
minio: # S3 API 19010 / Web Console 19011
|
||||
endpoint: "127.0.0.1:19010"
|
||||
access_key: go-sms
|
||||
secret_key: go-sms123456
|
||||
bucket: gosms
|
||||
use_ssl: false
|
||||
|
||||
tracer: # OTLP → Jaeger(Day 9);宿主机 14318
|
||||
enabled: true
|
||||
otlp_endpoint: "127.0.0.1:14318"
|
||||
service_name: "go-sms-gateway"
|
||||
|
||||
rate_limit: # Redis 令牌桶分布式限流(Day 9)
|
||||
enabled: true
|
||||
capacity: 1000 # 桶容量(突发上限)
|
||||
rate_per_sec: 100 # 每秒回填令牌数
|
||||
key: ip # ip | global
|
||||
|
||||
balance: # 资金预扣模式(Day54 R1,评审 docs/review/r1-balance.md)
|
||||
mode: redis # P1 压测(09-27):mysql→redis——行锁扣款(HDD 单行串行 ~140ms)把受理锁死在 30/s(实测 20:41-20:45);SubmitFast 快路径设计前提=Redis Lua(112μs);对账覆写 5min 兜底
|
||||
reconcile_interval: 5m # 对账周期(评审约束 ≤5min;MySQL 流水重算覆写 Redis)
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"version": "0.2",
|
||||
"language": "en",
|
||||
"files": ["**/*.go", "**/*.ts", "**/*.vue"],
|
||||
"ignorePaths": [
|
||||
"**/node_modules/**",
|
||||
"**/dist/**",
|
||||
"**/*.sum",
|
||||
"web/public/mockServiceWorker.js",
|
||||
"web/src/api/dict.ts",
|
||||
"web/src/api/openapi.d.ts"
|
||||
],
|
||||
"useGitignore": true,
|
||||
"words": [
|
||||
"gosms", "Gosms", "GOSMS",
|
||||
"SGIP", "sgip", "CMPP", "cmpp", "SMPP", "smpp",
|
||||
"UCS", "UCS2", "MsgID", "msgid", "seqid",
|
||||
"precheck", "Precheck",
|
||||
"tmpl", "varchar", "datetime", "zset", "ZSet",
|
||||
"gopls", "gorm", "pinia", "vitest", "volar", "msw",
|
||||
"fe2", "FE2",
|
||||
"dryrun", "DryRun",
|
||||
"upsert", "Upsert",
|
||||
"apikey", "unsub", "unsubscribe"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
# alert_rules.yml Day 53(runbook §6.2):告警规则(指标现实核对版)。
|
||||
# 已可用指标(internal/pkg/metrics/prometheus.go):http_requests_total{method,route,status}/
|
||||
# http_request_duration_seconds_bucket{method,route}/http_requests_in_flight。
|
||||
# 挂账注记:
|
||||
# - ~~Kafka lag(P1)~~(Day55 已清偿:gateway 侧 LagReader 采样导出 go_sms_kafka_lag{topic},
|
||||
# 规则见 gateway-p1 组;背压三级水位 L1 口径一致 lag>10 万);
|
||||
# - 通道速率异常(P0 资损):Monitor 内存指标未导出 Prometheus——挂账 Day56 并入;
|
||||
# - BalanceReconcileDiff(P0·R1 前置接入位):Day54 对账任务导出
|
||||
# go_sms_balance_reconcile_diff{account} 后本规则即生效(abs>0 for 1m)。
|
||||
groups:
|
||||
- name: gateway-p0
|
||||
rules:
|
||||
- alert: HTTPError5xxBurst
|
||||
expr: sum by (route) (rate(http_requests_total{status=~"5.."}[5m])) > 1
|
||||
for: 2m
|
||||
labels:
|
||||
severity: P0
|
||||
annotations:
|
||||
summary: "5xx 突增 {{ $labels.route }}"
|
||||
description: "5 分钟 5xx 速率 {{ $value }}/s 超 1(runbook §0:全站不可用/资损级)"
|
||||
|
||||
- alert: BalanceReconcileDiff
|
||||
expr: abs(go_sms_balance_reconcile_diff) > 0
|
||||
for: 1m
|
||||
labels:
|
||||
severity: P0
|
||||
annotations:
|
||||
summary: "R1 余额对账差额 account={{ $labels.account }}"
|
||||
description: "Redis 预扣余额与 MySQL 流水重算差 {{ $value }} 条(流水为唯一事实源,runbook §3 数据修复专项)"
|
||||
|
||||
- name: gateway-p1
|
||||
rules:
|
||||
- alert: HTTPP99High
|
||||
expr: histogram_quantile(0.99, sum by (route, le) (rate(http_request_duration_seconds_bucket[5m]))) > 0.5
|
||||
for: 5m
|
||||
labels:
|
||||
severity: P1
|
||||
annotations:
|
||||
summary: "P99>500ms {{ $labels.route }}"
|
||||
description: "接口 P99 {{ $value }}s(runbook §6.2 P1 口径)"
|
||||
|
||||
- alert: KafkaLagHigh
|
||||
expr: go_sms_kafka_lag > 100000
|
||||
for: 2m
|
||||
labels:
|
||||
severity: P1
|
||||
annotations:
|
||||
summary: "submit 消费组 lag>10 万 topic={{ $labels.topic }}"
|
||||
description: "Kafka 积压 {{ $value }} 条(背压 L1 告警口径;持续不收敛查 consumer 通道侧限速/暂停,runbook §2)"
|
||||
|
||||
# Day 69 百万级长短信压测观测组(gateway: 入向聚合健康度/consumer: 回执聚合健康度)。
|
||||
- name: longsms-1m
|
||||
rules:
|
||||
- alert: LongSmsExpireFailBurst
|
||||
expr: increase(go_sms_longsms_expire_fail_total[1h]) > 10
|
||||
for: 5m
|
||||
labels:
|
||||
severity: P1
|
||||
annotations:
|
||||
summary: "长短信挂起组 72h 到期失败突增"
|
||||
description: "1h 到期失败 {{ $value }} 组——客户端拆片后大面积丢片(如 ISC:004 32% 超时率),建议推进客户端背靠背发片整改"
|
||||
|
||||
- alert: LongSmsOverflowDegraded
|
||||
expr: increase(go_sms_longsms_degrade_total{kind=~"overflow|ref_conflict"}[5m]) > 0
|
||||
for: 1m
|
||||
labels:
|
||||
severity: P1
|
||||
annotations:
|
||||
summary: "长短信聚合溢出/协议异常退化"
|
||||
description: "并发未到齐组逼近 4096 上限或同组 total 不一致(5 分钟 {{ $value }} 片)——压测速率×片间隔需下调"
|
||||
|
||||
- alert: ReportPendingNoFinalize
|
||||
expr: rate(go_sms_report_pending_total[5m]) > 1 and rate(go_sms_report_recheck_total{kind="finalized"}[5m]) == 0
|
||||
for: 10m
|
||||
labels:
|
||||
severity: P1
|
||||
annotations:
|
||||
summary: "回执聚合持续挂起且复查零终态"
|
||||
description: "pending 事件持续产生而 recheck 补刀为 0——回执段丢失(通道未回执),将挂至 48h 补偿,核对上游通道回执率"
|
||||
@@ -0,0 +1,22 @@
|
||||
# alertmanager.yml Day 53(runbook §6.2 P0):告警分发通道。
|
||||
# dev 验收链路:Prometheus 规则 → AM 分组(group_interval 10s 加速验收)→ webhook
|
||||
# → 宿主网关 POST /alerts/sink(日志留痕,P0=Error/P1=Warn/resolved=Info)。
|
||||
# 生产切换:钉钉/企微改用 prometheus-webhook-dingtalk 侧车或直连机器人 webhook,
|
||||
# 并给 /alerts/sink 加内网/白名单(当前 public 为 AM 无 JWT 所需)。
|
||||
route:
|
||||
receiver: gateway-sink
|
||||
group_by: ["alertname"]
|
||||
group_wait: 5s
|
||||
group_interval: 10s
|
||||
repeat_interval: 1h
|
||||
routes:
|
||||
- matchers:
|
||||
- severity="P0"
|
||||
receiver: gateway-sink
|
||||
repeat_interval: 15m # P0 提频重发(runbook §0 电话+强提醒语义)
|
||||
|
||||
receivers:
|
||||
- name: gateway-sink
|
||||
webhook_configs:
|
||||
- url: http://host.docker.internal:8080/api/v1/alerts/sink
|
||||
send_resolved: true
|
||||
@@ -0,0 +1,30 @@
|
||||
# prometheus.yml Day 53(runbook §6.2 P0):抓取网关 /metrics。
|
||||
# 网关为宿主进程(configs/config.dev.yaml :8080),容器内经 host.docker.internal 访问
|
||||
# (compose extra_hosts: host-gateway 映射;生产改为 k8s Service 发现)。
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
evaluation_interval: 15s
|
||||
|
||||
rule_files:
|
||||
- /etc/prometheus/alert_rules.yml
|
||||
|
||||
alerting:
|
||||
alertmanagers:
|
||||
- static_configs:
|
||||
- targets: ["alertmanager:9093"]
|
||||
|
||||
scrape_configs:
|
||||
- job_name: gateway
|
||||
metrics_path: /metrics
|
||||
static_configs:
|
||||
- targets: ["host.docker.internal:8080"]
|
||||
labels:
|
||||
env: dev
|
||||
|
||||
# Day 69 百万级长短信压测:consumer worker 侧指标(回执聚合/recheck 健康度)。
|
||||
- job_name: consumer
|
||||
metrics_path: /metrics
|
||||
static_configs:
|
||||
- targets: ["host.docker.internal:8082"]
|
||||
labels:
|
||||
env: dev
|
||||
@@ -0,0 +1,239 @@
|
||||
# ============================================================
|
||||
# go-sms(Go-sms)本地开发环境中间件(Day 1)
|
||||
# 使用:make up(启动并等待 healthy)/ make down(完全清理)/ make logs
|
||||
# 端口规划(避免本机与中间件默认端口冲突):
|
||||
# MySQL 13306 | Redis 16379 | Kafka 19092(外部容器 go-sms-kafka 提供,见下) | ETCD 12379
|
||||
# ClickHouse HTTP 18123 / Native 19000 | MinIO API 19010 / Console 19011
|
||||
#
|
||||
# ⚠️ Kafka 说明(2026-09-11 Day47 运维处置,详见 docs/runbook/incident-response.md):
|
||||
# 本 compose 曾含 kafka 服务(go-sms-kafka),历史原因容器脱离 go-sms-dev_default 网络,
|
||||
# KRaft QUORUM_VOTERS(1@kafka:29093)解析失败→broker 永不就绪→重启循环 1271 次,
|
||||
# 且与外部 Kafka 容器抢宿主端口 19092。已停用(docker update --restart=no + stop;2026-09-12 尸体已删除腾名)。
|
||||
# 当前本地开发 Kafka = go-sms-kafka(2026-09-12 由 hwsms-kafka 更名;手动 docker run,非 compose 管理;
|
||||
# 2026-09-13 网络迁移:历史遗留网 hwsms-dev_default→go-sms-dev_default,显式别名 kafka 随迁
|
||||
# (KRaft QUORUM_VOTERS=1@kafka:29093 依赖它,解析 172.22.0.8),宿主 19092,
|
||||
# 网关 configs/config.dev.yaml brokers=127.0.0.1:19092 即它;Day45/46 冒烟均其承载。
|
||||
# 注意:advertised=127.0.0.1:19092 为宿主观,容器内 CLI 连不通属预期,验证须走 host 网络容器(scripts/init_kafka_topics.sh 同法)。
|
||||
# 若未来恢复本服务:①取消下段注释 ②QUORUM_VOTERS 主机名与网络别名对齐 ③换宿主端口避免冲突。
|
||||
# ============================================================
|
||||
name: go-sms-dev
|
||||
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:8.0
|
||||
container_name: go-sms-mysql
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
MYSQL_ROOT_PASSWORD: root123
|
||||
MYSQL_DATABASE: gosms
|
||||
MYSQL_USER: gosms
|
||||
MYSQL_PASSWORD: gosms123
|
||||
command:
|
||||
- --character-set-server=utf8mb4
|
||||
- --collation-server=utf8mb4_unicode_ci
|
||||
- --default-time-zone=+08:00
|
||||
# Day62d 吞吐修复:双 1 刷盘高并发小事务 fsync 排队(压测 SLOW 1394 条/审核
|
||||
# UPDATE 1s),dev 回归 2/0(掉电丢 ≤1s 事务可接受);生产另评估。
|
||||
- --innodb-flush-log-at-trx-commit=2
|
||||
- --sync-binlog=0
|
||||
# Day64 明细查询修复:message_detail 190 万行/2GB,默认 buffer_pool=128M
|
||||
# 缓存零命中+HDD 全扫(实测 downlink-details 46-82s);调 4G 全热驻留
|
||||
# (宿主 31G/可用 15G 余量充足;已先 SET GLOBAL 在线生效,此处持久化)。
|
||||
# Day66 单表化后再调 6G:百万级/天写入冲刷 4G 池导致二级索引页换出,
|
||||
# 冷查询 HDD 随机回表 29.5s;6G 给 detail 索引+数据页更多常驻余量
|
||||
# (表+索引已 4.5G+;宿主 31G,在线 SET GLOBAL 生效,此处持久化)。
|
||||
- --innodb-buffer-pool-size=6G
|
||||
ports:
|
||||
- "13306:3306"
|
||||
volumes:
|
||||
- mysql-data:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-proot123"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
start_period: 60s
|
||||
|
||||
redis:
|
||||
image: redis:7.0
|
||||
container_name: go-sms-redis
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- redis-server
|
||||
- --appendonly
|
||||
- "yes"
|
||||
- --appendfsync
|
||||
- everysec # Day54 R1 评审议题2前置①:显式 AOF everysec(丢 1s 容忍三前置之一;默认即 everysec,显式防漂移)
|
||||
ports:
|
||||
- "16379:6379"
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
|
||||
# ---- kafka 服务已于 2026-09-11 停用(见文件头说明;恢复时取消注释并修 QUORUM_VOTERS/端口) ----
|
||||
# kafka:
|
||||
# image: apache/kafka:3.7.0
|
||||
# container_name: go-sms-kafka
|
||||
# restart: unless-stopped
|
||||
# environment:
|
||||
# # ---- KRaft 单节点模式(无 ZooKeeper)----
|
||||
# CLUSTER_ID: "5L6g3nShT-eMCtK--X86sw"
|
||||
# KAFKA_NODE_ID: 1
|
||||
# KAFKA_PROCESS_ROLES: broker,controller
|
||||
# KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:29093
|
||||
# # ---- 监听器:单一 listener,advertise 宿主机地址 ----
|
||||
# # 消费者组需经 metadata 返回的 endpoint 连接 Coordinator,
|
||||
# # 故统一广播 127.0.0.1:19092(宿主机端口映射 19092 -> 29092)
|
||||
# KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:29092,CONTROLLER://0.0.0.0:29093
|
||||
# KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:19092
|
||||
# KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT
|
||||
# KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
|
||||
# KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
|
||||
# # ---- 单节点副本数必须为 1 ----
|
||||
# KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
||||
# KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
|
||||
# KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
|
||||
# KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
||||
# KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
|
||||
# KAFKA_LOG_DIRS: /var/lib/kafka/data
|
||||
# ports:
|
||||
# - "19092:29092"
|
||||
# volumes:
|
||||
# - kafka-data:/var/lib/kafka/data
|
||||
# healthcheck:
|
||||
# test: ["CMD-SHELL", "bash -c '</dev/tcp/127.0.0.1/29092' || exit 1"]
|
||||
# interval: 10s
|
||||
# timeout: 5s
|
||||
# retries: 30
|
||||
# start_period: 60s
|
||||
|
||||
# ---- 监控告警栈(Day 53 · runbook §6.2 P0) ----
|
||||
# Prometheus 抓宿主网关 /metrics(host.docker.internal:host-gateway 映射);
|
||||
# Alertmanager webhook 回推网关 /api/v1/alerts/sink(日志留痕验收抓手)。
|
||||
# 宿主端口 19090/19093(避开常用 9090/9093 冲突)。
|
||||
prometheus:
|
||||
image: prom/prometheus:v2.53.0
|
||||
container_name: go-sms-prometheus
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- --config.file=/etc/prometheus/prometheus.yml
|
||||
- --storage.tsdb.retention.time=7d
|
||||
volumes:
|
||||
- ./deploy/monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
|
||||
- ./deploy/monitoring/alert_rules.yml:/etc/prometheus/alert_rules.yml:ro
|
||||
ports:
|
||||
- "19090:9090"
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
|
||||
alertmanager:
|
||||
image: prom/alertmanager:v0.27.0
|
||||
container_name: go-sms-alertmanager
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./deploy/monitoring/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
|
||||
ports:
|
||||
- "19093:9093"
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
|
||||
etcd:
|
||||
image: quay.io/coreos/etcd:v3.5.15
|
||||
container_name: go-sms-etcd
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
ETCD_NAME: etcd0
|
||||
ETCD_ADVERTISE_CLIENT_URLS: http://127.0.0.1:2379
|
||||
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
|
||||
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
|
||||
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://127.0.0.1:2380
|
||||
ETCD_INITIAL_CLUSTER: etcd0=http://127.0.0.1:2380
|
||||
ETCD_INITIAL_CLUSTER_STATE: new
|
||||
ETCD_INITIAL_CLUSTER_TOKEN: go-sms-etcd
|
||||
ETCD_AUTO_COMPACTION_RETENTION: "1"
|
||||
command: ["etcd", "--data-dir=/etcd-data"]
|
||||
ports:
|
||||
- "12379:2379"
|
||||
volumes:
|
||||
- etcd-data:/etcd-data
|
||||
healthcheck:
|
||||
test: ["CMD", "etcdctl", "endpoint", "health", "--endpoints=127.0.0.1:2379"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
start_period: 15s
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2024-06-13T22-53-53Z
|
||||
container_name: go-sms-minio
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
MINIO_ROOT_USER: go-sms
|
||||
MINIO_ROOT_PASSWORD: go-sms123456
|
||||
command: ["server", "/data", "--console-address", ":9001"]
|
||||
ports:
|
||||
- "19010:9000" # S3 API
|
||||
- "19011:9001" # Web Console
|
||||
volumes:
|
||||
- minio-data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/live"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
start_period: 15s
|
||||
|
||||
clickhouse:
|
||||
image: clickhouse/clickhouse-server:24.3
|
||||
container_name: go-sms-clickhouse
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
CLICKHOUSE_DB: gosms
|
||||
CLICKHOUSE_USER: gosms
|
||||
CLICKHOUSE_PASSWORD: gosms123
|
||||
ports:
|
||||
- "18123:8123" # HTTP
|
||||
- "19000:9000" # Native
|
||||
volumes:
|
||||
- clickhouse-data:/var/lib/clickhouse
|
||||
ulimits:
|
||||
nofile:
|
||||
soft: 262144
|
||||
hard: 262144
|
||||
healthcheck:
|
||||
test: ["CMD", "clickhouse-client", "--user", "gosms", "--password", "gosms123", "--query", "SELECT 1"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
start_period: 30s
|
||||
|
||||
jaeger:
|
||||
image: jaegertracing/all-in-one:1.57
|
||||
container_name: go-sms-jaeger
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
COLLECTOR_OTLP_ENABLED: "true" # 启用 OTLP 接收(4318)
|
||||
ports:
|
||||
- "16686:16686" # Jaeger UI
|
||||
- "14318:4318" # OTLP HTTP(应用上报)
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1:16686 || exit 1"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
start_period: 15s
|
||||
|
||||
volumes:
|
||||
mysql-data:
|
||||
redis-data:
|
||||
kafka-data:
|
||||
etcd-data:
|
||||
minio-data:
|
||||
clickhouse-data:
|
||||
@@ -0,0 +1,101 @@
|
||||
# 生产环境 Bug 快速定位与修复手册(Incident Response Runbook)
|
||||
|
||||
> 版本:v1.0(2026-09-11,Tory 裁决立项) · 适用:go-sms 生产环境(gateway/前端/中间件)
|
||||
> 原则:**回滚优先于修复;一个 trace_id 讲完整个故事;变更即嫌疑。**
|
||||
> 关联:开发日历 Day51+ 运维基建排期;Day43 修复循环(vite proxy 500)为首发案例。
|
||||
|
||||
## 0. 响应分级与 SLA
|
||||
|
||||
| 级别 | 定义 | 止血 SLA | 修复 SLA | 通知 |
|
||||
|---|---|---|---|---|
|
||||
| **P0** | 全站不可用/资损(发错/超发/重复扣费) | ≤5min 回滚/降级 | ≤2h hotfix | 电话+强提醒 |
|
||||
| **P1** | 核心降级(下发延迟/某通道全失败/消费堆积) | ≤15min 灰度摘除 | ≤24h | 群机器人 |
|
||||
| **P2** | 局部异常有绕行 | 不止血 | 随下版本 | 每日汇总 |
|
||||
| **P3** | 体验/文案 | — | 排期 | — |
|
||||
|
||||
## 1. 可观测性资产清单(定位工具箱)
|
||||
|
||||
| 能力 | 落点 | 用法 |
|
||||
|---|---|---|
|
||||
| zap JSON 日志+**trace_id** | `internal/middleware/logger.go`(status/latency/client_ip) | 按用户/IP/时间窗过滤 trace_id |
|
||||
| OTel→Jaeger | `internal/middleware/tracer.go`(OTLP :14318) | trace_id 查瀑布图(慢在哪段:MySQL/Redis/Kafka/上游) |
|
||||
| Prometheus | `internal/middleware/metrics.go`(GET /metrics) | 5xx 率/QPS/P99 曲线定时间窗 |
|
||||
| 错误码字典 | ISC:xxx + `x-isc-dict`(FE dict.ts 同步) | 用户截图码→grep 后端码点 |
|
||||
| panic 兜底 | `internal/middleware/recovery.go` | panic 留日志不崩进程 |
|
||||
| 健康检查 | `GET /health` | 探针/发布验证 |
|
||||
| 审计三表 | SystemOperateLog / SystemLoginLog / SystemErrorCode | 还原操作现场 |
|
||||
| 慢 SQL | GORM SLOW SQL ≥200ms | DB 层瓶颈 |
|
||||
| 死信队列 | Kafka *.DLQ | 丢消息排查 |
|
||||
| 兜底自愈 | 48h Scanner / 锁 watchdog | 已知设计兜底,非紧急 |
|
||||
| **告警通道(Day53)** | Prometheus(宿主 19090)+Alertmanager(19093)→ webhook `/api/v1/alerts/sink` | 规则:5xx 突增 P0/P99 P1/余额对账差额 P0(R1);grep `alerts_sink_alert_received` 分级 error/warn |
|
||||
| **前端错误上报(Day53)** | fe_error_log 表+POST /fe-errors;FE 三路采集 | 前端层故障感知(DecisionTree 步骤 2 首查);GET /fe-errors?trace_id= 排障 |
|
||||
| **资金域四指标(Day54·R1)** | `go_sms_balance_reconcile_diff{account}`(对账差额,P0 规则已挂 for 1m)/`_mode`(1=redis/0=mysql)/`_fallback_total`(Redis 基础设施错回退计数)/`_flowlog_pending`(异步流水积压) | 预扣异常定界:diff>0→§3 数据修复;fallback 涨→Redis 可用性;pending 持续涨→MySQL 写入瓶颈;diff error 日志 grep `balance_reconcile_diff_detected` 带 runbook 指向 |
|
||||
|
||||
**基建缺口(上线前补齐,见 §6)**:~~告警通道(Alertmanager)~~、~~前端错误上报~~(两项 P0 已于 Day53 落地);日志聚合(Loki)、发布/回滚流水线。
|
||||
|
||||
## 2. 定位 SOP(发现→根因 ≤30min)
|
||||
|
||||
### 步骤 1:定级止血(≤5min)
|
||||
P0 首选回滚镜像/降级开关,而非现场改代码。止血动作记录时间线。
|
||||
|
||||
### 步骤 2:决策树分流
|
||||
```
|
||||
用户报错
|
||||
├─ 有 ISC 码 → grep errors/codes.go 定码点 → trace_id 查日志+Jaeger
|
||||
├─ HTTP 500 无响应体 → 查 gateway 日志有无该请求:
|
||||
│ · 无 → 代理/CDN/前端层(看 fe-errors 上报;Day43 案例:vite proxy 死端口)
|
||||
│ · 有 → trace_id 对应日志+span
|
||||
├─ 慢/超时 → Jaeger 瀑布图定位红段(DB/Redis/Kafka/CMPP 上游)
|
||||
├─ 数据不一致 → 审计表还原操作序列 + DLQ 查丢消息
|
||||
└─ 状态机卡死 → message_task.status/audit_status 快照 + Scanner 日志
|
||||
```
|
||||
|
||||
### 步骤 3:取证命令
|
||||
```bash
|
||||
kubectl logs <pod> --since=30m | grep <trace_id> # 单实例
|
||||
logcli query '{app="gateway"} |= "<trace_id>"' # Loki 跨实例(基建后)
|
||||
# Jaeger UI 粘贴 trace_id → span 树;MySQL 慢日志+SHOW PROCESSLIST
|
||||
```
|
||||
|
||||
### 步骤 4:根因确认
|
||||
1. **变更回溯**:`git log $(git describe --tags --abbrev=0 HEAD~1)..HEAD --oneline` 对照故障时间
|
||||
2. **复现**:staging 同参重放(Day48 全链路环境复用)
|
||||
3. **修复**:先写回归单测钉死 Bug(如 submit_test.go 400/402 例),再改代码
|
||||
|
||||
## 3. 修复流程
|
||||
|
||||
### P0/P1 hotfix(≤2h 上线)
|
||||
```
|
||||
生产 tag → hotfix/vN.x 分支 → 修复+回归单测(CI 不豁免)
|
||||
→ CR 一人预授权放行(24h 内补评审) → 发布 → /health+核心 API 验证
|
||||
→ 回合并 develop + CHANGELOG 补记
|
||||
```
|
||||
|
||||
### 数据修复专项(最高危)
|
||||
- 订正 SQL:SELECT 快照留档→影响行数预估→双人复核→事务执行→审计留痕
|
||||
- 批量订正写幂等脚本入 `scripts/fixes/`(编号+日期),禁手工裸 SQL
|
||||
- 退费复用 48h Scanner refund 流水模式(BalanceAfter 回填),对账可解释
|
||||
|
||||
## 4. 防复发
|
||||
|
||||
1. **季度演练**:杀 MySQL 主库/灌满 Kafka/通道超时,验证告警 5min 触达+回滚 15min 完成
|
||||
2. **错误码预算**:新错误路径先注册 ISC 码+字典同步(现 51 码)
|
||||
3. **发版检查单**:门禁全绿→迁移向前兼容(只加不改删,回滚代码不回滚 schema)→回滚点确认→告警规则刷新→值守到位
|
||||
4. **复盘模板**:时间线/根因/为什么没拦住(测试?监控?评审?)/改进项挂账进开发日历
|
||||
|
||||
## 5. 历史案例索引
|
||||
|
||||
| 日期 | 案例 | 根因 | 教训 |
|
||||
|---|---|---|---|
|
||||
| 2026-09-11 | batch estimate 500(一轮 W3) | vite proxy 指死端口 8081(历史 env 覆盖残留) | proxy/env 仅启动生效,改后必重启;前端代理层故障后端零感知→催生 §6.4 |
|
||||
| 2026-09-11 | real 模式 login 500(二轮前) | 同上,旧 vite 进程内存残留配置 | 同上;配置变更须纳入发布检查单 |
|
||||
| 2026-09-11 | go-sms-kafka 重启循环 1271 次(Day47 开工前处置) | 容器脱离 go-sms-dev_default 网络→KRaft QUORUM_VOTERS(1@kafka:29093)DNS 解析失败→broker 永不就绪;且与外部 hwsms-kafka 抢宿主 19092 | 同端口双实例=隐性单点+冲突源;容器脱离网络属"僵尸态",compose 状态与 runtime 漂移要 `docker inspect NetworkSettings` 核对;已停用(docker update --restart=no+stop)并注释 compose 服务;**2026-09-12 容器更名统一**:僵尸尸体删除腾名,hwsms-kafka→go-sms-kafka(手动容器,显式别名 kafka/宿主 19092/网关 127.0.0.1:19092 连接均不变,rename 后 TCP/DNS/重启安全已验证);**2026-09-13 遗留清零**:①kind 集群删旧建新 hwsms-dev→go-sms-dev(清 CrashLoop 2147 次废弃 hwsms-gateway deployment,清早期 hwsms→go-sms 更名挂账,context=kind-go-sms-dev,与 deployments/ namespace 对齐);②kafka 迁网 hwsms-dev_default→go-sms-dev_default(先 connect 带别名后 disconnect 零空窗;旧网已删;healthy/restarts=0/host 网络容器 topic list 验证过);③容器内 CLI 连 19092 不通=advertised 宿主观设计,勿误判故障 |
|
||||
|
||||
## 6. 上线前基建补齐清单(6 工作日,排期见开发日历)
|
||||
|
||||
| # | 项 | 工作量 | 优先级 |
|
||||
|---|---|---|---|
|
||||
| 6.1 | Loki+Promtail+Grafana 日志聚合(索引 trace_id/code/status) | 2d | P1 |
|
||||
| 6.2 | ~~Alertmanager→钉钉/企微:5xx 突增(P0)/P99>500ms(P1)/Kafka lag>1w(P1)/通道速率异常(P0 资损)~~ **✅ Day53 落地**:监控栈+规则+webhook sink 真机验通;Kafka lag/通道速率两指标导出挂账 Day55/56;生产切换钉钉/企微须加 dingtalk 侧车 | 1d | **P0** |
|
||||
| 6.3 | Gitea release 流水线:tag→测试→镜像(git describe)→staging 冒烟→生产滚动;一键回滚 | 2d | P0 |
|
||||
| 6.4 | ~~前端错误上报:request.ts 拦截器+window.onerror → POST /fe-errors 落库(url/ua/trace_id)~~ **✅ Day53 落地**:v12 迁移+双端点+FE 三路采集(keepalive/去重/防递归) | 1d | **P0** |
|
||||
@@ -0,0 +1,159 @@
|
||||
module go-sms
|
||||
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.25.0
|
||||
github.com/IBM/sarama v1.43.3
|
||||
github.com/alicebob/miniredis/v2 v2.33.0
|
||||
github.com/casbin/casbin/v2 v2.100.0
|
||||
github.com/casbin/gorm-adapter/v3 v3.22.0
|
||||
github.com/coocood/freecache v1.2.4
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/glebarez/sqlite v1.11.0
|
||||
github.com/go-gormigrate/gormigrate/v2 v2.1.1
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/minio/minio-go/v7 v7.3.0
|
||||
github.com/prometheus/client_golang v1.19.1
|
||||
github.com/redis/go-redis/v9 v9.5.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
github.com/xuri/excelize/v2 v2.11.0
|
||||
go.etcd.io/etcd/client/v3 v3.5.15
|
||||
go.opentelemetry.io/otel v1.44.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
|
||||
go.opentelemetry.io/otel/sdk v1.44.0
|
||||
go.opentelemetry.io/otel/trace v1.44.0
|
||||
go.uber.org/zap v1.28.0
|
||||
golang.org/x/crypto v0.55.0
|
||||
golang.org/x/text v0.41.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/gorm v1.30.0
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/ClickHouse/ch-go v0.61.5 // indirect
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
|
||||
github.com/andybalholm/brotli v1.2.2 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/casbin/govaluate v1.2.0 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/eapache/go-resiliency v1.7.0 // indirect
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
|
||||
github.com/eapache/queue v1.1.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/go-faster/city v1.0.1 // indirect
|
||||
github.com/go-faster/errors v0.7.1 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/hashicorp/go-uuid v1.0.3 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/gofork v1.7.6 // indirect
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/magiconair/properties v1.8.10 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/microsoft/go-mssqldb v1.6.0 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/paulmach/orb v0.13.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.3.1 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.27 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_model v0.5.0 // indirect
|
||||
github.com/prometheus/common v0.48.0 // indirect
|
||||
github.com/prometheus/procfs v0.12.0 // indirect
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/richardlehane/mscfb v1.0.7 // indirect
|
||||
github.com/richardlehane/msoleps v1.0.6 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/segmentio/asm v1.2.1 // indirect
|
||||
github.com/shopspring/decimal v1.4.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tiendc/go-deepcopy v1.7.2 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/xuri/efp v0.0.1 // indirect
|
||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
|
||||
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.44.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/net v0.58.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
|
||||
google.golang.org/grpc v1.64.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
gorm.io/driver/postgres v1.5.7 // indirect
|
||||
gorm.io/driver/sqlserver v1.5.3 // indirect
|
||||
gorm.io/plugin/dbresolver v1.6.2 // indirect
|
||||
modernc.org/libc v1.22.5 // indirect
|
||||
modernc.org/mathutil v1.5.0 // indirect
|
||||
modernc.org/memory v1.5.0 // indirect
|
||||
modernc.org/sqlite v1.23.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,487 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.1/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1 h1:/iHxaJhsFr0+xVFfbMr5vxz848jyiWuIEDhYq3y5odY=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 h1:vcYCAze6p19qBW7MhZybIsqD8sMV8js0NyQM8JDnVtg=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0/go.mod h1:OQeznEEkTZ9OrhHJoDD8ZDq51FHgXjqtP9z6bEwBq9U=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInmMgOsuGwdjjVkEIde0OtY=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.0 h1:yfJe15aSwEQ6Oo6J+gdfdulPNoZ3TEhmbhLIoxZcA+U=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.0/go.mod h1:Q28U+75mpCaSCDowNEmhIo/rmgdkqmkmzI7N6TGR4UY=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v0.8.0 h1:T028gtTPiYt/RMUfs8nVsAL7FDQrfLlrm/NnRG/zcC4=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v0.8.0/go.mod h1:cw4zVQgBby0Z5f2v0itn6se2dDP17nTjbZFXW5uPyHA=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.0 h1:HCc0+LpPfpCKs6LGGLAhwBARt9632unrVcI6i8s/8os=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.0/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
|
||||
github.com/ClickHouse/ch-go v0.61.5 h1:zwR8QbYI0tsMiEcze/uIMK+Tz1D3XZXLdNrlaOpeEI4=
|
||||
github.com/ClickHouse/ch-go v0.61.5/go.mod h1:s1LJW/F/LcFs5HJnuogFMta50kKDO0lf9zzfrbl0RQg=
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.25.0 h1:rKscwqgQHzWBTZySZDcHKxgs0Ad+xFULfZvo26W5UlY=
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.25.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ=
|
||||
github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
|
||||
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
|
||||
github.com/agiledragon/gomonkey/v2 v2.2.0 h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI=
|
||||
github.com/agiledragon/gomonkey/v2 v2.2.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
|
||||
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
|
||||
github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM=
|
||||
github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
|
||||
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/casbin/casbin/v2 v2.100.0 h1:aeugSNjjHfCrgA22nHkVvw2xsscboHv5r0a13ljQKGQ=
|
||||
github.com/casbin/casbin/v2 v2.100.0/go.mod h1:LO7YPez4dX3LgoTCqSQAleQDo0S0BeZBDxYnPUl95Ng=
|
||||
github.com/casbin/gorm-adapter/v3 v3.22.0 h1:VM3ihZyM7YSi9FIWYRbYMMGG679fP4CjSZ2K9mBbiQk=
|
||||
github.com/casbin/gorm-adapter/v3 v3.22.0/go.mod h1:z3kcgwp4og8xlFNAwMnapBEI5cxhsbmt2UWN6V66wuk=
|
||||
github.com/casbin/govaluate v1.2.0 h1:wXCXFmqyY+1RwiKfYo3jMKyrtZmOL3kHwaqDyCPOYak=
|
||||
github.com/casbin/govaluate v1.2.0/go.mod h1:G/UnbIjZk/0uMNaLwZZmFQrR72tYRZWQkO70si/iR7A=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M=
|
||||
github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
|
||||
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
|
||||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
|
||||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
|
||||
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/eapache/go-resiliency v1.7.0 h1:n3NRTnBn5N0Cbi/IeOHuQn9s2UwVUH7Ga0ZWcP+9JTA=
|
||||
github.com/eapache/go-resiliency v1.7.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws=
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3/go.mod h1:YvSRo5mw33fLEx1+DlK6L2VV43tJt5Eyel9n9XBcR+0=
|
||||
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
|
||||
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
|
||||
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
|
||||
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
||||
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
|
||||
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
|
||||
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
|
||||
github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo=
|
||||
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
|
||||
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
||||
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
|
||||
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
|
||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
|
||||
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo=
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0/go.mod h1:b0TnjGOvI/n42bZa+hmXL+kFJZsFT7G4t3HTlQ184QM=
|
||||
github.com/jcmturner/gofork v1.7.6 h1:QH0l3hzAU1tfT3rZCnW5zXl+orbkNMMRGJfdJjHVETg=
|
||||
github.com/jcmturner/gofork v1.7.6/go.mod h1:1622LH6i/EZqLloHfE7IeZ0uEJwMSUyQ/nDd82IeqRo=
|
||||
github.com/jcmturner/goidentity/v6 v6.0.1 h1:VKnZd2oEIMorCTsFBnJWbExfNN7yZr3EhJAxwOkZg6o=
|
||||
github.com/jcmturner/goidentity/v6 v6.0.1/go.mod h1:X1YW3bgtvwAXju7V3LCIMpY0Gbxyjn/mY9zx4tFonSg=
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh687T8=
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
|
||||
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
|
||||
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/microsoft/go-mssqldb v1.6.0 h1:mM3gYdVwEPFrlg/Dvr2DNVEgYFG7L42l+dGc67NNNpc=
|
||||
github.com/microsoft/go-mssqldb v1.6.0/go.mod h1:00mDtPbeQCRGC1HwOOR5K/gr30P1NcEG0vx6Kbv2aJU=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.3.0 h1:HM4pFCSQq/TK+j0/zmorSh5ddh81iDgRgU0BG0Vz/YU=
|
||||
github.com/minio/minio-go/v7 v7.3.0/go.mod h1:KUPWdecEO1LWyUz+sTGXAuf2jZHrPh5fCsRH86QbPfk=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
|
||||
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/paulmach/orb v0.13.0 h1:r7n7mQGGF+cj/CbcivEj9J3HGK+XR+yXnvzRdq9saIw=
|
||||
github.com/paulmach/orb v0.13.0/go.mod h1:6scRWINywA2Jf05dcjOfLfxrUIMECvTSG2MVbRLxu/k=
|
||||
github.com/pelletier/go-toml/v2 v2.3.1 h1:MYEvvGnQjeNkRF1qUuGolNtNExTDwct51yp7olPtrEc=
|
||||
github.com/pelletier/go-toml/v2 v2.3.1/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pierrec/lz4/v4 v4.1.27 h1:+PhzhWDrjRj89TH2sw43nE3+4+W8lSxIuQadEHZyjUk=
|
||||
github.com/pierrec/lz4/v4 v4.1.27/go.mod h1:EoQMVJgeeEOMsCqCzqFm2O0cJvljX2nGZjcRIPL34O4=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
|
||||
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
|
||||
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
|
||||
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
|
||||
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
|
||||
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
|
||||
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
|
||||
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
|
||||
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/richardlehane/mscfb v1.0.7 h1:oeoiM0WE79vHwE8RpIYYvIAc8ajTH2mb6UZm55/+EB0=
|
||||
github.com/richardlehane/mscfb v1.0.7/go.mod h1:pe0+IUIc0AHh0+teNzBlJCtSyZdFOGgV4ZK9bsoV+Jo=
|
||||
github.com/richardlehane/msoleps v1.0.6 h1:9BvkpjvD+iUBalUY4esMwv6uBkfOip/Lzvd93jvR9gg=
|
||||
github.com/richardlehane/msoleps v1.0.6/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
|
||||
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/tiendc/go-deepcopy v1.7.2 h1:Ut2yYR7W9tWjTQitganoIue4UGxZwCcJy3orjrrIj44=
|
||||
github.com/tiendc/go-deepcopy v1.7.2/go.mod h1:4bKjNC2r7boYOkD2IOuZpYjmlDdzjbpTRyCx+goBCJQ=
|
||||
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
|
||||
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
|
||||
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||
github.com/xuri/excelize/v2 v2.11.0 h1:HxaEFl6sRN2+8J5a8HaKq+0M4FsjBGMnWWtjOCPSG88=
|
||||
github.com/xuri/excelize/v2 v2.11.0/go.mod h1:jxFLbzaIwGQ5ufFNvYfUOHqXhfPaNmP14KWfmNz2Uak=
|
||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
|
||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 h1:3KpLJir1ZEBrYuV2v+Twaa/e2MdDCEZ/70H+lzEiwsk=
|
||||
go.etcd.io/etcd/api/v3 v3.5.15/go.mod h1:N9EhGzXq58WuMllgH9ZvnEr7SI9pS0k0+DHZezGp7jM=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 h1:fo0HpWz/KlHGMCC+YejpiCmyWDEuIpnTDzpJLB5fWlA=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15/go.mod h1:mXDI4NAOwEiszrHCb0aqfAYNCrZP4e9hRca3d1YK8EU=
|
||||
go.etcd.io/etcd/client/v3 v3.5.15 h1:23M0eY4Fd/inNv1ZfU3AxrbbOdW79r9V9Rl62Nm6ip4=
|
||||
go.etcd.io/etcd/client/v3 v3.5.15/go.mod h1:CLSJxrYjvLtHsrPKsy7LmZEE+DK2ktfd2bN4RhBMwlU=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU=
|
||||
go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 h1:j9+03ymgYhPKmeXGk5Zu+cIZOlVzd9Zv7QIiyItjFBU=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0/go.mod h1:Y5+XiUG4Emn1hTfciPzGPJaSI+RpDts6BnCIir0SLqk=
|
||||
go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc=
|
||||
go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo=
|
||||
go.opentelemetry.io/otel/sdk v1.44.0 h1:nHYwb9lK+fJPU/dnT6s7W7Z8itMWyqrnVfbheVYrZ58=
|
||||
go.opentelemetry.io/otel/sdk v1.44.0/go.mod h1:Osuydd3Se74nqjAKxid74N5eC+jfEqfTegHRnq58oK0=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRks6si09iEfI=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA=
|
||||
go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk=
|
||||
go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.28.0 h1:IZzaP1Fv73/T/pBMLk4VutPl36uNC+OSUh3JLG3FIjo=
|
||||
go.uber.org/zap v1.28.0/go.mod h1:rDLpOi171uODNm/mxFcuYWxDsqWSAVkFdX4XojSKg/Q=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||
golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M=
|
||||
golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE=
|
||||
golang.org/x/image v0.38.0/go.mod h1:/3f6vaXC+6CEanU4KJxbcUZyEePbyKbaLoDOe4ehFYY=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
|
||||
golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
|
||||
golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
|
||||
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
|
||||
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
|
||||
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
|
||||
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
|
||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
|
||||
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
|
||||
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
|
||||
gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
|
||||
gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
|
||||
gorm.io/driver/sqlserver v1.5.3 h1:rjupPS4PVw+rjJkfvr8jn2lJ8BMhT4UW5FwuJY0P3Z0=
|
||||
gorm.io/driver/sqlserver v1.5.3/go.mod h1:B+CZ0/7oFJ6tAlefsKoyxdgDCXJKSgwS2bMOQZT0I00=
|
||||
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
|
||||
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||
gorm.io/plugin/dbresolver v1.6.2 h1:F4b85TenghUeITqe3+epPSUtHH7RIk3fXr5l83DF8Pc=
|
||||
gorm.io/plugin/dbresolver v1.6.2/go.mod h1:tctw63jdrOezFR9HmrKnPkmig3m5Edem9fdxk9bQSzM=
|
||||
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
|
||||
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
|
||||
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
|
||||
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
@@ -0,0 +1,307 @@
|
||||
// Package app 应用启动器(Day 12):统一装配 配置/日志/DB/Redis/Kafka/追踪/HTTP,
|
||||
// 提供配置热更新(ETCD watch)与优雅停机(反向依序释放)。
|
||||
// 组件连接失败采用降级策略(Warn 并继续),保障 /health 始终可用。
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/config"
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/pkg/cache"
|
||||
"go-sms/internal/pkg/logger"
|
||||
"go-sms/internal/pkg/metrics"
|
||||
"go-sms/internal/pkg/queue"
|
||||
"go-sms/internal/repository/mysql"
|
||||
)
|
||||
|
||||
// Options 启动选项。
|
||||
type Options struct {
|
||||
ConfigPath string // 配置文件路径(空则走默认解析规则)
|
||||
ServiceName string // 服务名(tracer/日志标识)
|
||||
Version string // 构建注入版本
|
||||
Commit string // 构建注入提交
|
||||
RegisterRoutes func(r *gin.Engine, a *App) // 业务路由钩子(阶段 2+;可经 a 取 DB 等依赖)
|
||||
}
|
||||
|
||||
// App 应用实例:持有全部组件句柄,负责生命周期。
|
||||
type App struct {
|
||||
cfg *config.Config
|
||||
lg *zap.Logger
|
||||
httpSrv *http.Server
|
||||
db *gorm.DB // nil = 降级
|
||||
redis *cache.Client // nil = 降级
|
||||
producer *queue.Producer // nil = 降级
|
||||
etcdCli *clientv3.Client
|
||||
watchCancel context.CancelFunc
|
||||
tracerShutdown func(context.Context) error
|
||||
|
||||
mu sync.Mutex // 保护 shutdownHooks
|
||||
shutdownHooks []func() // 停机钩子(Day 27:定时任务随优雅停机退出)
|
||||
}
|
||||
|
||||
// New 装配应用:配置 → 日志 → 追踪 → DB/Redis/Kafka(降级)→ HTTP → 热更新。
|
||||
func New(opts Options) (*App, error) {
|
||||
cfg, err := config.Load(opts.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := logger.Init(logger.Options{
|
||||
Level: cfg.Log.Level, Format: cfg.Log.Format, Output: cfg.Log.Output,
|
||||
File: logger.FileOptions{
|
||||
Path: cfg.Log.File.Path, MaxSizeMB: cfg.Log.File.MaxSizeMB,
|
||||
MaxAgeDays: cfg.Log.File.MaxAgeDays, MaxBackups: cfg.Log.File.MaxBackups,
|
||||
Compress: cfg.Log.File.Compress,
|
||||
},
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("app: 初始化日志失败: %w", err)
|
||||
}
|
||||
lg := logger.L()
|
||||
a := &App{cfg: cfg, lg: lg}
|
||||
lg.Info("app_assembling",
|
||||
zap.String("service", opts.ServiceName), zap.String("version", opts.Version),
|
||||
zap.String("config_source", cfg.Source))
|
||||
|
||||
// 链路追踪(失败降级)
|
||||
if cfg.Tracer.Enabled {
|
||||
if sd, err := middleware.InitTracer(context.Background(), cfg.Tracer.OTLPEndpoint, cfg.Tracer.ServiceName); err != nil {
|
||||
lg.Warn("tracer_init_failed_degraded", zap.Error(err))
|
||||
} else {
|
||||
a.tracerShutdown = sd
|
||||
}
|
||||
}
|
||||
|
||||
// MySQL(失败降级)
|
||||
if db, err := mysql.Open(cfg.MySQL.DSN, mysql.PoolOptions{
|
||||
MaxOpenConns: cfg.MySQL.MaxOpenConns, MaxIdleConns: cfg.MySQL.MaxIdleConns,
|
||||
ConnMaxLifetime: cfg.MySQL.ConnMaxLifetime, SlowThresholdMs: cfg.MySQL.SlowThresholdMs,
|
||||
}); err != nil {
|
||||
lg.Warn("mysql_init_failed_degraded", zap.Error(err))
|
||||
} else {
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
if err := mysql.Ping(pingCtx, db); err != nil {
|
||||
lg.Warn("mysql_ping_failed_degraded", zap.Error(err))
|
||||
} else {
|
||||
lg.Info("mysql_connected", zap.String("dsn", maskDSN(cfg.MySQL.DSN)))
|
||||
}
|
||||
cancel()
|
||||
a.db = db
|
||||
}
|
||||
|
||||
// Redis(失败降级)
|
||||
if rc, err := cache.New(cache.Options{
|
||||
Mode: cfg.Redis.Mode, Addrs: cfg.Redis.Addrs, Password: cfg.Redis.Password,
|
||||
DB: cfg.Redis.DB, PoolSize: cfg.Redis.PoolSize,
|
||||
}); err != nil {
|
||||
lg.Warn("redis_init_failed_degraded", zap.Error(err))
|
||||
} else {
|
||||
a.redis = rc
|
||||
lg.Info("redis_connected", zap.Strings("addrs", cfg.Redis.Addrs), zap.String("mode", cfg.Redis.Mode))
|
||||
}
|
||||
|
||||
// Kafka 生产者(失败降级)
|
||||
if p, err := queue.NewProducer(queue.ProducerConfig{Brokers: cfg.Kafka.Brokers, Logger: lg}); err != nil {
|
||||
lg.Warn("kafka_producer_init_failed_degraded", zap.Error(err))
|
||||
} else {
|
||||
a.producer = p
|
||||
lg.Info("kafka_producer_connected", zap.Strings("brokers", cfg.Kafka.Brokers))
|
||||
}
|
||||
|
||||
// HTTP 引擎:请求日志 → 限流 → Recovery → /health(+业务路由)
|
||||
if os.Getenv("GIN_MODE") == "" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.New()
|
||||
engine.Use(middleware.Logger(lg), middleware.Recovery(lg), middleware.Metrics())
|
||||
if a.tracerShutdown != nil {
|
||||
engine.Use(middleware.Tracer(cfg.Tracer.ServiceName))
|
||||
}
|
||||
if cfg.RateLimit.Enabled && a.redis != nil {
|
||||
rlCfg := middleware.RateLimitConfig{Capacity: cfg.RateLimit.Capacity, Rate: cfg.RateLimit.RatePerSec}
|
||||
if cfg.RateLimit.Key == "ip" {
|
||||
rlCfg.KeyFunc = middleware.ByClientIP
|
||||
}
|
||||
engine.Use(middleware.RateLimit(a.redis, rlCfg))
|
||||
}
|
||||
middleware.HealthRegister(engine)
|
||||
// /metrics:Prometheus 抓取端点(生产建议集群内访问/加白名单)
|
||||
engine.GET("/metrics", gin.WrapH(metrics.Handler()))
|
||||
if opts.RegisterRoutes != nil {
|
||||
opts.RegisterRoutes(engine, a)
|
||||
}
|
||||
a.httpSrv = &http.Server{
|
||||
Addr: cfg.Server.HTTP.Addr, Handler: engine,
|
||||
ReadTimeout: cfg.Server.HTTP.ReadTimeout, WriteTimeout: cfg.Server.HTTP.WriteTimeout,
|
||||
}
|
||||
|
||||
// 配置热更新(ETCD watch;失败降级)
|
||||
if cfg.HotReload.Enabled {
|
||||
if cli, err := clientv3.New(clientv3.Config{Endpoints: cfg.ETCD.Endpoints, DialTimeout: 5 * time.Second}); err != nil {
|
||||
lg.Warn("etcd_init_failed_hotreload_degraded", zap.Error(err))
|
||||
} else {
|
||||
a.etcdCli = cli
|
||||
w := config.NewETCDWatcher(cli, cfg.HotReload.Prefix, cfg.Source, a.onConfigReload, lg)
|
||||
wctx, wcancel := context.WithCancel(context.Background())
|
||||
a.watchCancel = wcancel
|
||||
go func() {
|
||||
if err := w.Run(wctx); err != nil {
|
||||
lg.Warn("etcd_watcher_exited", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
lg.Info("hotreload_enabled", zap.String("prefix", cfg.HotReload.Prefix), zap.Strings("etcd", cfg.ETCD.Endpoints))
|
||||
}
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// onConfigReload 热更新回调:日志级别即时生效;其余项更新内存配置(连接类需重启)。
|
||||
func (a *App) onConfigReload(cfg *config.Config) {
|
||||
if level, err := zap.ParseAtomicLevel(cfg.Log.Level); err == nil {
|
||||
logger.Level().SetLevel(level.Level())
|
||||
}
|
||||
a.cfg = cfg
|
||||
}
|
||||
|
||||
// DB 返回 MySQL 连接(可能为 nil:降级态;业务装配前判空)。
|
||||
func (a *App) DB() *gorm.DB { return a.db }
|
||||
|
||||
// Redis 返回 Redis 客户端(可能为 nil:降级态;业务装配前判空)。
|
||||
func (a *App) Redis() *cache.Client { return a.redis }
|
||||
|
||||
// Config 返回当前配置(热更新后为最新)。
|
||||
func (a *App) Config() *config.Config { return a.cfg }
|
||||
|
||||
// Lg 返回全局日志器(业务装配/降级告警用)。
|
||||
func (a *App) Lg() *zap.Logger { return a.lg }
|
||||
|
||||
// Producer Day 46:Kafka 生产者(可 nil=未装配降级;调用方自行判空)。
|
||||
func (a *App) Producer() *queue.Producer { return a.producer }
|
||||
|
||||
// OnShutdown 注册停机钩子(注册顺序执行;定时任务/后台协程随优雅停机退出)。
|
||||
func (a *App) OnShutdown(fn func()) {
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
a.shutdownHooks = append(a.shutdownHooks, fn)
|
||||
}
|
||||
|
||||
// Run 启动 HTTP 并阻塞直至 SIGINT/SIGTERM,随后优雅停机。
|
||||
func (a *App) Run() error {
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
a.lg.Info("http_server_started", zap.String("addr", a.cfg.Server.HTTP.Addr), zap.String("endpoint", "GET /health"))
|
||||
if err := a.httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
return fmt.Errorf("app: HTTP 服务异常: %w", err)
|
||||
case sig := <-quit:
|
||||
a.lg.Info("shutdown_signal_received", zap.Stringer("signal", sig))
|
||||
}
|
||||
a.Shutdown(10 * time.Second)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown 优雅停机:HTTP 等待在途请求 → 停热更新 → 逆序释放组件 → 日志落盘。
|
||||
func (a *App) Shutdown(grace time.Duration) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), grace)
|
||||
defer cancel()
|
||||
if err := a.httpSrv.Shutdown(ctx); err != nil {
|
||||
a.lg.Error("http_shutdown_failed", zap.Error(err))
|
||||
} else {
|
||||
a.lg.Info("http_server_stopped")
|
||||
}
|
||||
if a.watchCancel != nil {
|
||||
a.watchCancel()
|
||||
a.lg.Info("hotreload_stopped")
|
||||
}
|
||||
// 业务停机钩子(定时任务等;注册序执行)
|
||||
a.mu.Lock()
|
||||
hooks := append([]func(){}, a.shutdownHooks...)
|
||||
a.mu.Unlock()
|
||||
for _, fn := range hooks {
|
||||
fn()
|
||||
}
|
||||
if a.producer != nil {
|
||||
if err := a.producer.Close(); err != nil {
|
||||
a.lg.Error("kafka_producer_close_failed", zap.Error(err))
|
||||
} else {
|
||||
a.lg.Info("kafka_producer_closed")
|
||||
}
|
||||
}
|
||||
if a.redis != nil {
|
||||
_ = a.redis.Close()
|
||||
a.lg.Info("redis_closed")
|
||||
}
|
||||
if a.db != nil {
|
||||
if err := mysql.Close(a.db); err != nil {
|
||||
a.lg.Error("mysql_close_failed", zap.Error(err))
|
||||
} else {
|
||||
a.lg.Info("mysql_closed")
|
||||
}
|
||||
}
|
||||
if a.etcdCli != nil {
|
||||
_ = a.etcdCli.Close()
|
||||
}
|
||||
if a.tracerShutdown != nil {
|
||||
if err := a.tracerShutdown(context.Background()); err != nil {
|
||||
a.lg.Warn("tracer_shutdown_failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
logger.Sync()
|
||||
a.lg.Info("app_stopped_gracefully")
|
||||
}
|
||||
|
||||
// maskDSN 密码脱敏。
|
||||
func maskDSN(dsn string) string {
|
||||
for i := 0; i < len(dsn); i++ {
|
||||
if dsn[i] == ':' {
|
||||
for j := i + 1; j < len(dsn); j++ {
|
||||
if dsn[j] == '@' {
|
||||
return dsn[:i+1] + "***" + dsn[j:]
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return dsn
|
||||
}
|
||||
|
||||
// Main 便捷入口// Main 便捷入口:flag 解析 + New + Run(cmd/*/main.go 薄壳)。
|
||||
func Main(opts Options) {
|
||||
configPath := flag.String("config", os.Getenv(config.EnvKeyFile),
|
||||
"配置文件路径(留空则依次取环境变量 GOSMS_CONFIG、configs/config.dev.yaml)")
|
||||
flag.Parse()
|
||||
opts.ConfigPath = *configPath
|
||||
|
||||
a, err := New(opts)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[%s] 启动失败: %v\n", opts.ServiceName, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := a.Run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[%s] 运行异常退出: %v\n", opts.ServiceName, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
// Package config 提供 go-sms 平台统一配置模型与加载能力(Viper + 环境变量覆盖)。
|
||||
// 对应文件:configs/config.dev.yaml;环境变量规则见 loader.go。
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config 平台总配置根结构,与 configs/config.*.yaml 一一对应。
|
||||
type Config struct {
|
||||
// Source 配置文件来源路径(运行时自动填充,yaml 中无需定义)。
|
||||
Source string `mapstructure:"-"`
|
||||
Server Server `mapstructure:"server"`
|
||||
Log Log `mapstructure:"log"`
|
||||
MySQL MySQL `mapstructure:"mysql"`
|
||||
Redis Redis `mapstructure:"redis"`
|
||||
Kafka Kafka `mapstructure:"kafka"`
|
||||
ETCD ETCD `mapstructure:"etcd"`
|
||||
ClickHouse ClickHouse `mapstructure:"clickhouse"`
|
||||
MinIO MinIO `mapstructure:"minio"`
|
||||
Tracer Tracer `mapstructure:"tracer"`
|
||||
JWT JWT `mapstructure:"jwt"`
|
||||
RateLimit RateLimit `mapstructure:"rate_limit"`
|
||||
HotReload HotReload `mapstructure:"hot_reload"`
|
||||
Balance Balance `mapstructure:"balance"`
|
||||
}
|
||||
|
||||
// Balance 资金预扣模式(Day54 R1,评审 docs/review/r1-balance.md):
|
||||
// mysql=行锁强一致(默认,回退路径);redis=Lua 原子扣+异步流水+对账覆写。
|
||||
type Balance struct {
|
||||
Mode string `mapstructure:"mode"` // mysql | redis
|
||||
ReconcileInterval time.Duration `mapstructure:"reconcile_interval"` // 对账周期(评审 ≤5min)
|
||||
}
|
||||
|
||||
// HotReload 配置热更新(Day 12:ETCD watch)。
|
||||
type HotReload struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Prefix string `mapstructure:"prefix"` // 如 /config/go-sms/
|
||||
}
|
||||
|
||||
// JWT 鉴权令牌配置(Day 22)。
|
||||
type JWT struct {
|
||||
Secret string `mapstructure:"secret"`
|
||||
TTL time.Duration `mapstructure:"ttl"`
|
||||
}
|
||||
|
||||
// Tracer 链路追踪配置(OTLP → Jaeger,Day 9)。
|
||||
type Tracer struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
OTLPEndpoint string `mapstructure:"otlp_endpoint"` // 形如 127.0.0.1:4318(OTLP HTTP)
|
||||
ServiceName string `mapstructure:"service_name"`
|
||||
}
|
||||
|
||||
// RateLimit 分布式限流配置(Redis 令牌桶,Day 9)。
|
||||
type RateLimit struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Capacity int64 `mapstructure:"capacity"` // 桶容量(突发)
|
||||
RatePerSec int64 `mapstructure:"rate_per_sec"` // 每秒填充
|
||||
Key string `mapstructure:"key"` // ip | global
|
||||
}
|
||||
|
||||
// Server 服务进程监听配置(HTTP API / CMPP+SMPP 客户接入)。
|
||||
type Server struct {
|
||||
Name string `mapstructure:"name"`
|
||||
HTTP HTTPServer `mapstructure:"http"`
|
||||
CMPP CMPPServer `mapstructure:"cmpp"`
|
||||
SMPP SMPPServer `mapstructure:"smpp"`
|
||||
}
|
||||
|
||||
// HTTPServer HTTP API 监听配置。
|
||||
type HTTPServer struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
||||
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
||||
}
|
||||
|
||||
// CMPPServer CMPP 服务端监听配置(Day 47 客户接入)。
|
||||
type CMPPServer struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
MaxConnsPerAccount int `mapstructure:"max_connections_per_account"`
|
||||
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
||||
WindowSize int `mapstructure:"window_size"`
|
||||
SubmitRatePerConn int `mapstructure:"submit_rate_per_conn"` // Day 62:单链限速 条/s(账号级 0 时回退值)
|
||||
LongSmsWindow time.Duration `mapstructure:"longsms_window"` // Day 71:入向长短信挂起窗(默认 72h,到期不齐失败终态)
|
||||
}
|
||||
|
||||
// SMPPServer SMPP 服务端监听配置(Day 47 客户接入;addr 空=不启用)。
|
||||
type SMPPServer struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
||||
}
|
||||
|
||||
// Log 结构化日志配置(zap + lumberjack,Day 5 实现)。
|
||||
type Log struct {
|
||||
Level string `mapstructure:"level"` // debug | info | warn | error
|
||||
Format string `mapstructure:"format"` // json | console
|
||||
Output []string `mapstructure:"output"` // stdout | file
|
||||
File LogFile `mapstructure:"file"`
|
||||
}
|
||||
|
||||
// LogFile 日志文件滚动切割配置。
|
||||
type LogFile struct {
|
||||
Path string `mapstructure:"path"`
|
||||
MaxSizeMB int `mapstructure:"max_size_mb"`
|
||||
MaxAgeDays int `mapstructure:"max_age_days"`
|
||||
MaxBackups int `mapstructure:"max_backups"`
|
||||
Compress bool `mapstructure:"compress"`
|
||||
}
|
||||
|
||||
// MySQL 业务主库配置(用户/通道/余额/结算等事务数据)。
|
||||
type MySQL struct {
|
||||
DSN string `mapstructure:"dsn"`
|
||||
MaxOpenConns int `mapstructure:"max_open_conns"`
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
||||
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
|
||||
SlowThresholdMs int `mapstructure:"slow_threshold_ms"`
|
||||
}
|
||||
|
||||
// Redis 缓存/分布式锁/限流配置。
|
||||
type Redis struct {
|
||||
Mode string `mapstructure:"mode"` // single | cluster | sentinel
|
||||
Addrs []string `mapstructure:"addrs"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
PoolSize int `mapstructure:"pool_size"`
|
||||
MasterName string `mapstructure:"master_name"` // sentinel 模式使用
|
||||
}
|
||||
|
||||
// Kafka 消息队列配置(发送/回执/上行/统计四类主题)。
|
||||
type Kafka struct {
|
||||
Brokers []string `mapstructure:"brokers"`
|
||||
Topics map[string]string `mapstructure:"topics"`
|
||||
GroupID string `mapstructure:"group_id"`
|
||||
MaxRetry int `mapstructure:"max_retry"`
|
||||
Consumer Consumer `mapstructure:"consumer"` // Day 48:submit 消费域
|
||||
}
|
||||
|
||||
// Consumer submit 消费者配置(Day 48;零值全部缺省)。
|
||||
type Consumer struct {
|
||||
GroupID string `mapstructure:"group_id"` // 消费组(空=go-sms-consumer)
|
||||
ReclaimEvery time.Duration `mapstructure:"reclaim_every"` // 补偿扫描周期(默认 5s)
|
||||
ReclaimBatch int `mapstructure:"reclaim_batch"` // 单轮认领上限(默认 100)
|
||||
BPSampleEvery time.Duration `mapstructure:"bp_sample_every"` // 背压采样周期(默认 10s)
|
||||
Enable bool `mapstructure:"enable"` // 背压/限速总开关
|
||||
OffsetMode string `mapstructure:"offset_mode"` // Day58:起步 offset oldest(默认)|latest;仅新组/offset 重置后生效(env GOSMS_KAFKA_CONSUMER_OFFSET_MODE)
|
||||
PaceEvery int `mapstructure:"pace_every"` // Day58:每成功消费 N 条歇一次(0=不限速)
|
||||
PaceSleep time.Duration `mapstructure:"pace_sleep"` // Day58:每次歇时长(配合 pace_every;dev 存量回放防落库风暴)
|
||||
Workers int `mapstructure:"workers"` // Day65:每分区并行 worker 数(默认 0/1=串行;submit 域慢事务链提吞吐)
|
||||
}
|
||||
|
||||
// ETCD 配置中心 / 服务发现。
|
||||
type ETCD struct {
|
||||
Endpoints []string `mapstructure:"endpoints"`
|
||||
DialTimeout time.Duration `mapstructure:"dial_timeout"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
}
|
||||
|
||||
// ClickHouse 历史明细与统计库。
|
||||
type ClickHouse struct {
|
||||
Addrs []string `mapstructure:"addrs"`
|
||||
Database string `mapstructure:"database"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
DialTimeout time.Duration `mapstructure:"dial_timeout"`
|
||||
}
|
||||
|
||||
// MinIO 对象存储(号码文件/报表/检测结果)。
|
||||
type MinIO struct {
|
||||
Endpoint string `mapstructure:"endpoint"`
|
||||
AccessKey string `mapstructure:"access_key"`
|
||||
SecretKey string `mapstructure:"secret_key"`
|
||||
Bucket string `mapstructure:"bucket"`
|
||||
UseSSL bool `mapstructure:"use_ssl"`
|
||||
}
|
||||
|
||||
// Validate 对关键配置项做基础校验,避免进程带病启动。
|
||||
func (c *Config) Validate() error {
|
||||
if c.Server.HTTP.Addr == "" {
|
||||
return errors.New("server.http.addr 不能为空")
|
||||
}
|
||||
if c.MySQL.DSN == "" {
|
||||
return errors.New("mysql.dsn 不能为空")
|
||||
}
|
||||
switch c.Redis.Mode {
|
||||
case "single", "cluster", "sentinel":
|
||||
default:
|
||||
return fmt.Errorf("redis.mode 非法: %q(可选 single/cluster/sentinel)", c.Redis.Mode)
|
||||
}
|
||||
if len(c.Redis.Addrs) == 0 {
|
||||
return errors.New("redis.addrs 不能为空")
|
||||
}
|
||||
if len(c.Kafka.Brokers) == 0 {
|
||||
return errors.New("kafka.brokers 不能为空")
|
||||
}
|
||||
for _, key := range []string{"submit", "report", "up", "stats"} {
|
||||
if c.Kafka.Topics[key] == "" {
|
||||
return fmt.Errorf("kafka.topics.%s 不能为空", key)
|
||||
}
|
||||
}
|
||||
if len(c.ETCD.Endpoints) == 0 {
|
||||
return errors.New("etcd.endpoints 不能为空")
|
||||
}
|
||||
if len(c.ClickHouse.Addrs) == 0 {
|
||||
return errors.New("clickhouse.addrs 不能为空")
|
||||
}
|
||||
if c.MinIO.Endpoint == "" {
|
||||
return errors.New("minio.endpoint 不能为空")
|
||||
}
|
||||
if c.Tracer.Enabled && c.Tracer.OTLPEndpoint == "" {
|
||||
return errors.New("tracer.enabled 时 otlp_endpoint 不能为空")
|
||||
}
|
||||
if c.RateLimit.Enabled {
|
||||
if c.RateLimit.Capacity <= 0 || c.RateLimit.RatePerSec <= 0 {
|
||||
return errors.New("rate_limit.capacity / rate_per_sec 必须大于 0")
|
||||
}
|
||||
switch c.RateLimit.Key {
|
||||
case "ip", "global":
|
||||
default:
|
||||
return fmt.Errorf("rate_limit.key 非法: %q(可选 ip/global)", c.RateLimit.Key)
|
||||
}
|
||||
}
|
||||
if c.JWT.Secret == "" {
|
||||
return errors.New("jwt.secret 不能为空")
|
||||
}
|
||||
if c.HotReload.Enabled && c.HotReload.Prefix == "" {
|
||||
return errors.New("hot_reload.enabled 时 prefix 不能为空")
|
||||
}
|
||||
switch c.Log.Level {
|
||||
case "debug", "info", "warn", "error":
|
||||
default:
|
||||
return fmt.Errorf("log.level 非法: %q(可选 debug/info/warn/error)", c.Log.Level)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ETCDWatcher 配置热更新(Day 12):监听 ETCD prefix,变更即全量合并并回调。
|
||||
//
|
||||
// 键模型(prefix 下每个键对应一个配置项,/ 转为 . 层级):
|
||||
//
|
||||
// /config/go-sms/log/level = debug → log.level=debug
|
||||
// /config/go-sms/rate_limit/capacity=5 → rate_limit.capacity=5
|
||||
//
|
||||
// 变更策略:任一 put/delete 触发全量重拉 prefix → 覆盖加载 → 校验 → 回调;
|
||||
// 校验失败保留旧配置并告警(不会带病生效)。
|
||||
type ETCDWatcher struct {
|
||||
client *clientv3.Client
|
||||
prefix string
|
||||
source string // 底稿配置文件路径
|
||||
reload func(cfg *Config)
|
||||
logger *zap.Logger
|
||||
lastSnap string // 上次覆盖集快照(变更检测,轮询幂等)
|
||||
}
|
||||
|
||||
// NewETCDWatcher 创建 watcher;reload 在每次成功合并后回调(异步 goroutine 中调用)。
|
||||
func NewETCDWatcher(client *clientv3.Client, prefix, sourcePath string, reload func(*Config), lg *zap.Logger) *ETCDWatcher {
|
||||
if lg == nil {
|
||||
lg = zap.NewNop()
|
||||
}
|
||||
return &ETCDWatcher{client: client, prefix: prefix, source: sourcePath, reload: reload, logger: lg}
|
||||
}
|
||||
|
||||
// Run 阻塞监听直至 ctx 取消:watch 实时事件 + 5s 兜底轮询(防 watch 静默失联)。
|
||||
func (w *ETCDWatcher) Run(ctx context.Context) error {
|
||||
w.apply(ctx, "startup")
|
||||
|
||||
ticker := time.NewTicker(5 * time.Second) // 兜底轮询:变更检测幂等
|
||||
defer ticker.Stop()
|
||||
ch := w.client.Watch(ctx, w.prefix, clientv3.WithPrefix())
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
w.apply(ctx, "poll")
|
||||
case resp, ok := <-ch:
|
||||
if !ok || resp.Canceled { // watch 通道断开:重建
|
||||
w.logger.Warn("etcd_watch_disconnected_retrying")
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-time.After(3 * time.Second):
|
||||
}
|
||||
ch = w.client.Watch(ctx, w.prefix, clientv3.WithPrefix())
|
||||
w.apply(ctx, "rewatch")
|
||||
continue
|
||||
}
|
||||
if len(resp.Events) > 0 {
|
||||
w.apply(ctx, "watch")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// apply 全量拉取 prefix → 覆盖加载 → 校验 → 回调。
|
||||
func (w *ETCDWatcher) apply(ctx context.Context, trigger string) {
|
||||
getCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
resp, err := w.client.Get(getCtx, w.prefix, clientv3.WithPrefix())
|
||||
cancel()
|
||||
if err != nil {
|
||||
w.logger.Warn("etcd_config_get_failed", zap.String("trigger", trigger), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if len(resp.Kvs) == 0 {
|
||||
return // 无覆盖键:保持文件配置
|
||||
}
|
||||
|
||||
overrides := make(map[string]string, len(resp.Kvs))
|
||||
for _, kv := range resp.Kvs {
|
||||
key := strings.TrimPrefix(string(kv.Key), w.prefix)
|
||||
key = strings.TrimSuffix(strings.ReplaceAll(key, "/", "."), ".")
|
||||
overrides[key] = string(kv.Value)
|
||||
}
|
||||
|
||||
// 变更检测:快照一致则跳过(轮询幂等,不重复回调/刷日志)
|
||||
snap := fmt.Sprint(overrides)
|
||||
if snap == w.lastSnap {
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := LoadWithOverrides(w.source, overrides)
|
||||
if err != nil {
|
||||
w.logger.Warn("etcd_config_invalid_keep_old", zap.String("trigger", trigger), zap.Error(err))
|
||||
return
|
||||
}
|
||||
w.lastSnap = snap
|
||||
w.reload(cfg)
|
||||
w.logger.Info("config_reloaded",
|
||||
zap.String("trigger", trigger), zap.Int("override_keys", len(overrides)))
|
||||
}
|
||||
|
||||
// fmt 保持引用(键规范化注释)。
|
||||
@@ -0,0 +1,134 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
// EnvPrefix 环境变量统一前缀,如 GOSMS_MYSQL_DSN 覆盖 mysql.dsn。
|
||||
EnvPrefix = "GOSMS"
|
||||
// EnvKeyFile 指定配置文件路径的环境变量(GOSMS_CONFIG)。
|
||||
EnvKeyFile = EnvPrefix + "_CONFIG"
|
||||
// DefaultConfigFile 默认配置文件(相对工作目录)。
|
||||
DefaultConfigFile = "configs/config.dev.yaml"
|
||||
)
|
||||
|
||||
// Load 从指定路径加载并校验配置。
|
||||
//
|
||||
// 加载优先级:path 参数 > 环境变量 GOSMS_CONFIG > configs/config.dev.yaml。
|
||||
// YAML 中的任意键均可被环境变量覆盖:键层级以 "_" 连接并加前缀 GOSMS_,
|
||||
// 示例:mysql.dsn -> GOSMS_MYSQL_DSN;redis.addrs(切片)以逗号分隔。
|
||||
func Load(path string) (*Config, error) {
|
||||
return load(path, nil)
|
||||
}
|
||||
|
||||
// LoadWithOverrides 在文件与环境变量之上应用键值覆盖(ETCD 热更新用)。
|
||||
// overrides 的键为点分层级(如 "log.level"),值以字符串写入。
|
||||
func LoadWithOverrides(path string, overrides map[string]string) (*Config, error) {
|
||||
return load(path, overrides)
|
||||
}
|
||||
|
||||
// load 统一装配:文件 → 默认值 → 环境变量 → 显式覆盖。
|
||||
func load(path string, overrides map[string]string) (*Config, error) {
|
||||
if path == "" {
|
||||
path = resolveDefaultPath()
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetEnvPrefix(EnvPrefix)
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.AutomaticEnv()
|
||||
setDefaults(v)
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("读取配置文件 %q 失败: %w", path, err)
|
||||
}
|
||||
for k, val := range overrides {
|
||||
v.Set(k, val) // 显式覆盖优先级最高
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析配置文件 %q 失败: %w", path, err)
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("配置校验失败(%s): %w", path, err)
|
||||
}
|
||||
cfg.Source = path
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// resolveDefaultPath 解析默认配置文件路径。
|
||||
func resolveDefaultPath() string {
|
||||
if p := os.Getenv(EnvKeyFile); p != "" {
|
||||
return p
|
||||
}
|
||||
return filepath.Join(filepath.Clean(DefaultConfigFile))
|
||||
}
|
||||
|
||||
// setDefaults 为全部键设置默认值;
|
||||
// 同时保证仅通过环境变量覆盖的字段在 Unmarshal 时可见(viper 已知键限制)。
|
||||
func setDefaults(v *viper.Viper) {
|
||||
// server
|
||||
v.SetDefault("server.name", "go-sms")
|
||||
v.SetDefault("server.http.addr", ":8080")
|
||||
v.SetDefault("server.http.read_timeout", "30s")
|
||||
v.SetDefault("server.http.write_timeout", "30s")
|
||||
v.SetDefault("server.cmpp.addr", ":7890")
|
||||
v.SetDefault("server.cmpp.max_connections_per_account", 50)
|
||||
v.SetDefault("server.cmpp.idle_timeout", "60s")
|
||||
v.SetDefault("server.cmpp.window_size", 32)
|
||||
// log
|
||||
v.SetDefault("log.level", "info")
|
||||
v.SetDefault("log.format", "json")
|
||||
v.SetDefault("log.output", []string{"stdout"})
|
||||
v.SetDefault("log.file.path", "/var/log/go-sms/app.log")
|
||||
v.SetDefault("log.file.max_size_mb", 100)
|
||||
v.SetDefault("log.file.max_age_days", 30)
|
||||
v.SetDefault("log.file.max_backups", 10)
|
||||
v.SetDefault("log.file.compress", true)
|
||||
// mysql
|
||||
v.SetDefault("mysql.max_open_conns", 100)
|
||||
v.SetDefault("mysql.max_idle_conns", 20)
|
||||
v.SetDefault("mysql.conn_max_lifetime", "1h")
|
||||
v.SetDefault("mysql.slow_threshold_ms", 500)
|
||||
// redis
|
||||
v.SetDefault("redis.mode", "single")
|
||||
v.SetDefault("redis.db", 0)
|
||||
v.SetDefault("redis.pool_size", 100)
|
||||
// balance(Day54 R1:默认 mysql 行锁,灰度切 redis;env GOSMS_BALANCE_MODE 覆盖)
|
||||
v.SetDefault("balance.mode", "mysql")
|
||||
v.SetDefault("balance.reconcile_interval", "5m")
|
||||
// kafka
|
||||
v.SetDefault("kafka.group_id", "go-sms")
|
||||
v.SetDefault("kafka.max_retry", 3)
|
||||
// etcd
|
||||
v.SetDefault("etcd.dial_timeout", "5s")
|
||||
// clickhouse
|
||||
v.SetDefault("clickhouse.database", "default")
|
||||
v.SetDefault("clickhouse.username", "default")
|
||||
v.SetDefault("clickhouse.dial_timeout", "10s")
|
||||
// minio
|
||||
v.SetDefault("minio.use_ssl", false)
|
||||
// tracer(Day 9)
|
||||
v.SetDefault("tracer.enabled", true)
|
||||
v.SetDefault("tracer.otlp_endpoint", "127.0.0.1:4318")
|
||||
v.SetDefault("tracer.service_name", "go-sms-gateway")
|
||||
// rate_limit(Day 9)
|
||||
v.SetDefault("rate_limit.enabled", true)
|
||||
v.SetDefault("rate_limit.capacity", int64(1000))
|
||||
v.SetDefault("rate_limit.rate_per_sec", int64(100))
|
||||
v.SetDefault("rate_limit.key", "ip")
|
||||
// jwt(Day 22)
|
||||
v.SetDefault("jwt.secret", "dev-go-sms-secret")
|
||||
v.SetDefault("jwt.ttl", "2h")
|
||||
// hot_reload(Day 12)
|
||||
v.SetDefault("hot_reload.enabled", true)
|
||||
v.SetDefault("hot_reload.prefix", "/config/go-sms/")
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// testConfigFile 单元测试专用配置(位于 testdata,与开发配置隔离)。
|
||||
const testConfigFile = "config.test.yaml"
|
||||
|
||||
func loadTestConfig(t *testing.T) *Config {
|
||||
t.Helper()
|
||||
cfg, err := Load(filepath.Join("testdata", testConfigFile))
|
||||
if err != nil {
|
||||
t.Fatalf("加载测试配置失败: %v", err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// TestLoad_FieldsMapped 验证 YAML 各层级字段被正确映射到结构体。
|
||||
func TestLoad_FieldsMapped(t *testing.T) {
|
||||
cfg := loadTestConfig(t)
|
||||
|
||||
if cfg.Server.Name != "go-sms-test" {
|
||||
t.Errorf("server.name = %q, 期望 %q", cfg.Server.Name, "go-sms-test")
|
||||
}
|
||||
if cfg.Server.HTTP.Addr != ":9800" {
|
||||
t.Errorf("server.http.addr = %q, 期望 %q", cfg.Server.HTTP.Addr, ":9800")
|
||||
}
|
||||
if cfg.Server.HTTP.ReadTimeout != 10*time.Second {
|
||||
t.Errorf("server.http.read_timeout = %s, 期望 10s", cfg.Server.HTTP.ReadTimeout)
|
||||
}
|
||||
if cfg.MySQL.MaxOpenConns != 10 {
|
||||
t.Errorf("mysql.max_open_conns = %d, 期望 10", cfg.MySQL.MaxOpenConns)
|
||||
}
|
||||
if cfg.Redis.Mode != "cluster" {
|
||||
t.Errorf("redis.mode = %q, 期望 cluster", cfg.Redis.Mode)
|
||||
}
|
||||
if len(cfg.Redis.Addrs) != 2 || cfg.Redis.Addrs[0] != "127.0.0.1:16380" {
|
||||
t.Errorf("redis.addrs = %v, 期望 [127.0.0.1:16380 127.0.0.1:16381]", cfg.Redis.Addrs)
|
||||
}
|
||||
if cfg.Kafka.Topics["submit"] != "test-sms-submit" {
|
||||
t.Errorf("kafka.topics.submit = %q, 期望 test-sms-submit", cfg.Kafka.Topics["submit"])
|
||||
}
|
||||
if cfg.ETCD.DialTimeout != 5*time.Second {
|
||||
t.Errorf("etcd.dial_timeout = %s, 期望 5s", cfg.ETCD.DialTimeout)
|
||||
}
|
||||
if cfg.MinIO.Bucket != "go-sms-test" {
|
||||
t.Errorf("minio.bucket = %q, 期望 go-sms-test", cfg.MinIO.Bucket)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoad_DefaultsApplied 验证未在 YAML 中出现的键回退到默认值。
|
||||
func TestLoad_DefaultsApplied(t *testing.T) {
|
||||
cfg := loadTestConfig(t)
|
||||
|
||||
// 测试配置未定义以下键,应命中 setDefaults
|
||||
if !cfg.Log.File.Compress {
|
||||
t.Error("log.file.compress 默认应为 true")
|
||||
}
|
||||
if cfg.Server.CMPP.WindowSize != 32 {
|
||||
t.Errorf("server.cmpp.window_size 默认应为 32, 实际 %d", cfg.Server.CMPP.WindowSize)
|
||||
}
|
||||
if cfg.MySQL.MaxIdleConns != 20 {
|
||||
t.Errorf("mysql.max_idle_conns 默认应为 20, 实际 %d", cfg.MySQL.MaxIdleConns)
|
||||
}
|
||||
if cfg.Log.Level != "info" {
|
||||
t.Errorf("log.level 默认应为 info, 实际 %q", cfg.Log.Level)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoad_EnvOverride 验证 GOSMS_ 前缀环境变量可覆盖 YAML 值。
|
||||
func TestLoad_EnvOverride(t *testing.T) {
|
||||
t.Setenv("GOSMS_MYSQL_DSN", "env-user:env-pass@tcp(127.0.0.1:13306)/env_db")
|
||||
t.Setenv("GOSMS_REDIS_ADDRS", "10.0.0.1:6379,10.0.0.2:6379")
|
||||
|
||||
cfg := loadTestConfig(t)
|
||||
|
||||
if !strings.Contains(cfg.MySQL.DSN, "env_db") {
|
||||
t.Errorf("mysql.dsn 环境变量覆盖失败, 实际 %q", cfg.MySQL.DSN)
|
||||
}
|
||||
if len(cfg.Redis.Addrs) != 2 || cfg.Redis.Addrs[0] != "10.0.0.1:6379" {
|
||||
t.Errorf("redis.addrs 环境变量覆盖失败, 实际 %v", cfg.Redis.Addrs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoad_FileNotFound 验证配置文件不存在时返回错误。
|
||||
func TestLoad_FileNotFound(t *testing.T) {
|
||||
if _, err := Load(filepath.Join("testdata", "not-exists.yaml")); err == nil {
|
||||
t.Fatal("配置文件不存在时应返回错误")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadWithOverrides 验证覆盖加载优先级(文件 < 显式覆盖)。
|
||||
func TestLoadWithOverrides(t *testing.T) {
|
||||
t.Setenv("GOSMS_LOG_LEVEL", "warn") // 环境变量也应被显式覆盖压过
|
||||
cfg, err := LoadWithOverrides(filepath.Join("testdata", testConfigFile),
|
||||
map[string]string{"log.level": "error", "mysql.max_open_conns": "7"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadWithOverrides 失败: %v", err)
|
||||
}
|
||||
if cfg.Log.Level != "error" {
|
||||
t.Errorf("log.level = %q, 期望 error(覆盖最高优先级)", cfg.Log.Level)
|
||||
}
|
||||
if cfg.MySQL.MaxOpenConns != 7 {
|
||||
t.Errorf("mysql.max_open_conns = %d, 期望 7", cfg.MySQL.MaxOpenConns)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoad_ValidateFailed 验证非法配置被 Validate 拦截。
|
||||
func TestLoad_ValidateFailed(t *testing.T) {
|
||||
_, err := Load(filepath.Join("testdata", "invalid.yaml"))
|
||||
if err == nil {
|
||||
t.Fatal("非法配置应返回校验错误")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "配置校验失败") {
|
||||
t.Errorf("错误信息应包含 \"配置校验失败\", 实际: %v", err)
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
# 单元测试专用配置:字段值与开发配置刻意不同,便于断言映射正确性。
|
||||
server:
|
||||
name: go-sms-test
|
||||
http:
|
||||
addr: ":9800"
|
||||
read_timeout: 10s
|
||||
write_timeout: 10s
|
||||
|
||||
mysql:
|
||||
dsn: "test:test@tcp(127.0.0.1:13306)/go-sms_test?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
max_open_conns: 10
|
||||
|
||||
redis:
|
||||
mode: cluster
|
||||
addrs: ["127.0.0.1:16380", "127.0.0.1:16381"]
|
||||
|
||||
kafka:
|
||||
brokers: ["127.0.0.1:19092"]
|
||||
topics:
|
||||
submit: test-sms-submit
|
||||
report: test-sms-report
|
||||
up: test-sms-up
|
||||
stats: test-stats
|
||||
|
||||
etcd:
|
||||
endpoints: ["127.0.0.1:12379"]
|
||||
dial_timeout: 5s
|
||||
|
||||
clickhouse:
|
||||
addrs: ["127.0.0.1:19000"]
|
||||
database: go-sms_test
|
||||
|
||||
minio:
|
||||
endpoint: "127.0.0.1:19010"
|
||||
access_key: test-access
|
||||
secret_key: test-secret
|
||||
bucket: go-sms-test
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
# 故意缺失 mysql.dsn / log.level 非法,用于验证 Validate 拦截非法配置。
|
||||
server:
|
||||
http:
|
||||
addr: ":9800"
|
||||
|
||||
mysql:
|
||||
dsn: ""
|
||||
|
||||
redis:
|
||||
mode: single
|
||||
addrs: ["127.0.0.1:16379"]
|
||||
|
||||
kafka:
|
||||
brokers: ["127.0.0.1:19092"]
|
||||
topics:
|
||||
submit: sms-submit
|
||||
report: sms-report
|
||||
up: sms-up
|
||||
stats: stats
|
||||
|
||||
etcd:
|
||||
endpoints: ["127.0.0.1:12379"]
|
||||
|
||||
clickhouse:
|
||||
addrs: ["127.0.0.1:19000"]
|
||||
|
||||
minio:
|
||||
endpoint: "127.0.0.1:19010"
|
||||
|
||||
log:
|
||||
level: trace
|
||||
@@ -0,0 +1,83 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
func etcdAddrForWatch() string {
|
||||
if a := os.Getenv("GOSMS_TEST_ETCD_ADDR"); a != "" {
|
||||
return a
|
||||
}
|
||||
return "127.0.0.1:12379"
|
||||
}
|
||||
|
||||
func etcdReachableForWatch() bool {
|
||||
conn, err := net.DialTimeout("tcp", etcdAddrForWatch(), time.Second)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_ = conn.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
// TestETCDWatcher_HotReload 实连验证:put 键后 10s 内收到 config_reloaded 回调。
|
||||
// 前置:docker compose up -d etcd;不可达自动 Skip。
|
||||
func TestETCDWatcher_HotReload(t *testing.T) {
|
||||
if !etcdReachableForWatch() {
|
||||
t.Skipf("ETCD %s 不可达,跳过", etcdAddrForWatch())
|
||||
}
|
||||
cli, err := clientv3.New(clientv3.Config{Endpoints: []string{etcdAddrForWatch()}, DialTimeout: 3 * time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("连接 ETCD 失败: %v", err)
|
||||
}
|
||||
defer func() { _ = cli.Close() }()
|
||||
|
||||
prefix := fmt.Sprintf("/config/go-sms-it/%d/", time.Now().UnixNano())
|
||||
key := prefix + "log/level"
|
||||
defer func() { _, _ = cli.Delete(context.Background(), prefix, clientv3.WithPrefix()) }()
|
||||
|
||||
reloaded := make(chan *Config, 4)
|
||||
w := NewETCDWatcher(cli, prefix, "testdata/"+testConfigFile, func(cfg *Config) {
|
||||
select {
|
||||
case reloaded <- cfg:
|
||||
default:
|
||||
}
|
||||
}, nil)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() { _ = w.Run(ctx) }()
|
||||
|
||||
// 变更 1:设置 debug → 回调 cfg.Log.Level=debug
|
||||
if _, err := cli.Put(ctx, key, "debug"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case cfg := <-reloaded:
|
||||
if cfg.Log.Level != "debug" {
|
||||
t.Fatalf("热更后 log.level = %q, 期望 debug", cfg.Log.Level)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("10s 内未触发 config_reloaded")
|
||||
}
|
||||
|
||||
// 变更 2:改为 error → 再次回调
|
||||
if _, err := cli.Put(ctx, key, "error"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case cfg := <-reloaded:
|
||||
if cfg.Log.Level != "error" {
|
||||
t.Fatalf("二次热更 log.level = %q, 期望 error", cfg.Log.Level)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("第二次变更未触发")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// audit.go Day 29:审计拦截中间件——受保护组的敏感写操作(POST/PUT/DELETE)
|
||||
// 与导出类 GET(path 含 export)在响应 2xx/3xx 后留痕;who=claims,what=路由模板,
|
||||
// when=时间(库侧 autoCreateTime),IP=ClientIP;记录失败不影响响应。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"go-sms/internal/service/audit"
|
||||
)
|
||||
|
||||
// AuditTrail 敏感操作审计:recorder 为 nil 时直通(降级)。
|
||||
func AuditTrail(recorder *audit.Service) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Next()
|
||||
if recorder == nil {
|
||||
return
|
||||
}
|
||||
if !auditWorthy(c.Request.Method, c.FullPath()) {
|
||||
return
|
||||
}
|
||||
if c.Writer.Status() >= http.StatusBadRequest {
|
||||
return // 失败请求(含 401/403/4xx/5xx)不留操作日志,避免噪音
|
||||
}
|
||||
e := &audit.Entry{
|
||||
IP: c.ClientIP(),
|
||||
Module: moduleOf(c.FullPath()),
|
||||
Action: actionOf(c.Request.Method, c.FullPath()),
|
||||
Target: c.Param("id"),
|
||||
Detail: fmt.Sprintf("%s %s -> %d", c.Request.Method, c.Request.URL.Path, c.Writer.Status()),
|
||||
}
|
||||
if v, ok := c.Get(ContextKeyClaims); ok {
|
||||
if claims, _ := v.(*Claims); claims != nil {
|
||||
e.Operator, e.AccountID = claims.Username, claims.AccountID
|
||||
}
|
||||
}
|
||||
// 尽力而为:审计失败不影响业务响应
|
||||
_ = recorder.Record(c.Request.Context(), e)
|
||||
}
|
||||
}
|
||||
|
||||
// auditWorthy 是否值得留痕:写方法或导出类 GET。
|
||||
func auditWorthy(method, fullPath string) bool {
|
||||
switch method {
|
||||
case http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch:
|
||||
return true
|
||||
case http.MethodGet:
|
||||
return strings.Contains(fullPath, "export")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// moduleOf 路由模板首段(如 /api/v1/accounts/:id → accounts→account 域)。
|
||||
func moduleOf(fullPath string) string {
|
||||
if fullPath == "" {
|
||||
return "unknown"
|
||||
}
|
||||
parts := strings.Split(strings.Trim(fullPath, "/"), "/")
|
||||
// 跳过 api/v1 前缀
|
||||
for _, p := range parts {
|
||||
if p == "api" || p == "v1" {
|
||||
continue
|
||||
}
|
||||
return strings.TrimSuffix(p, "s")
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// actionOf 动作名:方法+路径尾段(去掉参数)。
|
||||
func actionOf(method, fullPath string) string {
|
||||
parts := strings.Split(strings.Trim(fullPath, "/"), "/")
|
||||
tail := ""
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
if !strings.HasPrefix(parts[i], ":") && parts[i] != "" {
|
||||
tail = parts[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if tail == "" {
|
||||
return strings.ToLower(method)
|
||||
}
|
||||
return tail + ":" + strings.ToLower(method)
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// audit_test.go Day 29:审计拦截中间件(who/what/when 留痕)。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/service/audit"
|
||||
)
|
||||
|
||||
// newAudit29 内存库审计环境。
|
||||
func newAudit29(t *testing.T) (*gin.Engine, *gorm.DB, string) {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.SystemOperateLog{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
tok, _ := SignAccessToken("sec", 7, "admin", "admin", time.Minute)
|
||||
protected := r.Group("/api/v1", AuthJWT("sec"), AuditTrail(audit.NewService(db)))
|
||||
protected.POST("/accounts/:id/recharge", func(c *gin.Context) { c.Status(http.StatusOK) })
|
||||
protected.GET("/accounts", func(c *gin.Context) { c.Status(http.StatusOK) })
|
||||
protected.GET("/accounts/export", func(c *gin.Context) { c.Status(http.StatusOK) })
|
||||
protected.POST("/accounts", func(c *gin.Context) { c.Status(http.StatusBadRequest) })
|
||||
return r, db, tok
|
||||
}
|
||||
|
||||
// countLogs 库内日志条数(可选过滤)。
|
||||
func countLogs(t *testing.T, db *gorm.DB) []entity.SystemOperateLog {
|
||||
t.Helper()
|
||||
var logs []entity.SystemOperateLog
|
||||
if err := db.Order("id").Find(&logs).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return logs
|
||||
}
|
||||
|
||||
// TestAuditTrail_WritesAreLogged 验收:写操作 100% 留痕(who/what/when)。
|
||||
func TestAuditTrail_WritesAreLogged(t *testing.T) {
|
||||
r, db, tok := newAudit29(t)
|
||||
// POST 200 → 留痕
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/2/recharge", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
r.ServeHTTP(httptest.NewRecorder(), req)
|
||||
|
||||
logs := countLogs(t, db)
|
||||
if len(logs) != 1 {
|
||||
t.Fatalf("写操作应留痕 1 条, got %d", len(logs))
|
||||
}
|
||||
l := logs[0]
|
||||
if l.Operator != "admin" || l.AccountID != 7 {
|
||||
t.Fatalf("who 异常: %q/%d", l.Operator, l.AccountID)
|
||||
}
|
||||
if l.Module != "account" || l.Action != "recharge:post" || l.Target != "2" {
|
||||
t.Fatalf("what 异常: %s/%s/%s", l.Module, l.Action, l.Target)
|
||||
}
|
||||
if l.CreatedAt.IsZero() {
|
||||
t.Fatal("when(created_at) 异常")
|
||||
}
|
||||
if l.Detail == "" {
|
||||
t.Fatal("detail 应含方法/路径/状态摘要")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuditTrail_SkipRules GET 普通不留;GET export 留;失败请求不留;未认证不留。
|
||||
func TestAuditTrail_SkipRules(t *testing.T) {
|
||||
r, db, tok := newAudit29(t)
|
||||
hit := func(method, path, token string) {
|
||||
req := httptest.NewRequest(method, path, nil)
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
r.ServeHTTP(httptest.NewRecorder(), req)
|
||||
}
|
||||
hit(http.MethodGet, "/api/v1/accounts", tok) // 普通 GET:不留
|
||||
hit(http.MethodPost, "/api/v1/accounts", tok) // 400:不留
|
||||
hit(http.MethodPost, "/api/v1/accounts/2/recharge", "") // 401:不留
|
||||
hit(http.MethodGet, "/api/v1/accounts/export", tok) // 导出 GET:留
|
||||
logs := countLogs(t, db)
|
||||
if len(logs) != 1 {
|
||||
t.Fatalf("应仅导出留痕 1 条, got %d", len(logs))
|
||||
}
|
||||
if logs[0].Action != "export:get" {
|
||||
t.Fatalf("导出动作 = %s", logs[0].Action)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuditTrail_NilRecorder 降级直通。
|
||||
func TestAuditTrail_NilRecorder(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.POST("/x", AuditTrail(nil), func(c *gin.Context) { c.JSON(http.StatusOK, json.RawMessage("{}")) })
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/x", nil))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("nil recorder 应直通, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
|
||||
jwtpkg "go-sms/internal/pkg/jwt"
|
||||
|
||||
"go-sms/internal/pkg/errors"
|
||||
)
|
||||
|
||||
// Claims go-sms JWT 载荷(Day 17 起由 internal/pkg/jwt 定义,别名保持兼容)。
|
||||
type Claims = jwtpkg.Claims
|
||||
|
||||
// ContextKeyClaims gin.Context 中存放已解析 Claims 的键。
|
||||
const ContextKeyClaims = "auth_claims"
|
||||
|
||||
// AuthJWT JWT 鉴权中间件:
|
||||
// Authorization: Bearer <token>;无效/过期/缺失返回 401。
|
||||
// 解析成功后将 Claims 写入 Context 供后续 handler 与权限校验使用。
|
||||
func AuthJWT(secret string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if parseBearer(secret, c) == nil {
|
||||
return // 已 abort
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// parseBearer 解析并校验 Bearer 令牌;失败 abort 并返回 nil,成功则写入
|
||||
// ContextKeyClaims 并返回 Claims。
|
||||
//
|
||||
// 注意:不调用 c.Next()。组合型中间件(WithChecker/WithRevocation)必须
|
||||
// 在放行前完成全部校验,若复用 AuthJWT(末尾 c.Next)会导致 handler 先于
|
||||
// 包装层校验执行(Day 24 修复的 Day 22 遗留问题)。
|
||||
func parseBearer(secret string, c *gin.Context) *Claims {
|
||||
raw := bearerToken(c)
|
||||
if raw == "" {
|
||||
abortUnauthorized(c, "缺少 Bearer Token")
|
||||
return nil
|
||||
}
|
||||
claims := &Claims{}
|
||||
token, err := jwt.ParseWithClaims(raw, claims, func(t *jwt.Token) (any, error) {
|
||||
// 强制 HMAC 系列签名算法,防 alg 混淆攻击
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return []byte(secret), nil
|
||||
})
|
||||
if err != nil || !token.Valid {
|
||||
abortUnauthorized(c, "Token 无效或已过期")
|
||||
return nil
|
||||
}
|
||||
c.Set(ContextKeyClaims, claims)
|
||||
return claims
|
||||
}
|
||||
|
||||
// BearerToken 提取 Authorization 头中的 Bearer 令牌(导出版,handler 复用)。
|
||||
func BearerToken(c *gin.Context) string { return bearerToken(c) }
|
||||
|
||||
// bearerToken 提取 Authorization 头中的 Bearer 令牌。
|
||||
// 权限校验(PermissionChecker/RequirePermission)见 permission.go(Day 25)。
|
||||
func bearerToken(c *gin.Context) string {
|
||||
h := c.GetHeader("Authorization")
|
||||
if h == "" {
|
||||
return ""
|
||||
}
|
||||
parts := strings.SplitN(h, " ", 2)
|
||||
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
// abortUnauthorized 统一 401 响应。
|
||||
func abortUnauthorized(c *gin.Context, msg string) {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"code": errors.Unauthorized.Code,
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
||||
|
||||
// AuthJWTWithChecker 带账号状态校验的 JWT 鉴权(Day 22:禁用后 Token 即失效;
|
||||
// Day 24 修复:改用 parseBearer,校验先于 handler,不再经 base(c) 提前放行)。
|
||||
func AuthJWTWithChecker(secret string, checker func(ctx context.Context, accountID int64) (bool, error)) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
claims := parseBearer(secret, c)
|
||||
if claims == nil {
|
||||
return // 已 abort
|
||||
}
|
||||
if checker == nil {
|
||||
return
|
||||
}
|
||||
enabled, err := checker(c.Request.Context(), claims.AccountID)
|
||||
if err != nil {
|
||||
abortUnauthorized(c, "账号状态校验失败")
|
||||
return
|
||||
}
|
||||
if !enabled {
|
||||
abortUnauthorized(c, "账号已被禁用,令牌失效")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AuthJWTWithRevocation JWT 鉴权 + 登出黑名单 + 账号状态校验(Day 24):
|
||||
// revoked 查询令牌是否已注销(nil 跳过);checker 校验账号启用态(nil 跳过)。
|
||||
// 任一不通过返回 401(令牌层面的失效统一按未认证处理)。
|
||||
func AuthJWTWithRevocation(secret string,
|
||||
revoked func(ctx context.Context, rawToken string) bool,
|
||||
checker func(ctx context.Context, accountID int64) (bool, error)) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
claims := parseBearer(secret, c)
|
||||
if claims == nil {
|
||||
return // 已 abort
|
||||
}
|
||||
if revoked != nil && revoked(c.Request.Context(), bearerToken(c)) {
|
||||
abortUnauthorized(c, "令牌已注销,请重新登录")
|
||||
return
|
||||
}
|
||||
if checker == nil {
|
||||
return
|
||||
}
|
||||
enabled, err := checker(c.Request.Context(), claims.AccountID)
|
||||
if err != nil {
|
||||
abortUnauthorized(c, "账号状态校验失败")
|
||||
return
|
||||
}
|
||||
if !enabled {
|
||||
abortUnauthorized(c, "账号已被禁用,令牌失效")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IPCheck IP 白名单校验中间件(Day 23):
|
||||
// 从 JWT claims 取 accountID,查询 account_user.ip_bind(JSON 数组),
|
||||
// 非空白名单且请求 IP 不在其中 → 403 ISC:403。
|
||||
func IPCheck(checker func(ctx context.Context, accountID int64, clientIP string) (bool, error)) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if checker == nil {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
v, ok := c.Get(ContextKeyClaims)
|
||||
if !ok {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
claims := v.(*Claims)
|
||||
allowed, err := checker(c.Request.Context(), claims.AccountID, c.ClientIP())
|
||||
if err != nil || !allowed {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
"code": errors.Forbidden.Code,
|
||||
"msg": "IP 不在白名单内: " + c.ClientIP(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// ttl 直接透传(非正值同样生效,便于测试过期场景)。
|
||||
func SignAccessToken(secret string, accountID int64, username, role string, ttl time.Duration) (string, error) {
|
||||
return (&jwtpkg.Manager{Secret: secret, TTL: ttl, Issuer: "go-sms"}).Sign(accountID, username, []string{role}, nil)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// auth_day24_test.go Day 24:黑名单鉴权中间件测试。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// runRevocation 用给定 revoked/checker 构造中间件并执行一次请求。
|
||||
func runRevocation(t *testing.T, token string,
|
||||
revoked func(context.Context, string) bool,
|
||||
checker func(context.Context, int64) (bool, error)) int {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/p", AuthJWTWithRevocation("sec", revoked, checker), func(c *gin.Context) {
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/p", nil)
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
r.ServeHTTP(w, req)
|
||||
return w.Code
|
||||
}
|
||||
|
||||
// TestAuthJWTWithRevocation 黑名单/禁用/放行三分支。
|
||||
func TestAuthJWTWithRevocation(t *testing.T) {
|
||||
tok, err := SignAccessToken("sec", 42, "d24", "ops", time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
notRevoked := func(context.Context, string) bool { return false }
|
||||
enabled := func(context.Context, int64) (bool, error) { return true, nil }
|
||||
disabled := func(context.Context, int64) (bool, error) { return false, nil }
|
||||
|
||||
// 已注销令牌 → 401
|
||||
if code := runRevocation(t, tok, func(context.Context, string) bool { return true }, enabled); code != http.StatusUnauthorized {
|
||||
t.Fatalf("已注销令牌 = %d, 期望 401", code)
|
||||
}
|
||||
// 未注销但账号禁用 → 401
|
||||
if code := runRevocation(t, tok, notRevoked, disabled); code != http.StatusUnauthorized {
|
||||
t.Fatalf("禁用账号 = %d, 期望 401", code)
|
||||
}
|
||||
// 正常放行 → 200
|
||||
if code := runRevocation(t, tok, notRevoked, enabled); code != http.StatusOK {
|
||||
t.Fatalf("正常令牌 = %d, 期望 200", code)
|
||||
}
|
||||
// 无令牌 → 401
|
||||
if code := runRevocation(t, "", notRevoked, enabled); code != http.StatusUnauthorized {
|
||||
t.Fatalf("无令牌 = %d, 期望 401", code)
|
||||
}
|
||||
// nil 回调全部跳过(仅 JWT 校验)→ 200
|
||||
if code := runRevocation(t, tok, nil, nil); code != http.StatusOK {
|
||||
t.Fatalf("nil 回调 = %d, 期望 200", code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const testSecret = "day9-test-secret"
|
||||
|
||||
// newAuthRouter 构建带 JWT 保护路由的测试引擎。
|
||||
func newAuthRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
api := r.Group("/api/v1", AuthJWT(testSecret))
|
||||
api.GET("/me", func(c *gin.Context) {
|
||||
v, _ := c.Get(ContextKeyClaims)
|
||||
claims := v.(*Claims)
|
||||
c.JSON(200, gin.H{"username": claims.Username, "roles": claims.Roles})
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
// TestAuthJWT_ValidToken 验证有效 Token 放行且 Claims 可用。
|
||||
func TestAuthJWT_ValidToken(t *testing.T) {
|
||||
r := newAuthRouter()
|
||||
tok, err := SignAccessToken(testSecret, 1001, "alice", "admin", time.Minute)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 || w.Body.String() != `{"roles":["admin"],"username":"alice"}` {
|
||||
t.Fatalf("有效 Token 应放行: code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuthJWT_InvalidToken 验收项:无效 Token 返回 401。
|
||||
func TestAuthJWT_InvalidToken(t *testing.T) {
|
||||
r := newAuthRouter()
|
||||
cases := []struct {
|
||||
name string
|
||||
auth string
|
||||
}{
|
||||
{"缺失", ""},
|
||||
{"非 Bearer", "Basic dXNlcjpwYXNz"},
|
||||
{"垃圾串", "Bearer not.a.jwt"},
|
||||
{"错误签名", "Bearer " + mustSign("wrong-secret")},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
if tc.auth != "" {
|
||||
req.Header.Set("Authorization", tc.auth)
|
||||
}
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("%s: 状态码 = %d, 期望 401", tc.name, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuthJWT_ExpiredToken 验证过期 Token 返回 401。
|
||||
func TestAuthJWT_ExpiredToken(t *testing.T) {
|
||||
r := newAuthRouter()
|
||||
tok, err := SignAccessToken(testSecret, 1, "bob", "user", -time.Minute) // 已过期
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("过期 Token 应 401, 实际 %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequirePermission 验证 Casbin 预留接口:checker 拒绝 → 403;nil → 放行。
|
||||
func TestRequirePermission(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
deny := func(*Claims, string, string) bool { return false }
|
||||
allow := func(*Claims, string, string) bool { return true }
|
||||
|
||||
tok, _ := SignAccessToken(testSecret, 1, "u", "viewer", time.Minute)
|
||||
hit := func(checker PermissionChecker) int {
|
||||
r := gin.New()
|
||||
r.GET("/res", AuthJWT(testSecret), RequirePermission(checker, "sms", "send"), func(c *gin.Context) {
|
||||
c.Status(200)
|
||||
})
|
||||
req := httptest.NewRequest(http.MethodGet, "/res", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w.Code
|
||||
}
|
||||
if hit(deny) != http.StatusForbidden {
|
||||
t.Error("checker 拒绝应 403")
|
||||
}
|
||||
if hit(allow) != 200 {
|
||||
t.Error("checker 放行应 200")
|
||||
}
|
||||
if hit(nil) != 200 {
|
||||
t.Error("nil checker(未接入 Casbin)应放行")
|
||||
}
|
||||
}
|
||||
|
||||
// mustSign 用指定密钥签发(错误签名用例)。
|
||||
func mustSign(secret string) string {
|
||||
tok, _ := SignAccessToken(secret, 1, "x", "x", time.Minute)
|
||||
return tok
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// backpressure.go Day 48(技术方案 10.4.4):入口限流背压收紧——
|
||||
// 消费者进程按 lag 水位写 Redis 键(go-sms:bp:level/cap);此处 L2 及以上时
|
||||
// 账号桶容量收紧至 min(原值, Σ通道speed);本地 1s 缓存,Redis 故障按原值
|
||||
// (可用性优先)。收紧动作与 L3 消费暂停共同构成三级水位联动。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// 背压键(与 send 包 RedisBPStore 同款口径)。
|
||||
const (
|
||||
bpLevelKey = "go-sms:bp:level"
|
||||
bpCapKey = "go-sms:bp:cap"
|
||||
bpLevelTighten = 2
|
||||
bpCacheTTL = time.Second
|
||||
)
|
||||
|
||||
// bpCacheEntry 本地缓存条目(削 Redis 压力:入口热路径 1s 一读)。
|
||||
type bpCacheEntry struct {
|
||||
until time.Time
|
||||
cap int64 // -1=水位 <L2(不收紧)
|
||||
}
|
||||
|
||||
// BackpressureShrink 容量包装器:返回新的 CapacityOf——
|
||||
// 水位≥L2 时 min(原容量, bp:cap 键值);client nil/键缺失/故障→原容量。
|
||||
func BackpressureShrink(client redis.UniversalClient, base func(*gin.Context) int64) func(*gin.Context) int64 {
|
||||
var mu sync.Mutex
|
||||
var cache bpCacheEntry
|
||||
return func(c *gin.Context) int64 {
|
||||
v := base(c)
|
||||
if client == nil {
|
||||
return v
|
||||
}
|
||||
mu.Lock()
|
||||
entry := cache
|
||||
mu.Unlock()
|
||||
if time.Now().Before(entry.until) {
|
||||
if entry.cap >= 0 && entry.cap < v {
|
||||
return entry.cap
|
||||
}
|
||||
return v
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
capPerSec := int64(-1)
|
||||
lv, err := client.Get(ctx, bpLevelKey).Result()
|
||||
if err == nil {
|
||||
if n, e := strconv.Atoi(lv); e == nil && n >= bpLevelTighten {
|
||||
if cv, e2 := client.Get(ctx, bpCapKey).Result(); e2 == nil {
|
||||
if n2, e3 := strconv.ParseInt(cv, 10, 64); e3 == nil {
|
||||
capPerSec = n2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
cache = bpCacheEntry{until: time.Now().Add(bpCacheTTL), cap: capPerSec}
|
||||
mu.Unlock()
|
||||
if capPerSec >= 0 && capPerSec < v {
|
||||
return capPerSec
|
||||
}
|
||||
return v
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// backpressure_test.go Day 50:BackpressureShrink 单元验证——
|
||||
// L2 水位收紧取 min / 键缺失与水位不足回退原值 / 1s 本地缓存窗口。
|
||||
// (Day50 压测修复:openapi 账号桶 CapacityOf+RateOf 复用同一 shrink——
|
||||
// 稳态吞吐由 rate 决定,仅收容量对持续流量无效。)
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func shrinkOf(t *testing.T, base int64) (func(*gin.Context) int64, redis.UniversalClient) {
|
||||
t.Helper()
|
||||
c := newMiniRedis(t)
|
||||
return BackpressureShrink(c, func(*gin.Context) int64 { return base }), c
|
||||
}
|
||||
|
||||
// newBPContext 带 Request 的 gin 上下文(shrink 内部取 c.Request.Context())。
|
||||
func newBPContext() *gin.Context {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
return c
|
||||
}
|
||||
|
||||
// TestBackpressureShrink_TightenToMin L2 且 bp:cap<base → 收紧至键值。
|
||||
func TestBackpressureShrink_TightenToMin(t *testing.T) {
|
||||
shrink, c := shrinkOf(t, 6000)
|
||||
if err := c.Set(context.Background(), bpLevelKey, "2", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Set(context.Background(), bpCapKey, "500", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := shrink(newBPContext()); got != 500 {
|
||||
t.Fatalf("L2 收紧后 = %d, 期望 500", got)
|
||||
}
|
||||
// 键值>base 时保持 base(收紧语义是 min,不放大量)。
|
||||
if err := c.Set(context.Background(), bpCapKey, "9999", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(1100 * time.Millisecond) // 过本地缓存 TTL
|
||||
if got := shrink(newBPContext()); got != 6000 {
|
||||
t.Fatalf("bp:cap>base 时 = %d, 期望保持 6000", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackpressureShrink_Fallback 键缺失/水位<L2/nil client → 原值。
|
||||
func TestBackpressureShrink_Fallback(t *testing.T) {
|
||||
shrink, c := shrinkOf(t, 6000)
|
||||
if got := shrink(newBPContext()); got != 6000 {
|
||||
t.Fatalf("无水位键 = %d, 期望 6000", got)
|
||||
}
|
||||
if err := c.Set(context.Background(), bpLevelKey, "1", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Set(context.Background(), bpCapKey, "500", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
if got := shrink(newBPContext()); got != 6000 {
|
||||
t.Fatalf("L1 不收紧 = %d, 期望 6000", got)
|
||||
}
|
||||
if got := BackpressureShrink(nil, func(*gin.Context) int64 { return 42 })(newBPContext()); got != 42 {
|
||||
t.Fatalf("nil client = %d, 期望 42", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimit_CountByPhones Day50:批量接口按号码数扣令牌(非每请求 1 个)。
|
||||
func TestRateLimit_CountByPhones(t *testing.T) {
|
||||
c := newMiniRedis(t)
|
||||
r := newRateLimitRouter(c, RateLimitConfig{
|
||||
Capacity: 3,
|
||||
Rate: 1, // 低速率:稳态几乎不回填,考察扣费口径
|
||||
CountOf: func(*gin.Context) int64 { return 2 }, // 模拟 2 号/req
|
||||
KeyFunc: func(*gin.Context) string { return "acct" },
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("第 1 请求(扣 2/3)应放行, got %d", w.Code)
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("第 2 请求(剩 1<2)应 429, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackpressureShrink_LocalCacheTTL 1s 内键变化不影响结果(削 Redis 压力)。
|
||||
func TestBackpressureShrink_LocalCacheTTL(t *testing.T) {
|
||||
shrink, c := shrinkOf(t, 6000)
|
||||
_ = shrink(newBPContext()) // 预热缓存(无键→原值)
|
||||
if err := c.Set(context.Background(), bpLevelKey, "2", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Set(context.Background(), bpCapKey, "500", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := shrink(newBPContext()); got != 6000 {
|
||||
t.Fatalf("缓存窗口内应保持原值 = %d, 期望 6000", got)
|
||||
}
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
if got := shrink(newBPContext()); got != 500 {
|
||||
t.Fatalf("缓存过期后应收紧 = %d, 期望 500", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package middleware Gin 中间件与通用处理器(日志/限流/鉴权/追踪在 Day 5、Day 9 实现)。
|
||||
package middleware
|
||||
@@ -0,0 +1,19 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HealthRegister 注册健康检查路由(GET /health)。
|
||||
// 供 K8s liveness/readiness 探针、负载均衡与健康巡检使用。
|
||||
func HealthRegister(r *gin.Engine) {
|
||||
r.GET("/health", HealthHandler)
|
||||
}
|
||||
|
||||
// HealthHandler 返回进程存活状态:200 + {"status":"ok"}。
|
||||
// Day 5 起可扩展依赖项检查(MySQL ping / Redis ping)并返回 503。
|
||||
func HealthHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// newTestRouter 构建仅含健康检查路由的测试引擎。
|
||||
func newTestRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
HealthRegister(r)
|
||||
return r
|
||||
}
|
||||
|
||||
// TestHealthHandler_StatusOK 验证 GET /health 返回 200 与 {"status":"ok"}。
|
||||
func TestHealthHandler_StatusOK(t *testing.T) {
|
||||
r := newTestRouter()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("HTTP 状态码 = %d, 期望 200", w.Code)
|
||||
}
|
||||
if got := w.Body.String(); got != `{"status":"ok"}` {
|
||||
t.Errorf("响应体 = %s, 期望 {\"status\":\"ok\"}", got)
|
||||
}
|
||||
if ct := w.Header().Get("Content-Type"); ct != "application/json; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %q, 期望 application/json", ct)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHealthHandler_MethodNotAllowed 验证非 GET 请求返回 404。
|
||||
func TestHealthHandler_MethodNotAllowed(t *testing.T) {
|
||||
r := newTestRouter()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/health", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("POST /health 状态码 = %d, 期望 404", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestIPCheck(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
allowAll := func(ctx context.Context, id int64, ip string) (bool, error) { return true, nil }
|
||||
denyAll := func(ctx context.Context, id int64, ip string) (bool, error) { return false, nil }
|
||||
|
||||
setup := func(checker func(ctx context.Context, id int64, ip string) (bool, error)) *httptest.ResponseRecorder {
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set(ContextKeyClaims, &Claims{AccountID: 42, Username: "u"})
|
||||
})
|
||||
r.Use(IPCheck(checker))
|
||||
r.GET("/x", func(c *gin.Context) { c.Status(http.StatusOK) })
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest("GET", "/x", nil))
|
||||
return w
|
||||
}
|
||||
|
||||
if w := setup(allowAll); w.Code != 200 {
|
||||
t.Errorf("allow-all: code = %d, want 200", w.Code)
|
||||
}
|
||||
if w := setup(denyAll); w.Code != 403 {
|
||||
t.Errorf("deny-all: code = %d, want 403", w.Code)
|
||||
}
|
||||
// checker == nil → 直接放行
|
||||
if w := setup(nil); w.Code != 200 {
|
||||
t.Errorf("nil checker: code = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// TraceIDHeader 贯穿链路的追踪头(上游已有则透传,否则生成)。
|
||||
const TraceIDHeader = "X-Trace-Id"
|
||||
|
||||
// TraceIDKey gin.Context 中 trace_id 的键。
|
||||
const TraceIDKey = "trace_id"
|
||||
|
||||
// Logger Gin 请求日志中间件(Day 5):
|
||||
// 每个请求记录一条结构化日志,字段含 trace_id / status / method / path /
|
||||
// client_ip / latency / body_size / user_agent,并以 X-Trace-Id 响应头回传。
|
||||
func Logger(lg *zap.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
traceID := c.GetHeader(TraceIDHeader)
|
||||
if traceID == "" {
|
||||
traceID = NewTraceID()
|
||||
}
|
||||
c.Set(TraceIDKey, traceID)
|
||||
c.Header(TraceIDHeader, traceID)
|
||||
|
||||
c.Next()
|
||||
|
||||
fields := []zap.Field{
|
||||
zap.String("trace_id", traceID),
|
||||
zap.Int("status", c.Writer.Status()),
|
||||
zap.String("method", c.Request.Method),
|
||||
zap.String("path", c.Request.URL.Path),
|
||||
zap.String("client_ip", c.ClientIP()),
|
||||
zap.Duration("latency", time.Since(start)),
|
||||
zap.Int("body_size", c.Writer.Size()),
|
||||
zap.String("user_agent", c.Request.UserAgent()),
|
||||
}
|
||||
if len(c.Errors) > 0 {
|
||||
fields = append(fields, zap.String("errors", c.Errors.String()))
|
||||
}
|
||||
|
||||
switch {
|
||||
case c.Writer.Status() >= 500:
|
||||
lg.Error("http_request", fields...)
|
||||
case c.Writer.Status() >= 400:
|
||||
lg.Warn("http_request", fields...)
|
||||
default:
|
||||
lg.Info("http_request", fields...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewTraceID 生成 16 字符 hex 追踪 ID(时间戳前缀 + 随机数,便于肉眼按时间排序)。
|
||||
// Day 8 雪花 ID 落地后可替换为全局 ID 生成器。
|
||||
func NewTraceID() string {
|
||||
var b [8]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
// crypto/rand 失败极罕见,退化为纳秒时间戳
|
||||
return hex.EncodeToString([]byte(time.Now().Format("150405.000000000")))
|
||||
}
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
// newObservedRouter 构建带请求日志中间件(观察器捕获)的测试引擎。
|
||||
func newObservedRouter(t *testing.T) (*gin.Engine, *observer.ObservedLogs) {
|
||||
t.Helper()
|
||||
core, logs := observer.New(zap.InfoLevel)
|
||||
r := gin.New()
|
||||
r.Use(Logger(zap.New(core)))
|
||||
r.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong") })
|
||||
r.GET("/boom", func(c *gin.Context) { c.String(http.StatusInternalServerError, "err") })
|
||||
return r, logs
|
||||
}
|
||||
|
||||
// strField 从日志条目取字符串字段。
|
||||
func strField(t *testing.T, e observer.LoggedEntry, key string) string {
|
||||
t.Helper()
|
||||
for _, f := range e.Context {
|
||||
if f.Key == key {
|
||||
return f.String
|
||||
}
|
||||
}
|
||||
t.Fatalf("日志缺少字段 %q", key)
|
||||
return ""
|
||||
}
|
||||
|
||||
// intField 从日志条目取整型字段(zap.Int/Duration 存于 Integer)。
|
||||
func intField(t *testing.T, e observer.LoggedEntry, key string) int64 {
|
||||
t.Helper()
|
||||
for _, f := range e.Context {
|
||||
if f.Key == key {
|
||||
return f.Integer
|
||||
}
|
||||
}
|
||||
t.Fatalf("日志缺少字段 %q", key)
|
||||
return 0
|
||||
}
|
||||
|
||||
// TestLogger_GeneratesTraceID 验证自动生成 trace_id 并回写响应头。
|
||||
func TestLogger_GeneratesTraceID(t *testing.T) {
|
||||
r, logs := newObservedRouter(t)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("状态码 = %d, 期望 200", w.Code)
|
||||
}
|
||||
traceID := w.Header().Get(TraceIDHeader)
|
||||
if len(traceID) != 16 {
|
||||
t.Errorf("响应头 X-Trace-Id = %q, 期望 16 字符 hex", traceID)
|
||||
}
|
||||
if logs.Len() != 1 {
|
||||
t.Fatalf("日志条数 = %d, 期望 1", logs.Len())
|
||||
}
|
||||
e := logs.All()[0]
|
||||
if strField(t, e, "trace_id") != traceID {
|
||||
t.Errorf("日志 trace_id 与响应头不一致")
|
||||
}
|
||||
if intField(t, e, "status") != 200 {
|
||||
t.Errorf("日志 status = %d, 期望 200", intField(t, e, "status"))
|
||||
}
|
||||
if intField(t, e, "latency") <= 0 {
|
||||
t.Error("日志 latency 应大于 0")
|
||||
}
|
||||
if strField(t, e, "client_ip") == "" || strField(t, e, "method") == "" || strField(t, e, "path") == "" {
|
||||
t.Error("日志缺少 client_ip/method/path 字段")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLogger_PropagatesTraceID 验证上游 X-Trace-Id 透传一致。
|
||||
func TestLogger_PropagatesTraceID(t *testing.T) {
|
||||
r, logs := newObservedRouter(t)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
||||
req.Header.Set(TraceIDHeader, "abc123def4567890")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if got := w.Header().Get(TraceIDHeader); got != "abc123def4567890" {
|
||||
t.Errorf("X-Trace-Id 透传失败: %q", got)
|
||||
}
|
||||
if strField(t, logs.All()[0], "trace_id") != "abc123def4567890" {
|
||||
t.Error("日志应使用上游传入的 trace_id")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLogger_LevelByStatus 验证 5xx 走 Error 级别。
|
||||
func TestLogger_LevelByStatus(t *testing.T) {
|
||||
r, logs := newObservedRouter(t)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/boom", nil))
|
||||
|
||||
if logs.Len() != 1 || logs.All()[0].Level != zap.ErrorLevel {
|
||||
t.Errorf("5xx 请求应以 Error 级别记录,实际: %v", logs.All())
|
||||
}
|
||||
if intField(t, logs.All()[0], "status") != 500 {
|
||||
t.Errorf("日志 status = %d, 期望 500", intField(t, logs.All()[0], "status"))
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewTraceID 验证追踪 ID 格式与唯一性。
|
||||
func TestNewTraceID(t *testing.T) {
|
||||
seen := make(map[string]struct{}, 1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
id := NewTraceID()
|
||||
if len(id) != 16 {
|
||||
t.Fatalf("trace id 长度 = %d, 期望 16", len(id))
|
||||
}
|
||||
if _, dup := seen[id]; dup {
|
||||
t.Fatalf("trace id 重复: %s", id)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"go-sms/internal/pkg/metrics"
|
||||
)
|
||||
|
||||
// Metrics HTTP 指标埋点中间件(Day 13):
|
||||
// 请求总数(method/route/status)、耗时分布(method/route)、在途数(Gauge)。
|
||||
// route 使用路由模板(c.FullPath)避免路径参数导致标签基数爆炸;
|
||||
// 未匹配路由记为 "unmatched"。
|
||||
func Metrics() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
route := c.FullPath()
|
||||
if route == "" {
|
||||
route = "unmatched"
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
metrics.HTTPRequestsInFlight.Inc()
|
||||
c.Next()
|
||||
metrics.HTTPRequestsInFlight.Dec()
|
||||
|
||||
status := strconv.Itoa(c.Writer.Status())
|
||||
metrics.HTTPRequestsTotal.WithLabelValues(c.Request.Method, route, status).Inc()
|
||||
metrics.HTTPRequestDuration.WithLabelValues(c.Request.Method, route).
|
||||
Observe(time.Since(start).Seconds())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||
|
||||
"go-sms/internal/pkg/metrics"
|
||||
)
|
||||
|
||||
// TestMetricsMiddleware_CountsAndDuration 验证请求埋点:计数/耗时/在途回落。
|
||||
func TestMetricsMiddleware_CountsAndDuration(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(Metrics())
|
||||
r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") })
|
||||
r.GET("/boom", func(c *gin.Context) { c.String(500, "err") })
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
}
|
||||
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/boom", nil))
|
||||
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/not-exist", nil)) // unmatched
|
||||
|
||||
if got := testutil.ToFloat64(metrics.HTTPRequestsTotal.WithLabelValues("GET", "/ping", "200")); got != 3 {
|
||||
t.Errorf("/ping 200 计数 = %v, 期望 3", got)
|
||||
}
|
||||
if got := testutil.ToFloat64(metrics.HTTPRequestsTotal.WithLabelValues("GET", "/boom", "500")); got != 1 {
|
||||
t.Errorf("/boom 500 计数 = %v, 期望 1", got)
|
||||
}
|
||||
if got := testutil.ToFloat64(metrics.HTTPRequestsTotal.WithLabelValues("GET", "unmatched", "404")); got != 1 {
|
||||
t.Errorf("unmatched 计数 = %v, 期望 1", got)
|
||||
}
|
||||
// 耗时观测:>0 计数 3
|
||||
if got := testutil.CollectAndCount(metrics.HTTPRequestDuration); got == 0 {
|
||||
t.Error("耗时直方图无观测")
|
||||
}
|
||||
// 在途数回落为 0(请求结束后 Dec)
|
||||
if got := testutil.ToFloat64(metrics.HTTPRequestsInFlight); got != 0 {
|
||||
t.Errorf("在途数 = %v, 期望 0", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Package-level:权限中间件(Day 25)。
|
||||
// CasbinChecker 将 enforcer 适配为 PermissionChecker;RequirePermission 做
|
||||
// 路由级细粒度校验(401 未认证 / 403 无权限)。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"go-sms/internal/pkg/casbin"
|
||||
"go-sms/internal/pkg/errors"
|
||||
)
|
||||
|
||||
// 二级权限标记(Day 25):按账户粒度授予的敏感操作,区别于角色策略。
|
||||
// resource 统一为账号域 "account";action 为下列值。
|
||||
const (
|
||||
PermRecharge = "recharge" // 充值
|
||||
PermProxySend = "proxy_send" // 代发
|
||||
PermExport = "export" // 导出
|
||||
PermSend = "send" // 发送
|
||||
)
|
||||
|
||||
// SecondaryPermissions 全部二级权限标记(管理接口校验白名单)。
|
||||
var SecondaryPermissions = map[string]bool{
|
||||
PermRecharge: true, PermProxySend: true, PermExport: true, PermSend: true,
|
||||
}
|
||||
|
||||
// PermissionChecker 权限校验函数(由 Casbin enforcer 实现)。
|
||||
// 返回 true 表示允许访问 obj 资源的 act 操作。
|
||||
type PermissionChecker func(claims *Claims, obj, act string) bool
|
||||
|
||||
// CasbinChecker 将 Casbin enforcer 适配为 PermissionChecker:
|
||||
// sub=claims.AccountID(与 casbin_rule 的 g/p 规则匹配)。
|
||||
func CasbinChecker(e *casbin.Enforcer) PermissionChecker {
|
||||
return func(claims *Claims, obj, act string) bool {
|
||||
if e == nil || claims == nil {
|
||||
return false
|
||||
}
|
||||
ok, err := e.Check(claims.AccountID, obj, act)
|
||||
return err == nil && ok
|
||||
}
|
||||
}
|
||||
|
||||
// RequirePermission 细粒度权限校验(Day 25):
|
||||
// 依赖 AuthJWT* 先行(claims 必须已写入 Context);checker 为 nil 时仅要求已认证;
|
||||
// 未认证返回 401,未授权返回 403 ISC:403。
|
||||
func RequirePermission(checker PermissionChecker, obj, act string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
v, ok := c.Get(ContextKeyClaims)
|
||||
if !ok {
|
||||
abortUnauthorized(c, "未认证")
|
||||
return
|
||||
}
|
||||
claims, _ := v.(*Claims)
|
||||
if checker != nil && !checker(claims, obj, act) {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
"code": errors.Forbidden.Code,
|
||||
"msg": "无权限执行该操作",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// permission_test.go Day 25:Casbin 权限中间件测试。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/pkg/casbin"
|
||||
)
|
||||
|
||||
// TestCasbinChecker 真实 enforcer 适配:admin 通配放行 / 未授权 403 / nil 拒绝。
|
||||
func TestCasbinChecker(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e, err := casbin.New(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.AddPolicy("admin", "*", "*"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.BindRole(1, "admin"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checker := CasbinChecker(e)
|
||||
|
||||
adminTok, _ := SignAccessToken("sec", 1, "admin", "admin", time.Minute)
|
||||
custTok, _ := SignAccessToken("sec", 2, "cust", "customer", time.Minute)
|
||||
|
||||
hit := func(tok string) int {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/e", AuthJWT("sec"), RequirePermission(checker, "account", PermExport), func(c *gin.Context) {
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/e", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
r.ServeHTTP(w, req)
|
||||
return w.Code
|
||||
}
|
||||
if code := hit(adminTok); code != http.StatusOK {
|
||||
t.Fatalf("admin 导出 = %d, 期望 200(*/\u002a 通配)", code)
|
||||
}
|
||||
if code := hit(custTok); code != http.StatusForbidden {
|
||||
t.Fatalf("customer 导出 = %d, 期望 403", code)
|
||||
}
|
||||
// 授予二级权限后放行
|
||||
if _, err := e.GrantPermission(2, "account", PermExport); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code := hit(custTok); code != http.StatusOK {
|
||||
t.Fatalf("授予 export 后 = %d, 期望 200", code)
|
||||
}
|
||||
// nil enforcer → checker 恒拒
|
||||
if CasbinChecker(nil)(&Claims{AccountID: 1}, "account", "export") {
|
||||
t.Fatal("nil enforcer 应拒绝")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"go-sms/internal/pkg/errors"
|
||||
)
|
||||
|
||||
// tokenBucketScript 令牌桶 Lua 脚本(原子:填充+消费+过期)。
|
||||
// KEYS[1]=桶键;ARGV:容量、速率(个/秒)、当前毫秒、本次消耗。
|
||||
const tokenBucketScript = `
|
||||
local capacity = tonumber(ARGV[1])
|
||||
local rate = tonumber(ARGV[2])
|
||||
local now = tonumber(ARGV[3])
|
||||
local requested = tonumber(ARGV[4])
|
||||
|
||||
local bucket = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
|
||||
local tokens = tonumber(bucket[1])
|
||||
local ts = tonumber(bucket[2])
|
||||
if tokens == nil then
|
||||
tokens = capacity
|
||||
ts = now
|
||||
end
|
||||
|
||||
-- 按流逝时间填充令牌
|
||||
local elapsed = now - ts
|
||||
if elapsed > 0 then
|
||||
tokens = math.min(capacity, tokens + elapsed * rate / 1000)
|
||||
ts = now
|
||||
end
|
||||
|
||||
local allowed = tokens >= requested
|
||||
if allowed then
|
||||
tokens = tokens - requested
|
||||
end
|
||||
|
||||
redis.call('HMSET', KEYS[1], 'tokens', tokens, 'ts', ts)
|
||||
-- 桶空闲过期:容量耗尽所需时间的 2 倍(上限 1 小时)
|
||||
local ttl = math.min(3600000, math.ceil(capacity / math.max(rate, 0.001)) * 2000)
|
||||
redis.call('PEXPIRE', KEYS[1], ttl)
|
||||
if allowed then return 1 else return 0 end
|
||||
`
|
||||
|
||||
// RateLimitConfig 分布式限流配置。
|
||||
type RateLimitConfig struct {
|
||||
Capacity int64 // 桶容量(突发上限)
|
||||
Rate int64 // 每秒填充令牌数
|
||||
// KeyFunc 限流维度键(如同 IP、同账号、全局);nil 表示全局单桶。
|
||||
KeyFunc func(c *gin.Context) string
|
||||
// CountOf 本次消耗令牌数(缺省 1;Day50 修复:批量接口须按号码数扣,
|
||||
// 每请求 1 令牌使 100 号/req 的限速形同虚设——限流口径=号码/秒)。
|
||||
CountOf func(c *gin.Context) int64
|
||||
// KeyPrefix Redis 键前缀。
|
||||
KeyPrefix string
|
||||
}
|
||||
|
||||
// RateLimit Redis 令牌桶分布式限流中间件:超限返回 429。
|
||||
// 多实例共享同一 Redis,天然形成集群级限流。
|
||||
func RateLimit(client redis.UniversalClient, cfg RateLimitConfig) gin.HandlerFunc {
|
||||
if cfg.KeyPrefix == "" {
|
||||
cfg.KeyPrefix = "go-sms:ratelimit"
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
key := cfg.KeyPrefix
|
||||
if cfg.KeyFunc != nil {
|
||||
key += ":" + cfg.KeyFunc(c)
|
||||
}
|
||||
requested := int64(1)
|
||||
if cfg.CountOf != nil {
|
||||
if n := cfg.CountOf(c); n > 0 {
|
||||
requested = n
|
||||
}
|
||||
}
|
||||
allowed, err := takeToken(c.Request.Context(), client, key, cfg.Capacity, cfg.Rate, requested)
|
||||
if err != nil {
|
||||
// Redis 故障时放行(可用性优先),记录于响应头便于排查
|
||||
c.Header("X-RateLimit-Error", "backend-unavailable")
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if !allowed {
|
||||
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{
|
||||
"code": errors.RateLimited.Code,
|
||||
"msg": errors.RateLimited.Msg,
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// takeToken 消费 n 个令牌;返回是否放行。
|
||||
func takeToken(ctx context.Context, client redis.UniversalClient, key string, capacity, rate, requested int64) (bool, error) {
|
||||
res, err := client.Eval(ctx, tokenBucketScript, []string{key},
|
||||
capacity, rate, time.Now().UnixMilli(), requested).Int64()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("middleware: 限流脚本执行失败: %w", err)
|
||||
}
|
||||
return res == 1, nil
|
||||
}
|
||||
|
||||
// ByClientIP 常用量纲:按来源 IP 限流。
|
||||
func ByClientIP(c *gin.Context) string { return c.ClientIP() }
|
||||
|
||||
// RateLimitDynamic Day 46:动态容量令牌桶(外部 API 账号级限速)。
|
||||
// 与 RateLimit 的差异:容量/速率在请求期从 gin.Context 取(验签中间件注入的
|
||||
// speed_limit),无配置时用 defaults 兜底;Redis 故障放行(可用性优先)。
|
||||
// 超限响应格式走外部协议(code=99 超限文案由调用方包内约定,此处保持 ISC 码
|
||||
// 便于内部探测,正式协议由 handler 层覆写)。
|
||||
type RateLimitDynamicConfig struct {
|
||||
KeyFunc func(c *gin.Context) string // 限流维度键(如账号 ID)
|
||||
CapacityOf func(c *gin.Context) int64 // 桶容量(缺省 DefaultDynamicCap)
|
||||
RateOf func(c *gin.Context) int64 // 每秒填充(缺省 DefaultDynamicRate)
|
||||
// CountOf 本次消耗令牌数(缺省 1;Day50:批量接口按号码数扣,口径=号码/秒)。
|
||||
CountOf func(c *gin.Context) int64
|
||||
KeyPrefix string
|
||||
}
|
||||
|
||||
// 外部 API 账号桶缺省兜底(账号未配置 speed_limit 时)。
|
||||
const (
|
||||
DefaultDynamicCap = 500
|
||||
DefaultDynamicRate = 100
|
||||
)
|
||||
|
||||
// RateLimitDynamic 账号级动态限流(需在验签之后挂载,键=账号 ID)。
|
||||
func RateLimitDynamic(client redis.UniversalClient, cfg RateLimitDynamicConfig) gin.HandlerFunc {
|
||||
if cfg.KeyPrefix == "" {
|
||||
cfg.KeyPrefix = "go-sms:ratelimit:openapi"
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
key := cfg.KeyPrefix
|
||||
if cfg.KeyFunc != nil {
|
||||
key += ":" + cfg.KeyFunc(c)
|
||||
}
|
||||
var capacity, rate int64 = DefaultDynamicCap, DefaultDynamicRate
|
||||
if cfg.CapacityOf != nil {
|
||||
if v := cfg.CapacityOf(c); v > 0 {
|
||||
capacity = v
|
||||
}
|
||||
}
|
||||
if cfg.RateOf != nil {
|
||||
if v := cfg.RateOf(c); v > 0 {
|
||||
rate = v
|
||||
}
|
||||
}
|
||||
requested := int64(1)
|
||||
if cfg.CountOf != nil {
|
||||
if n := cfg.CountOf(c); n > 0 {
|
||||
requested = n
|
||||
}
|
||||
}
|
||||
allowed, err := takeToken(c.Request.Context(), client, key, capacity, rate, requested)
|
||||
if err != nil {
|
||||
c.Header("X-RateLimit-Error", "backend-unavailable")
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if !allowed {
|
||||
c.Header("Retry-After", "1")
|
||||
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{
|
||||
"code": "RATE_LIMITED",
|
||||
"message": "提交频率超限,请降低速率后重试",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// newMiniRedis 启动 miniredis 并返回直连客户端。
|
||||
func newMiniRedis(t *testing.T) redis.UniversalClient {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
return redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
}
|
||||
|
||||
// newRateLimitRouter 构建带限流中间件与 /ping 路由的测试引擎。
|
||||
func newRateLimitRouter(c redis.UniversalClient, cfg RateLimitConfig) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(RateLimit(c, cfg))
|
||||
r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") })
|
||||
return r
|
||||
}
|
||||
|
||||
// TestRateLimit_RejectWhenExceeded 验收项:超过桶容量后返回 429。
|
||||
func TestRateLimit_RejectWhenExceeded(t *testing.T) {
|
||||
c := newMiniRedis(t)
|
||||
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 3, Rate: 1}) // 容量 3,1 个/秒
|
||||
|
||||
code200, code429 := 0, 0
|
||||
for i := 0; i < 6; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
switch w.Code {
|
||||
case 200:
|
||||
code200++
|
||||
case 429:
|
||||
code429++
|
||||
default:
|
||||
t.Fatalf("意外状态码 %d", w.Code)
|
||||
}
|
||||
}
|
||||
if code200 != 3 || code429 != 3 {
|
||||
t.Errorf("容量3发6次: 200x%d + 429x%d, 期望 3+3", code200, code429)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimit_RefillOverTime 验证令牌按速率恢复。
|
||||
func TestRateLimit_RefillOverTime(t *testing.T) {
|
||||
c := newMiniRedis(t)
|
||||
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 2, Rate: 10}) // 每秒回 10 个
|
||||
|
||||
for i := 0; i < 2; i++ { // 耗尽
|
||||
r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != 429 {
|
||||
t.Fatalf("耗尽后应为 429, 实际 %d", w.Code)
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond) // 回填 ~1.5 个
|
||||
w = httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("回填后应放行, 实际 %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimit_PerKeyIsolation 验证按 KeyFunc 维度独立限流。
|
||||
func TestRateLimit_PerKeyIsolation(t *testing.T) {
|
||||
c := newMiniRedis(t)
|
||||
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 1, Rate: 1, KeyFunc: func(g *gin.Context) string {
|
||||
return g.Query("uid")
|
||||
}})
|
||||
|
||||
hit := func(uid string) int {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping?uid="+uid, nil)
|
||||
r.ServeHTTP(w, req)
|
||||
return w.Code
|
||||
}
|
||||
if hit("a") != 200 || hit("a") != 429 {
|
||||
t.Error("同 uid 第二次应 429")
|
||||
}
|
||||
if hit("b") != 200 {
|
||||
t.Error("不同 uid 应独立限流(200)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimit_BackendDownFailsOpen 验证 Redis 故障时放行(可用性优先)。
|
||||
func TestRateLimit_BackendDownFailsOpen(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
mr.Close() // 立即关闭后端
|
||||
|
||||
r := newRateLimitRouter(c, RateLimitConfig{Capacity: 1, Rate: 1})
|
||||
for i := 0; i < 3; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("后端故障应放行, 实际 %d", w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go-sms/internal/pkg/errors"
|
||||
)
|
||||
|
||||
// Recovery 全局异常恢复中间件(Day 15,替代 gin.Recovery):
|
||||
// 捕获 panic → 记录堆栈与请求上下文(含 trace_id)→ 返回
|
||||
// 500 {"code":"ISC:500","msg":"系统内部错误"},进程不崩溃。
|
||||
func Recovery(lg *zap.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fields := []zap.Field{
|
||||
zap.Any("panic", r),
|
||||
zap.String("stack", string(debug.Stack())),
|
||||
zap.String("method", c.Request.Method),
|
||||
zap.String("path", c.Request.URL.Path),
|
||||
zap.Int("status_pending", c.Writer.Status()),
|
||||
}
|
||||
if v, ok := c.Get(TraceIDKey); ok {
|
||||
if id, ok := v.(string); ok {
|
||||
fields = append(fields, zap.String("trace_id", id))
|
||||
}
|
||||
}
|
||||
lg.Error("panic_recovered", fields...)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"code": errors.InternalError.Code,
|
||||
"msg": errors.InternalError.Msg,
|
||||
})
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
// TestRecovery_PanicTo500 验收:panic → 500 + ISC:500,进程不崩。
|
||||
func TestRecovery_PanicTo500(t *testing.T) {
|
||||
core, logs := observer.New(zap.ErrorLevel)
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) { c.Set(TraceIDKey, "trace-recovery-1"); c.Next() }, Recovery(zap.New(core)))
|
||||
r.GET("/boom", func(c *gin.Context) { panic("boom-day15") })
|
||||
r.GET("/ok", func(c *gin.Context) { c.String(200, "fine") })
|
||||
|
||||
// panic 路由:500 + 统一错误体,后续路由仍可用(进程未崩)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/boom", nil))
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("状态 = %d, 期望 500", w.Code)
|
||||
}
|
||||
if want := `{"code":"ISC:500","msg":"其他错误,需要人工核查"}`; w.Body.String() != want {
|
||||
t.Fatalf("响应体 = %s, 期望 %s", w.Body.String(), want)
|
||||
}
|
||||
w2 := httptest.NewRecorder()
|
||||
r.ServeHTTP(w2, httptest.NewRequest(http.MethodGet, "/ok", nil))
|
||||
if w2.Code != 200 {
|
||||
t.Fatal("panic 后引擎应继续服务")
|
||||
}
|
||||
|
||||
// 日志:panic_recovered 含堆栈与 trace_id
|
||||
if logs.Len() == 0 {
|
||||
t.Fatal("应记录 panic_recovered")
|
||||
}
|
||||
entry := logs.All()[0]
|
||||
if entry.Message != "panic_recovered" {
|
||||
t.Fatalf("日志 msg = %q", entry.Message)
|
||||
}
|
||||
hasStack, hasTrace := false, false
|
||||
for _, f := range entry.Context {
|
||||
if f.Key == "stack" && len(f.String) > 50 {
|
||||
hasStack = true
|
||||
}
|
||||
if f.Key == "trace_id" && f.String == "trace-recovery-1" {
|
||||
hasTrace = true
|
||||
}
|
||||
}
|
||||
if !hasStack || !hasTrace {
|
||||
t.Errorf("日志缺少 stack(%v)/trace_id(%v)", hasStack, hasTrace)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
sdkresource "go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/semconv/v1.26.0"
|
||||
oteltrace "go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// InitTracer 初始化 OpenTelemetry:OTLP/HTTP 上报(Jaeger 兼容)。
|
||||
// endpoint 形如 "127.0.0.1:4318"(OTLP HTTP 接收端口,不带 scheme)。
|
||||
// 返回 shutdown 供进程退出时刷盘。
|
||||
func InitTracer(ctx context.Context, endpoint, serviceName string) (func(context.Context) error, error) {
|
||||
exporter, err := otlptracehttp.New(ctx,
|
||||
otlptracehttp.WithEndpoint(endpoint),
|
||||
otlptracehttp.WithInsecure(),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("middleware: 创建 OTLP exporter 失败: %w", err)
|
||||
}
|
||||
|
||||
res, _ := sdkresource.Merge(
|
||||
sdkresource.Default(),
|
||||
sdkresource.NewWithAttributes(semconv.SchemaURL,
|
||||
semconv.ServiceName(serviceName),
|
||||
),
|
||||
)
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exporter), // 后台批量上报,不阻塞请求
|
||||
sdktrace.WithResource(res),
|
||||
)
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, // W3C traceparent(与现有 trace_id 头兼容层在 Logger 中处理)
|
||||
propagation.Baggage{},
|
||||
))
|
||||
return tp.Shutdown, nil
|
||||
}
|
||||
|
||||
// Tracer Gin 链路追踪中间件(自研,替代 otelgin):
|
||||
// 每请求一个 span,记录方法/路径/状态/耗时,并从 traceparent 头续接上游链路。
|
||||
func Tracer(serviceName string) gin.HandlerFunc {
|
||||
tracer := otel.GetTracerProvider().Tracer("go-sms/gin")
|
||||
propagator := otel.GetTextMapPropagator()
|
||||
return func(c *gin.Context) {
|
||||
ctx := propagator.Extract(c.Request.Context(), propagation.HeaderCarrier(c.Request.Header))
|
||||
ctx, span := tracer.Start(ctx, spanName(c))
|
||||
defer span.End()
|
||||
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("http.method", c.Request.Method),
|
||||
attribute.String("http.route", c.FullPath()),
|
||||
attribute.String("http.target", c.Request.URL.Path),
|
||||
attribute.Int("http.status_code", c.Writer.Status()),
|
||||
attribute.Int64("http.duration_ms", time.Since(start).Milliseconds()),
|
||||
attribute.String("net.peer.ip", c.ClientIP()),
|
||||
)
|
||||
if len(c.Errors) > 0 {
|
||||
span.SetAttributes(attribute.String("gin.errors", c.Errors.String()))
|
||||
}
|
||||
// 回写 traceparent,便于响应侧关联
|
||||
if sc := oteltrace.SpanContextFromContext(ctx); sc.IsValid() {
|
||||
c.Header("Traceparent", fmt.Sprintf("00-%s-%s-01", sc.TraceID(), sc.SpanID()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// spanName 生成 span 名称(method + 命中路由)。
|
||||
func spanName(c *gin.Context) string {
|
||||
if p := c.FullPath(); p != "" {
|
||||
return c.Request.Method + " " + p
|
||||
}
|
||||
return c.Request.Method + " " + c.Request.URL.Path
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
)
|
||||
|
||||
// TestTracer_CreatesSpan 验证中间件产生带 HTTP 属性的 span。
|
||||
func TestTracer_CreatesSpan(t *testing.T) {
|
||||
// 注入内存 span 记录器
|
||||
rec := tracetest.NewSpanRecorder()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(rec))
|
||||
backup := otel.GetTracerProvider()
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(backup)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(Tracer("go-sms-test"))
|
||||
r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") })
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/ping", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("状态码 = %d", w.Code)
|
||||
}
|
||||
|
||||
spans := rec.Ended()
|
||||
if len(spans) != 1 {
|
||||
t.Fatalf("span 数 = %d, 期望 1", len(spans))
|
||||
}
|
||||
s := spans[0]
|
||||
if s.Name() != "GET /ping" {
|
||||
t.Errorf("span 名称 = %q, 期望 \"GET /ping\"", s.Name())
|
||||
}
|
||||
attr := map[string]any{}
|
||||
for _, a := range s.Attributes() {
|
||||
attr[string(a.Key)] = a.Value.AsInterface()
|
||||
}
|
||||
if attr["http.method"] != "GET" || attr["http.status_code"] != int64(200) {
|
||||
t.Errorf("span 属性缺失: %v", attr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// AccountUser 账号(用户)。
|
||||
type AccountUser struct {
|
||||
Model
|
||||
Username string `gorm:"size:64;uniqueIndex:uk_username;not null" json:"username"`
|
||||
PasswordHash string `gorm:"size:128;not null" json:"-"`
|
||||
RealName string `gorm:"size:64" json:"real_name"`
|
||||
RealIDCard string `gorm:"size:64" json:"-"` // Day 28:证件号(详情接口脱敏输出,JSON 不直接暴露)
|
||||
RealnameStatus int8 `gorm:"default:0" json:"realname_status"` // Day 28:0 待审核/1 通过/2 驳回
|
||||
Role string `gorm:"size:32;index:idx_role_status,priority:1;default:customer" json:"role"`
|
||||
ParentID int64 `gorm:"index:idx_parent;default:0" json:"parent_id"`
|
||||
Path string `gorm:"size:512;index:idx_path" json:"path"` // Day 28:物化路径 "/1/5/9/",切换上级级联重写
|
||||
IsStarred int8 `gorm:"default:0" json:"is_starred"` // Day 28:标星(列表置顶)
|
||||
Status int8 `gorm:"index:idx_role_status,priority:2;default:1" json:"status"`
|
||||
IPBind string `gorm:"size:256" json:"ip_bind"`
|
||||
Balance int64 `json:"balance"`
|
||||
CreditBalance int64 `json:"credit_balance"`
|
||||
SvcTeam string `gorm:"size:64" json:"svc_team"`
|
||||
ExpiredAt *time.Time `json:"expired_at"`
|
||||
LastLoginAt *time.Time `json:"last_login_at"`
|
||||
LastLoginIP string `gorm:"size:45" json:"last_login_ip"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
}
|
||||
|
||||
func (AccountUser) TableName() string { return "account_user" }
|
||||
|
||||
// AccountConfig 账号业务配置(每账号每业务一行)。
|
||||
type AccountConfig struct {
|
||||
Model
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_biz,priority:1;not null" json:"account_id"`
|
||||
Business string `gorm:"size:16;uniqueIndex:uk_account_biz,priority:2;default:sms" json:"business"`
|
||||
AuditMode int8 `json:"audit_mode"` // 0免审 1人工审核 2自动审核(黑词)
|
||||
SmsType string `gorm:"size:32" json:"sms_type"`
|
||||
FeeMode int8 `json:"fee_mode"`
|
||||
FailRefund int8 `gorm:"default:1" json:"fail_refund"`
|
||||
DayLimit int `json:"day_limit"`
|
||||
SpeedLimit int `json:"speed_limit"`
|
||||
ExtCode string `gorm:"size:32" json:"ext_code"`
|
||||
ApiSecret string `gorm:"size:64" json:"-"` // Day 46:接口密钥(存 MD5(secret) 32hex;明文仅开通时返回一次)
|
||||
SignLimit string `gorm:"size:512" json:"sign_limit"`
|
||||
ReportURL string `gorm:"size:255" json:"report_url"`
|
||||
UplinkURL string `gorm:"size:255" json:"uplink_url"`
|
||||
Protocol string `gorm:"size:16;default:http" json:"protocol"`
|
||||
|
||||
CmppMaxConns int `json:"cmpp_max_conns"` // Day 62:CMPP 每账号最大连接数(0=系统默认)
|
||||
CmppSpeed int `json:"cmpp_speed"` // Day 62:CMPP 单链提交限速 条/s(0=系统默认)
|
||||
}
|
||||
|
||||
func (AccountConfig) TableName() string { return "account_config" }
|
||||
|
||||
// AccountPrice 用户分时单价。
|
||||
type AccountPrice struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"index:idx_account_biz_time,priority:1" json:"account_id"`
|
||||
Business string `gorm:"size:16;index:idx_account_biz_time,priority:2;default:sms" json:"business"`
|
||||
Price float64 `gorm:"type:decimal(10,4)" json:"price"`
|
||||
EffectiveAt time.Time `gorm:"index:idx_account_biz_time,priority:3" json:"effective_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (AccountPrice) TableName() string { return "account_price" }
|
||||
|
||||
// AccountRole 角色。
|
||||
type AccountRole struct {
|
||||
Model
|
||||
Code string `gorm:"size:32;uniqueIndex:uk_role_code" json:"code"`
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
IsBuiltIn int8 `gorm:"default:0" json:"is_built_in"`
|
||||
}
|
||||
|
||||
func (AccountRole) TableName() string { return "account_role" }
|
||||
|
||||
// AccountPermission 权限点(resource:action)。
|
||||
type AccountPermission struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Code string `gorm:"size:64;uniqueIndex:uk_permission_code" json:"code"`
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
}
|
||||
|
||||
func (AccountPermission) TableName() string { return "account_permission" }
|
||||
|
||||
// AccountRolePermission 角色-权限关联。
|
||||
type AccountRolePermission struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
RoleID int64 `gorm:"uniqueIndex:uk_role_perm,priority:1" json:"role_id"`
|
||||
PermissionID int64 `gorm:"uniqueIndex:uk_role_perm,priority:2" json:"permission_id"`
|
||||
}
|
||||
|
||||
func (AccountRolePermission) TableName() string { return "account_role_permission" }
|
||||
|
||||
// AccountBalanceLog 余额变动日志(充值/扣费/返还/调整)。
|
||||
type AccountBalanceLog struct {
|
||||
LogModel
|
||||
AccountID int64 `gorm:"index:idx_account_time,priority:1" json:"account_id"`
|
||||
BizType string `gorm:"size:16" json:"biz_type"` // recharge/credit/deduct/refund/adjust
|
||||
Amount int64 `json:"amount"` // 正负
|
||||
BalanceAfter int64 `json:"balance_after"`
|
||||
RefID string `gorm:"size:64" json:"ref_id"`
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
}
|
||||
|
||||
func (AccountBalanceLog) TableName() string { return "account_balance_log" }
|
||||
@@ -0,0 +1,53 @@
|
||||
// Package entity GORM 实体模型(Day 11):与 scripts/init_schema.sql 一一对应。
|
||||
// 命名:表名 <module>_<entity>;通用字段见 Model / SnowflakeModel。
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Model 通用模型:自增主键 + 时间戳 + 软删除。
|
||||
type Model struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
// LogModel 日志/统计模型:自增主键 + 创建时间,无软删(高吞吐写入)。
|
||||
type LogModel struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
// SnowflakeModel 雪花主键模型(message_detail 等全局唯一明细)。
|
||||
type SnowflakeModel struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
// All 返回全部实体(AutoMigrate / schema 校验用),顺序与 init_schema.sql 一致。
|
||||
func All() []any {
|
||||
return []any{
|
||||
// 账号体系(7)
|
||||
&AccountUser{}, &AccountConfig{}, &AccountPrice{}, &AccountRole{},
|
||||
&AccountPermission{}, &AccountRolePermission{}, &AccountBalanceLog{},
|
||||
// 通道体系(7)
|
||||
&ChannelChannel{}, &ChannelUpstream{}, &ChannelPrice{}, &ChannelGroup{},
|
||||
&ChannelGroupItem{}, &ChannelBlockedCode{}, &ChannelLog{},
|
||||
// 路由与策略(5)
|
||||
&RouteRule{}, &PolicyBlacklist{}, &PolicyWhitelist{}, &PolicyKeyword{},
|
||||
&PolicyFrequency{},
|
||||
// 消息与审核(8)
|
||||
&MessageTask{}, &MessageDetail{}, &MessageReport{}, &MessageUplink{},
|
||||
&AuditRecord{}, &TemplateSMS{}, &SendBlockLog{}, &UplinkCode{},
|
||||
// 计费与结算(5)
|
||||
&BillingOrder{}, &BillingConsumeLog{}, &SettleCustomer{}, &SettleUpstream{},
|
||||
&SettleRecord{},
|
||||
// 统计与运维(8)
|
||||
&StatsUserDaily{}, &StatsChannelDaily{}, &SystemConfig{}, &SystemAlertRule{},
|
||||
&SystemLoginLog{}, &SystemOperateLog{}, &SystemErrorCode{}, &FeErrorLog{},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// BillingOrder 充值订单(付款审核确认后到账)。
|
||||
type BillingOrder struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
OrderNo string `gorm:"size:32;uniqueIndex:uk_order_no" json:"order_no"`
|
||||
AccountID int64 `gorm:"index:idx_account_time,priority:1" json:"account_id"`
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"`
|
||||
Count int64 `json:"count"`
|
||||
PayType string `gorm:"size:16" json:"pay_type"`
|
||||
PayStatus int8 `gorm:"index:idx_pay_status;default:0" json:"pay_status"` // 0待1到账2失败
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
PaidAt *time.Time `json:"paid_at"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_account_time,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (BillingOrder) TableName() string { return "billing_order" }
|
||||
|
||||
// BillingConsumeLog 消耗日志(按账号/业务/天)。
|
||||
type BillingConsumeLog struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_date,priority:1" json:"account_id"`
|
||||
Business string `gorm:"size:16;uniqueIndex:uk_account_date,priority:2;default:sms" json:"business"`
|
||||
StatDate time.Time `gorm:"type:date;uniqueIndex:uk_account_date,priority:3" json:"stat_date"`
|
||||
SubmitCount int `json:"submit_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (BillingConsumeLog) TableName() string { return "billing_consume_log" }
|
||||
|
||||
// SettleCustomer 下家(客户)月度结算单。
|
||||
type SettleCustomer struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SettleNo string `gorm:"size:32" json:"settle_no"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_period,priority:1" json:"account_id"`
|
||||
Period string `gorm:"size:7;uniqueIndex:uk_account_period,priority:2;index:idx_period" json:"period"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"`
|
||||
Status int8 `gorm:"default:0" json:"status"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SettleCustomer) TableName() string { return "settle_customer" }
|
||||
|
||||
// SettleUpstream 上家月度结算单。
|
||||
type SettleUpstream struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SettleNo string `gorm:"size:32" json:"settle_no"`
|
||||
UpstreamID int64 `gorm:"uniqueIndex:uk_upstream_period,priority:1" json:"upstream_id"`
|
||||
Period string `gorm:"size:7;uniqueIndex:uk_upstream_period,priority:2;index:idx_period" json:"period"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"`
|
||||
Status int8 `gorm:"default:0" json:"status"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SettleUpstream) TableName() string { return "settle_upstream" }
|
||||
|
||||
// SettleRecord 结账记录。
|
||||
type SettleRecord struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SettleNo string `gorm:"size:32;index:idx_settle_no" json:"settle_no"`
|
||||
Direction int8 `gorm:"default:1" json:"direction"` // 1下家 2上家
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"`
|
||||
PayType string `gorm:"size:16" json:"pay_type"`
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
SettledAt time.Time `json:"settled_at"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SettleRecord) TableName() string { return "settle_record" }
|
||||
@@ -0,0 +1,17 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// SendBlockLog 发送拦截日志(Day 42 拦截器链:命中原因落库,供运营复盘/客诉追溯)。
|
||||
type SendBlockLog struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
TaskNo string `gorm:"size:64;index:idx_sbl_task" json:"task_no"`
|
||||
AccountID int64 `gorm:"index:idx_sbl_account" json:"account_id"`
|
||||
Mobile string `gorm:"size:20;index:idx_sbl_mobile" json:"mobile"`
|
||||
Interceptor string `gorm:"size:16" json:"interceptor"` // blacklist/frequency/sign/unsubscribe/region
|
||||
Code string `gorm:"size:8" json:"code"` // ISC 码
|
||||
Msg string `gorm:"size:255" json:"msg"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_sbl_created" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SendBlockLog) TableName() string { return "send_block_log" }
|
||||
@@ -0,0 +1,128 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// ChannelChannel 短信通道。
|
||||
type ChannelChannel struct {
|
||||
Model
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
Code string `gorm:"size:32;uniqueIndex:uk_channel_code" json:"code"`
|
||||
Protocol string `gorm:"size:16;index:idx_protocol_status,priority:1" json:"protocol"` // cmpp/http/sgip/smpp
|
||||
UpstreamID int64 `gorm:"index:idx_upstream" json:"upstream_id"`
|
||||
Host string `gorm:"size:128" json:"host"`
|
||||
Port int `json:"port"`
|
||||
Username string `gorm:"size:64" json:"username"`
|
||||
Password string `gorm:"size:128" json:"-"`
|
||||
ConnCount int `gorm:"default:1" json:"conn_count"`
|
||||
Speed int `json:"speed"`
|
||||
ExtLength int8 `json:"ext_length"`
|
||||
SignMode int8 `json:"sign_mode"`
|
||||
Priority int `json:"priority"`
|
||||
Weight int `gorm:"default:1" json:"weight"`
|
||||
IsDefault int8 `json:"is_default"`
|
||||
DayLimit int `json:"day_limit"`
|
||||
NightLimit int `json:"night_limit"`
|
||||
BlackKeywords string `gorm:"size:512" json:"black_keywords"`
|
||||
BlockRegions string `gorm:"size:512" json:"block_regions"`
|
||||
TemplateMode int8 `json:"template_mode"` // 上游模板ID模式({%var%})
|
||||
Status int8 `gorm:"index:idx_protocol_status,priority:2;default:1" json:"status"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
|
||||
// ---- 协议接入参数(Day 31:测试工具/连接池消费) ----
|
||||
SpID string `gorm:"size:6" json:"sp_id"` // 企业代码(CMPP SourceAddr ≤6B/SGIP CorpID;空回退 username 兼容历史)
|
||||
ProtocolVersion int8 `gorm:"default:20" json:"protocol_version"` // cmpp:20|30(3.0 未实现) sgip:12 smpp:34
|
||||
HeartbeatInterval int `gorm:"default:30" json:"heartbeat_interval"` // 心跳周期(秒;cmpp active_test/smpp enquire_link)
|
||||
ReconnectMax int `gorm:"default:30" json:"reconnect_max"` // 重连退避上限(秒)
|
||||
ServiceID string `gorm:"size:16" json:"service_id"` // 服务代码(CMPP ServiceID ≤10B;空用 TEST)
|
||||
|
||||
// ---- 运营属性(Day 31:表单存储/回显;账务阶段消费) ----
|
||||
Carrier string `gorm:"size:24" json:"carrier"` // 运营商(逗号分隔 cmcc,cucc,ctcc)
|
||||
BillingLength int `gorm:"default:70" json:"billing_length"` // 计费长度(字/条)
|
||||
PaymentType string `gorm:"size:16" json:"payment_type"` // 付款方式 prepay_monthly/postpaid_monthly/prepay_realtime
|
||||
BillingMode string `gorm:"size:16" json:"billing_mode"` // 计费方式 success_unknown/success/all
|
||||
Price float64 `gorm:"type:decimal(10,4)" json:"price"` // 通道价格(元/条)
|
||||
Category string `gorm:"size:32" json:"category"` // 通道分类(自由文本)
|
||||
MOForwardChannelID int64 `gorm:"default:0" json:"mo_forward_channel_id"` // 上行组合通道(0=不转发)
|
||||
NumberRule int8 `gorm:"default:0" json:"number_rule"` // 号码组装规则 0通道号+签名扩展+用户扩展+自定义 1通道号+签名扩展+用户扩展 2通道号+用户扩展 3仅通道号
|
||||
CodeTruncate int8 `json:"code_truncate"` // 通道号截断位数(0=不截断)
|
||||
ExtParams string `gorm:"size:255" json:"ext_params"` // 扩展参数(自定义 KV)
|
||||
ServiceRemark string `gorm:"size:255" json:"service_remark"` // 客服备注
|
||||
}
|
||||
|
||||
func (ChannelChannel) TableName() string { return "channel_channel" }
|
||||
|
||||
// ChannelUpstream 上家(通道方)。
|
||||
type ChannelUpstream struct {
|
||||
Model
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
Contact string `gorm:"size:64" json:"contact"`
|
||||
Phone string `gorm:"size:32" json:"phone"`
|
||||
Category string `gorm:"size:32" json:"category"`
|
||||
Status int8 `gorm:"default:1" json:"status"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
}
|
||||
|
||||
func (ChannelUpstream) TableName() string { return "channel_upstream" }
|
||||
|
||||
// ChannelPrice 通道分时成本价。
|
||||
type ChannelPrice struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
ChannelID int64 `gorm:"index:idx_channel_time,priority:1" json:"channel_id"`
|
||||
Price float64 `gorm:"type:decimal(10,4)" json:"price"`
|
||||
EffectiveAt time.Time `gorm:"index:idx_channel_time,priority:2" json:"effective_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (ChannelPrice) TableName() string { return "channel_price" }
|
||||
|
||||
// ChannelGroup 通道组(四步路由容器)。
|
||||
type ChannelGroup struct {
|
||||
Model
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
Status int8 `gorm:"default:1" json:"status"`
|
||||
}
|
||||
|
||||
func (ChannelGroup) TableName() string { return "channel_group" }
|
||||
|
||||
// ChannelGroupItem 通道组成员(路由步骤参数)。
|
||||
type ChannelGroupItem struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
GroupID int64 `gorm:"uniqueIndex:uk_group_channel,priority:1" json:"group_id"`
|
||||
ChannelID int64 `gorm:"uniqueIndex:uk_group_channel,priority:2;index:idx_channel" json:"channel_id"`
|
||||
Step int8 `gorm:"default:3" json:"step"` // 1过滤 2优先 3权重 4兜底
|
||||
Priority int `json:"priority"`
|
||||
Weight int `gorm:"default:1" json:"weight"`
|
||||
Region string `gorm:"size:32" json:"region"`
|
||||
Carrier string `gorm:"size:16" json:"carrier"`
|
||||
Keyword string `gorm:"size:128" json:"keyword"`
|
||||
}
|
||||
|
||||
func (ChannelGroupItem) TableName() string { return "channel_group_item" }
|
||||
|
||||
// ChannelBlockedCode 屏蔽码号(按通道+码号直接拦截)。
|
||||
type ChannelBlockedCode struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
ChannelID int64 `gorm:"uniqueIndex:uk_channel_code,priority:1" json:"channel_id"`
|
||||
Code string `gorm:"size:64;uniqueIndex:uk_channel_code,priority:2" json:"code"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (ChannelBlockedCode) TableName() string { return "channel_blocked_code" }
|
||||
|
||||
// ChannelLog 通道日志(A-3.14,Day 40):连接事件逐条;提交/回执按窗口聚合
|
||||
// (防 3000 TPS 下逐条 INSERT 打爆 MySQL——Day 39.6 容量规划口径,落库走异步批量)。
|
||||
type ChannelLog struct {
|
||||
Model
|
||||
ChannelID int64 `gorm:"index:idx_clog_channel,priority:1" json:"channel_id"`
|
||||
Kind string `gorm:"size:16;index:idx_clog_kind" json:"kind"` // connect|disconnect|stop|submit|submit_fail|deliver|deliver_fail|alert
|
||||
Level int8 `gorm:"default:0" json:"level"` // 0=info 1=warn 2=error
|
||||
MsgID string `gorm:"size:32" json:"msg_id,omitempty"`
|
||||
Detail string `gorm:"size:512" json:"detail,omitempty"`
|
||||
Count int `gorm:"default:1" json:"count"` // 聚合行:窗口内条数(逐条=1)
|
||||
FailCount int `gorm:"default:0" json:"fail_count"` // 聚合行:窗口内失败数
|
||||
LatencyMs int `gorm:"default:0" json:"latency_ms"` // 聚合行:窗口均值(逐条=本次)
|
||||
}
|
||||
|
||||
func (ChannelLog) TableName() string { return "channel_log" }
|
||||
@@ -0,0 +1,17 @@
|
||||
// fe_error_log.go Day 53(runbook §6.4 P0):前端错误上报落库。
|
||||
// request.ts 拦截器(HTTP 5xx/网络层)+window.onerror/unhandledrejection(JS 运行时)
|
||||
// → POST /fe-errors;Day43 vite proxy 死端口案例后,前端层故障后端零感知的补洞。
|
||||
package entity
|
||||
|
||||
// FeErrorLog 前端错误记录(kind/js 堆栈/页面 URL/UA/trace_id 关联后端日志)。
|
||||
type FeErrorLog struct {
|
||||
LogModel
|
||||
Kind string `gorm:"size:16;index:idx_kind_time,priority:1" json:"kind"` // js|http|unhandled
|
||||
PageURL string `gorm:"size:512" json:"page_url"`
|
||||
Message string `gorm:"size:1024" json:"message"`
|
||||
Stack string `gorm:"type:text" json:"stack"`
|
||||
UA string `gorm:"size:512" json:"ua"`
|
||||
TraceID string `gorm:"size:64;index:idx_fe_trace" json:"trace_id"`
|
||||
}
|
||||
|
||||
func (FeErrorLog) TableName() string { return "fe_error_log" }
|
||||
@@ -0,0 +1,26 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// LongSmsPending 入向长短信挂起组(Day 71:聚合等待窗 10s→72h+不齐不发挂起制)。
|
||||
// 一行=一封未收齐的长短信(grouper 首片 INSERT/续片 UPDATE/到齐 DELETE);
|
||||
// 72h 扫描器(longsms_reaper)扫 expire_at 到期行:不齐→失败终态(task LP+组ID/
|
||||
// detail id=组ID/UNDELIV 回执);齐(删行失败兜底)→补 merged(detail 存在性防重)。
|
||||
// 进程重启 restore 全量回内存(grouper 持续等片,72h 内补齐仍可正常受理)。
|
||||
// Frags 为 {"idx":"文本"} JSON(锁内序列化全量替换,同组片串行到达无并发写)。
|
||||
type LongSmsPending struct {
|
||||
GroupID uint64 `gorm:"primaryKey" json:"group_id"` // 组ID=首片雪花(N 片 Resp/失败 detail 复用)
|
||||
AccountID int64 `json:"account_id"`
|
||||
Username string `gorm:"size:64" json:"username"` // source_addr(auth 快照;restore 重载)
|
||||
SrcID string `gorm:"size:32" json:"src_id"`
|
||||
Mobile string `gorm:"size:20" json:"mobile"`
|
||||
ExtCode string `gorm:"size:32" json:"ext_code"`
|
||||
Ref uint8 `json:"ref"` // 组键 ref(restore 组键还原)
|
||||
Total uint8 `json:"total"` // 客户端 pk_total
|
||||
Ucs2 bool `json:"ucs2"` // Day 70:首片 MsgFmt 快照(计费口径)
|
||||
Frags string `gorm:"type:text" json:"frags"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
ExpireAt time.Time `gorm:"index:idx_lsp_expire" json:"expire_at"` // created+window
|
||||
}
|
||||
|
||||
func (LongSmsPending) TableName() string { return "longsms_pending" }
|
||||
@@ -0,0 +1,140 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// MessageTask 发送任务(批次)。
|
||||
type MessageTask struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
TaskNo string `gorm:"size:32;uniqueIndex:uk_task_no" json:"task_no"`
|
||||
AccountID int64 `gorm:"index:idx_account_time,priority:1" json:"account_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
SendType int8 `gorm:"default:1" json:"send_type"` // 1群发 2个性 3定时 4文件
|
||||
Content string `gorm:"type:text" json:"content"`
|
||||
Sign string `gorm:"size:64" json:"sign"`
|
||||
MobileCount int `json:"mobile_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
UnknownCount int `json:"unknown_count"`
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"`
|
||||
AuditStatus int8 `gorm:"index:idx_audit,priority:1;default:0" json:"audit_status"` // 0待1过2拒3免4超时
|
||||
Status int8 `gorm:"index:idx_status_schedule,priority:1;default:0" json:"status"`
|
||||
ScheduleAt *time.Time `gorm:"index:idx_status_schedule,priority:2" json:"schedule_at"`
|
||||
FinishedAt *time.Time `json:"finished_at"`
|
||||
SubmitIP string `gorm:"size:45" json:"submit_ip"`
|
||||
CallData string `gorm:"size:64" json:"call_data"` // Day 46:外部 API 回传数据(回执推送时带回)
|
||||
Business string `gorm:"size:16;index:idx_business;default:sms" json:"business"` // Day 49:sms/video(biz_type 路由,记录隔离)
|
||||
Source int8 `gorm:"default:1" json:"source"` // Day 60(D24-25):受理来源 1API 2CMPP 3SMPP 4WEB(迁移 v14)
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_account_time,priority:2;index:idx_audit,priority:2" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (MessageTask) TableName() string { return "message_task" }
|
||||
|
||||
// MessageDetail 下行明细(雪花ID 全局唯一,回执配对键;热数据5天)。
|
||||
type MessageDetail struct {
|
||||
SnowflakeModel
|
||||
TaskID int64 `gorm:"index:idx_task" json:"task_id"`
|
||||
AccountID int64 `gorm:"index:idx_account_mobile,priority:1" json:"account_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
Mobile string `gorm:"size:20;index:idx_account_mobile,priority:2" json:"mobile"`
|
||||
Content string `gorm:"size:1024" json:"content"`
|
||||
Province string `gorm:"size:16" json:"province"`
|
||||
Carrier string `gorm:"size:8" json:"carrier"`
|
||||
Status int8 `gorm:"index:idx_status_time,priority:1;default:0" json:"status"` // 0待发1提交2成功3失败4未知5拦截
|
||||
ErrorCode string `gorm:"size:16" json:"error_code"` // UT:xxxx/通道码
|
||||
ExtCode string `gorm:"size:32" json:"ext_code"` // Day 46:通道扩展码(请求 extcode>账号默认)
|
||||
CallData string `gorm:"size:64" json:"call_data"` // Day 46:用户回传数据(回执推送带回)
|
||||
Fee float64 `gorm:"type:decimal(10,4)" json:"fee"`
|
||||
SendAt *time.Time `json:"send_at"`
|
||||
ReportAt *time.Time `json:"report_at"`
|
||||
// Day66(D26 明细查询优化):business/source 冗余自 task——明细 Count/分页
|
||||
// 单表化(原 JOIN message_task 在 340 万行 HDD 冷缓存下逐 task 随机回表
|
||||
// 29.5s 超时);索引 idx_business_created 由迁移 v17 建,不走 tag。
|
||||
Business string `gorm:"size:16;default:sms" json:"business"` // Day 49:sms/video
|
||||
Source int8 `gorm:"default:1" json:"source"` // Day 60:1API 2CMPP 3SMPP 4WEB
|
||||
}
|
||||
|
||||
func (MessageDetail) TableName() string { return "message_detail" }
|
||||
|
||||
// MessageReportSeg 段级回执配对表(Day 56):提交侧逐段登记
|
||||
// (msg_id+mobile 唯一,合包同 msg_id 多号由 mobile 区分;长短信 1~9 片
|
||||
// 每片独立 msg_id 各一行),回执消费者按段置态,再按 detail_id 聚合
|
||||
// 段数判任务明细终态——A-5.2「长短信段级回执配对」数据地基。
|
||||
type MessageReportSeg struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
DetailID int64 `gorm:"index:idx_seg_detail,priority:1" json:"detail_id"`
|
||||
ChannelID int64 `gorm:"index:idx_seg_channel" json:"channel_id"`
|
||||
MsgID string `gorm:"size:32;uniqueIndex:uk_seg_pair,priority:1" json:"msg_id"`
|
||||
Mobile string `gorm:"size:20;uniqueIndex:uk_seg_pair,priority:2" json:"mobile"`
|
||||
PkNumber int8 `json:"pk_number"` // 段序(1 起)
|
||||
PkTotal int8 `json:"pk_total"` // 总段数(单条=1,长短信 1~9)
|
||||
Status string `gorm:"size:16;default:''" json:"status"` // 段回执三态 SUCCESS/FAILURE/UNKNOWN(空=未达)
|
||||
RawCode string `gorm:"size:32" json:"raw_code"` // 通道原始 stat(DELIVRD/GB:0001…)
|
||||
ReportAt *time.Time `gorm:"index:idx_seg_report" json:"report_at"` // 段回执到达时刻(未达=NULL)
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (MessageReportSeg) TableName() string { return "message_report_seg" }
|
||||
|
||||
// MessageReport 状态回执(30天重推窗口)。
|
||||
type MessageReport struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
DetailID int64 `gorm:"uniqueIndex:uk_detail" json:"detail_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
Status string `gorm:"size:16" json:"status"` // DELIVRD/FAILURE/UNKNOWN
|
||||
RawCode string `gorm:"size:32" json:"raw_code"`
|
||||
ErrorCode string `gorm:"size:16" json:"error_code"`
|
||||
ReportAt time.Time `gorm:"index:idx_report_time" json:"report_at"`
|
||||
PushStatus int8 `gorm:"index:idx_push,priority:1;default:0" json:"push_status"`
|
||||
PushedAt *time.Time `json:"pushed_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_push,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (MessageReport) TableName() string { return "message_report" }
|
||||
|
||||
// MessageUplink 上行记录(15天匹配窗口)。
|
||||
type MessageUplink struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
DestCode string `gorm:"size:32;index:idx_dest_mobile,priority:1" json:"dest_code"`
|
||||
SrcMobile string `gorm:"size:20;index:idx_dest_mobile,priority:2" json:"src_mobile"`
|
||||
Content string `gorm:"size:512" json:"content"`
|
||||
AccountID int64 `gorm:"index:idx_account_time,priority:1" json:"account_id"`
|
||||
MatchTaskID int64 `json:"match_task_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
PushStatus int8 `gorm:"default:0" json:"push_status"`
|
||||
RecvAt time.Time `gorm:"index:idx_dest_mobile,priority:3" json:"recv_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_account_time,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (MessageUplink) TableName() string { return "message_uplink" }
|
||||
|
||||
// AuditRecord 审核工作流(48h未审自动失败)。
|
||||
type AuditRecord struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
BizType string `gorm:"size:16;index:idx_biz,priority:1" json:"biz_type"` // sms/template/sign/group
|
||||
BizID int64 `gorm:"index:idx_biz,priority:2" json:"biz_id"`
|
||||
AccountID int64 `gorm:"index:idx_account" json:"account_id"`
|
||||
Content string `gorm:"type:text" json:"content"`
|
||||
Status int8 `gorm:"index:idx_status_time,priority:1;default:0" json:"status"`
|
||||
Auditor string `gorm:"size:64" json:"auditor"`
|
||||
AuditAt *time.Time `json:"audit_at"`
|
||||
Reason string `gorm:"size:255" json:"reason"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_status_time,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (AuditRecord) TableName() string { return "audit_record" }
|
||||
|
||||
// TemplateSMS 短信模板(精确模板仅创建者可用)。
|
||||
type TemplateSMS struct {
|
||||
Model
|
||||
AccountID int64 `gorm:"index:idx_account_status,priority:1" json:"account_id"`
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
Content string `gorm:"size:1024" json:"content"`
|
||||
Variables string `gorm:"size:255" json:"variables"`
|
||||
ExactMatch int8 `gorm:"default:1" json:"exact_match"`
|
||||
Status int8 `gorm:"index:idx_account_status,priority:2;default:0" json:"status"`
|
||||
AuditAt *time.Time `json:"audit_at"`
|
||||
Reason string `gorm:"size:255" json:"reason"` // Day 41:审核结论(拒绝原因)
|
||||
}
|
||||
|
||||
func (TemplateSMS) TableName() string { return "template_sms" }
|
||||
@@ -0,0 +1,65 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// RouteRule 导流/路由规则(账号或全局维度指定通道)。
|
||||
type RouteRule struct {
|
||||
Model
|
||||
AccountID int64 `gorm:"index:idx_account_type,priority:1" json:"account_id"` // 0=全局
|
||||
ChannelID int64 `gorm:"index:idx_rr_channel" json:"channel_id"`
|
||||
RuleType string `gorm:"size:16;index:idx_account_type,priority:2" json:"rule_type"` // keyword/region/account/content
|
||||
RuleConfig string `gorm:"size:512" json:"rule_config"`
|
||||
Priority int `json:"priority"`
|
||||
Status int8 `gorm:"index:idx_account_type,priority:3;default:1" json:"status"`
|
||||
}
|
||||
|
||||
func (RouteRule) TableName() string { return "route_rule" }
|
||||
|
||||
// PolicyBlacklist 本地黑名单库。
|
||||
type PolicyBlacklist struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Mobile string `gorm:"size:20;uniqueIndex:uk_mobile_account,priority:1" json:"mobile"`
|
||||
Source int8 `gorm:"default:1" json:"source"` // 1手工 2上行拒收 3第三方
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_mobile_account,priority:2" json:"account_id"`
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_created" json:"created_at"`
|
||||
}
|
||||
|
||||
func (PolicyBlacklist) TableName() string { return "policy_blacklist" }
|
||||
|
||||
// PolicyWhitelist 白名单(命中忽略频次与黑名单限制)。
|
||||
type PolicyWhitelist struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Mobile string `gorm:"size:20;uniqueIndex:uk_mobile_account,priority:1" json:"mobile"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_mobile_account,priority:2" json:"account_id"`
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (PolicyWhitelist) TableName() string { return "policy_whitelist" }
|
||||
|
||||
// PolicyKeyword 黑白关键字。
|
||||
type PolicyKeyword struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Word string `gorm:"size:128;uniqueIndex:uk_word_level,priority:1" json:"word"`
|
||||
Level int8 `gorm:"uniqueIndex:uk_word_level,priority:2" json:"level"` // 1一级黑 2二级黑 3白
|
||||
Status int8 `gorm:"index:idx_level_status,priority:1;default:1" json:"status"`
|
||||
Operator string `gorm:"size:64" json:"operator"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (PolicyKeyword) TableName() string { return "policy_keyword" }
|
||||
|
||||
// PolicyFrequency 频次策略(同号码/同内容,自然日或滚动24h)。
|
||||
type PolicyFrequency struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"index:idx_account_status,priority:1" json:"account_id"`
|
||||
Dim int8 `gorm:"default:1" json:"dim"` // 1同号码 2同内容
|
||||
WindowUnit int8 `gorm:"default:1" json:"window_unit"` // 1自然日 2滚动24h
|
||||
MaxCount int `json:"max_count"`
|
||||
Status int8 `gorm:"index:idx_account_status,priority:2;default:1" json:"status"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (PolicyFrequency) TableName() string { return "policy_frequency" }
|
||||
@@ -0,0 +1,17 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// StatsDaily 运营总览日汇总(Day68:全平台一行/天,由 gateway 后台 60s ticker
|
||||
// 增量 UPSERT;group/customer 口径走明细直查不落本表——大客户场景再扩账号维度)。
|
||||
// Day 为 "2006-01-02" 字符串主键:MySQL/SQLite 统一按字符串比较排序,规避
|
||||
// 时间类型方言差。
|
||||
type StatsDaily struct {
|
||||
Day string `gorm:"primaryKey;size:10" json:"day"`
|
||||
Submitted int64 `json:"submitted"` // 提交量(全部明细行,含在途/拦截)
|
||||
Success int64 `json:"success"` // 终态成功(status=2)
|
||||
Failed int64 `json:"failed"` // 终态失败(status=3;未知4/拦截5不计成功率分母)
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (StatsDaily) TableName() string { return "stats_daily" }
|
||||
@@ -0,0 +1,106 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// StatsUserDaily 用户日统计(偶数整点刷新)。
|
||||
type StatsUserDaily struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_date,priority:1" json:"account_id"`
|
||||
StatDate time.Time `gorm:"type:date;uniqueIndex:uk_account_date,priority:2;index:idx_date" json:"stat_date"`
|
||||
SubmitCount int `json:"submit_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
UnknownCount int `json:"unknown_count"`
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (StatsUserDaily) TableName() string { return "stats_user_daily" }
|
||||
|
||||
// StatsChannelDaily 通道日统计(含估算余额)。
|
||||
type StatsChannelDaily struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
ChannelID int64 `gorm:"uniqueIndex:uk_channel_date,priority:1" json:"channel_id"`
|
||||
StatDate time.Time `gorm:"type:date;uniqueIndex:uk_channel_date,priority:2;index:idx_date" json:"stat_date"`
|
||||
SubmitCount int `json:"submit_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
UnknownCount int `json:"unknown_count"`
|
||||
CostTotal float64 `gorm:"type:decimal(12,4)" json:"cost_total"`
|
||||
EstBalance int64 `json:"est_balance"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (StatsChannelDaily) TableName() string { return "stats_channel_daily" }
|
||||
|
||||
// SystemConfig 系统参数配置。
|
||||
type SystemConfig struct {
|
||||
Model
|
||||
CfgKey string `gorm:"size:64;uniqueIndex:uk_key" json:"cfg_key"`
|
||||
CfgValue string `gorm:"size:1024" json:"cfg_value"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
UpdatedBy string `gorm:"size:64" json:"updated_by"`
|
||||
}
|
||||
|
||||
func (SystemConfig) TableName() string { return "system_config" }
|
||||
|
||||
// SystemAlertRule 告警规则(余额/通道)。
|
||||
type SystemAlertRule struct {
|
||||
Model
|
||||
TargetType string `gorm:"size:16;index:idx_target,priority:1" json:"target_type"` // account/channel/system
|
||||
TargetID int64 `gorm:"index:idx_target,priority:2" json:"target_id"`
|
||||
Metric string `gorm:"size:32" json:"metric"`
|
||||
Op string `gorm:"size:4;default:lt" json:"op"`
|
||||
Threshold float64 `gorm:"type:decimal(12,4)" json:"threshold"`
|
||||
NotifyType string `gorm:"size:32;default:wechat" json:"notify_type"`
|
||||
NotifyTo string `gorm:"size:128" json:"notify_to"`
|
||||
CooldownMin int `gorm:"default:30" json:"cooldown_min"`
|
||||
Status int8 `gorm:"index:idx_target,priority:3;default:1" json:"status"`
|
||||
LastAlertAt *time.Time `json:"last_alert_at"` // Day 27:上次告警时间(冷却去重锚点)
|
||||
}
|
||||
|
||||
func (SystemAlertRule) TableName() string { return "system_alert_rule" }
|
||||
|
||||
// SystemLoginLog 登录审计。
|
||||
type SystemLoginLog struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Username string `gorm:"size:64;index:idx_username_time,priority:1" json:"username"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
IP string `gorm:"size:45;index:idx_ip" json:"ip"`
|
||||
UserAgent string `gorm:"size:255" json:"user_agent"`
|
||||
Ok int8 `gorm:"default:1" json:"ok"`
|
||||
FailReason string `gorm:"size:64" json:"fail_reason"`
|
||||
LoginAt time.Time `gorm:"index:idx_username_time,priority:2;index:idx_login_at" json:"login_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SystemLoginLog) TableName() string { return "system_login_log" }
|
||||
|
||||
// SystemOperateLog 操作日志。
|
||||
type SystemOperateLog struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Operator string `gorm:"size:64;index:idx_operator_time,priority:1" json:"operator"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
Module string `gorm:"size:32;index:idx_module_time,priority:1" json:"module"`
|
||||
Action string `gorm:"size:32" json:"action"`
|
||||
Target string `gorm:"size:64" json:"target"`
|
||||
Detail string `gorm:"size:1024" json:"detail"`
|
||||
IP string `gorm:"size:45" json:"ip"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_operator_time,priority:2;index:idx_module_time,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SystemOperateLog) TableName() string { return "system_operate_log" }
|
||||
|
||||
// SystemErrorCode 错误代码表(UT:xxxx 体系)。
|
||||
type SystemErrorCode struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
Code string `gorm:"size:16;uniqueIndex:uk_errcode_code" json:"code"`
|
||||
Name string `gorm:"size:64" json:"name"`
|
||||
RawValue string `gorm:"size:64" json:"raw_value"`
|
||||
Action int8 `gorm:"default:0" json:"action"` // 0失败 1重发 2入审
|
||||
Status int8 `gorm:"default:1" json:"status"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (SystemErrorCode) TableName() string { return "system_error_code" }
|
||||
@@ -0,0 +1,16 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// UplinkCode 上行码号分配(Day 49 · A-4.18):码号→账号映射,
|
||||
// match_type 精确/前缀两级匹配(精确优先,前缀取最长),未匹配上行归 admin。
|
||||
type UplinkCode struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"index:idx_uplink_account_time,priority:1;not null" json:"account_id"`
|
||||
Code string `gorm:"size:32;index:idx_code,priority:1;not null" json:"code"` // 码号(精确码全串/前缀码前段)
|
||||
MatchType int8 `gorm:"default:1" json:"match_type"` // 1精确 2前缀
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_uplink_account_time,priority:2" json:"created_at"`
|
||||
}
|
||||
|
||||
func (UplinkCode) TableName() string { return "uplink_code" }
|
||||
@@ -0,0 +1,81 @@
|
||||
# internal/pkg 组件手册(Day 18)
|
||||
|
||||
> 各组件均有单元/集成测试;本文示例为最小用法,完整字段见 `go doc ./internal/pkg/<name>`。
|
||||
|
||||
## cache(Redis 客户端 + 多级缓存)
|
||||
```go
|
||||
// 单机/集群/哨兵统一客户端
|
||||
rc, _ := cache.New(cache.Options{Mode: cache.ModeSingle, Addrs: []string{"127.0.0.1:16379"}})
|
||||
_ = rc.Set(ctx, "k", []byte("v"), time.Minute).Err()
|
||||
|
||||
// 两级缓存:本地 freecache 优先,Miss 查 Redis 并回填;全 Miss 回源
|
||||
ml := cache.NewMultiLevel(rc, cache.MultiLevelOptions{LocalSizeMB: 16, LocalTTL: 30 * time.Second, RemoteTTL: 10 * time.Minute})
|
||||
v, hitRemote, _ := ml.GetOrLoad(ctx, "account:cfg:1001", func(ctx context.Context) ([]byte, error) { return loadFromDB(1001) })
|
||||
```
|
||||
|
||||
## lock(Redis 分布式锁)
|
||||
```go
|
||||
lk := lock.New(rc)
|
||||
l, err := lk.TryLock(ctx, "job:dispatch", 30*time.Second) // SETNX+PX
|
||||
defer func() { _ = l.Unlock(ctx) }() // Lua 原子释放(防误删)
|
||||
// watchdog 每 TTL/3 自动续期;锁丢失即停;Unlock 幂等停止
|
||||
```
|
||||
|
||||
## logger(zap + lumberjack)
|
||||
```go
|
||||
_ = logger.Init(logger.Options{Level: "info", Format: "json", Output: []string{"stdout", "file"},
|
||||
File: logger.FileOptions{Path: "/var/log/gosms/app.log", MaxSizeMB: 100, Compress: true}})
|
||||
logger.L().Info("order_created", zap.Int64("order_id", 42))
|
||||
logger.Level().SetLevel(zap.DebugLevel) // 动态级别(ETCD 热更新联动)
|
||||
```
|
||||
|
||||
## metrics(Prometheus)
|
||||
```go
|
||||
// app 已装配 GET /metrics;业务埋点示例:
|
||||
metrics.SMSSubmitTotal.WithLabelValues("ok").Inc()
|
||||
metrics.HTTPRequestDuration.WithLabelValues("GET", "/health").Observe(0.012)
|
||||
```
|
||||
|
||||
## queue(Kafka 生产/消费/死信)
|
||||
```go
|
||||
p, _ := queue.NewProducer(queue.ProducerConfig{Brokers: brokers, Logger: lg})
|
||||
_ = p.Send(queue.TopicSubmit, []byte(mobile), payload) // 失败自动重试→<topic>.dlq
|
||||
|
||||
cg, _ := queue.NewConsumerGroup(queue.ConsumerGroupConfig{
|
||||
Consumer: queue.ConsumerConfig{Brokers: brokers, GroupID: "sender", Topics: []string{queue.TopicSubmit}, Handler: handle},
|
||||
Producer: queue.ProducerConfig{Brokers: brokers, Logger: lg}, // 死信转投
|
||||
MaxRetry: 3, // 单消息重试 3 次(指数退避)
|
||||
})
|
||||
go func() { _ = cg.Run(ctx) }()
|
||||
cg.Pause(); cg.Resume() // 消费暂停/恢复(运维窗口)
|
||||
```
|
||||
|
||||
## errors / response(ISC:xxx 错误码与统一响应)
|
||||
```go
|
||||
response.Success(c, data)
|
||||
response.Error(c, errors.Blacklist) // 403 {"code":"ISC:006","msg":"黑名单"}
|
||||
response.Page(c, list, total, page, 20)
|
||||
```
|
||||
|
||||
## jwt(签发/解析)
|
||||
```go
|
||||
m := jwt.NewManager(secret, 2*time.Hour)
|
||||
tok, _ := m.Sign(1001, "alice", []string{"ops"}, []string{"account:read"})
|
||||
claims, err := m.Parse(tok) // 强制 HMAC;过期/篡改返回错误
|
||||
```
|
||||
|
||||
## casbin(RBAC,策略存 MySQL)
|
||||
```go
|
||||
e, _ := casbin.New(gormDB) // 自动建 casbin_rule 并加载
|
||||
ok, _ := e.Check(1001, "channel", "write") // account_id → resource:action
|
||||
_, _ = e.BindRole(1001, "ops") // 开户时绑定角色
|
||||
```
|
||||
|
||||
## util(工具库)
|
||||
```go
|
||||
util.IsValidMainlandMobile("+86 138-0013-8000") // true
|
||||
c, n := util.SmsLength("你好世界") // (4, 1) UCS2 计费
|
||||
util.ExtractVariables("尊敬的{name}") // [name]
|
||||
sf, _ := util.NewSnowflake(1); id := sf.NextID() // 雪花 ID(多实例用 WorkerIDFromETCD)
|
||||
enc, _ := util.EncryptSecret(passphrase, apiKey) // AES-GCM 落库加密
|
||||
```
|
||||
Vendored
+104
@@ -0,0 +1,104 @@
|
||||
// Package cache Redis 缓存客户端封装(go-redis/v9,Day 6)。
|
||||
// 统一 single / cluster / sentinel 三种模式的客户端构造与生命周期,
|
||||
// 业务侧通过嵌入的 redis.UniversalClient 使用全部 Redis 命令。
|
||||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Mode 常量:Redis 部署模式。
|
||||
const (
|
||||
ModeSingle = "single"
|
||||
ModeCluster = "cluster"
|
||||
ModeSentinel = "sentinel"
|
||||
)
|
||||
|
||||
// Options Redis 客户端配置(对应 config.Redis)。
|
||||
type Options struct {
|
||||
Mode string // single | cluster | sentinel
|
||||
Addrs []string
|
||||
Password string
|
||||
DB int // 仅 single/sentinel 有效
|
||||
PoolSize int
|
||||
MasterName string // 仅 sentinel 有效
|
||||
}
|
||||
|
||||
// Client 统一 Redis 客户端;嵌入 UniversalClient,
|
||||
// 单机与集群命令签名一致(Set/Get/Del/Eval/Pipeline 等)。
|
||||
type Client struct {
|
||||
redis.UniversalClient
|
||||
mode string
|
||||
}
|
||||
|
||||
// Mode 返回部署模式。
|
||||
func (c *Client) Mode() string { return c.mode }
|
||||
|
||||
// New 按模式构造 Redis 客户端(lazy 连接,首次命令时建立连接)。
|
||||
func New(opt Options) (*Client, error) {
|
||||
mode := strings.ToLower(strings.TrimSpace(opt.Mode))
|
||||
if mode == "" {
|
||||
mode = ModeSingle
|
||||
}
|
||||
if len(opt.Addrs) == 0 {
|
||||
return nil, errors.New("redis.addrs 不能为空")
|
||||
}
|
||||
if opt.PoolSize <= 0 {
|
||||
opt.PoolSize = 100
|
||||
}
|
||||
|
||||
var uc redis.UniversalClient
|
||||
switch mode {
|
||||
case ModeSingle:
|
||||
uc = redis.NewClient(&redis.Options{
|
||||
Addr: opt.Addrs[0],
|
||||
Password: opt.Password,
|
||||
DB: opt.DB,
|
||||
PoolSize: opt.PoolSize,
|
||||
DialTimeout: dialTimeout,
|
||||
ReadTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
})
|
||||
case ModeCluster:
|
||||
uc = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: opt.Addrs,
|
||||
Password: opt.Password,
|
||||
PoolSize: opt.PoolSize,
|
||||
DialTimeout: dialTimeout,
|
||||
ReadTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
})
|
||||
case ModeSentinel:
|
||||
if opt.MasterName == "" {
|
||||
return nil, errors.New("sentinel 模式必须配置 master_name")
|
||||
}
|
||||
uc = redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: opt.MasterName,
|
||||
SentinelAddrs: opt.Addrs,
|
||||
Password: opt.Password,
|
||||
DB: opt.DB,
|
||||
PoolSize: opt.PoolSize,
|
||||
DialTimeout: dialTimeout,
|
||||
ReadTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
})
|
||||
default:
|
||||
return nil, fmt.Errorf("未知 redis 模式: %q(可选 single/cluster/sentinel)", opt.Mode)
|
||||
}
|
||||
return &Client{UniversalClient: uc, mode: mode}, nil
|
||||
}
|
||||
|
||||
// Ping 连通性检查直接使用嵌入的 UniversalClient.Ping(返回 *redis.StatusCmd),
|
||||
// 例如:if err := c.Ping(ctx).Err(); err != nil { ... }。
|
||||
|
||||
// 超时常量(连接建立与命令 IO)。
|
||||
const (
|
||||
dialTimeout = 3 * time.Second
|
||||
readTimeout = 1 * time.Second
|
||||
writeTimeout = 1 * time.Second
|
||||
)
|
||||
Vendored
+101
@@ -0,0 +1,101 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// newMini 启动内存 Redis 并返回客户端构造参数。
|
||||
func newMini(t *testing.T) *Options {
|
||||
t.Helper()
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("启动 miniredis 失败: %v", err)
|
||||
}
|
||||
t.Cleanup(mr.Close)
|
||||
return &Options{Mode: ModeSingle, Addrs: []string{mr.Addr()}}
|
||||
}
|
||||
|
||||
// TestClient_SetGetDel 验证单机模式基础命令(开发环境验收同语义)。
|
||||
func TestClient_SetGetDel(t *testing.T) {
|
||||
opt := newMini(t)
|
||||
c, err := New(*opt)
|
||||
if err != nil {
|
||||
t.Fatalf("New 失败: %v", err)
|
||||
}
|
||||
defer func() { _ = c.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
if err := c.Ping(ctx).Err(); err != nil {
|
||||
t.Fatalf("Ping 失败: %v", err)
|
||||
}
|
||||
if err := c.Set(ctx, "go-sms:key", "value1", time.Minute).Err(); err != nil {
|
||||
t.Fatalf("SET 失败: %v", err)
|
||||
}
|
||||
got, err := c.Get(ctx, "go-sms:key").Result()
|
||||
if err != nil || got != "value1" {
|
||||
t.Fatalf("GET = (%q, %v), 期望 (value1, nil)", got, err)
|
||||
}
|
||||
if n, err := c.Del(ctx, "go-sms:key").Result(); err != nil || n != 1 {
|
||||
t.Fatalf("DEL = (%d, %v), 期望 (1, nil)", n, err)
|
||||
}
|
||||
if err := c.Get(ctx, "go-sms:key").Err(); !errors.Is(err, redis.Nil) {
|
||||
t.Fatalf("删除后 GET 应返回 redis.Nil, 实际: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestClient_Modes 验证三种模式构造与模式标识(cluster/sentinel 为 lazy 连接)。
|
||||
func TestClient_Modes(t *testing.T) {
|
||||
c, err := New(Options{Mode: ModeCluster, Addrs: []string{"127.0.0.1:7001", "127.0.0.1:7002"}})
|
||||
if err != nil || c.Mode() != ModeCluster {
|
||||
t.Errorf("cluster 构造失败: (%v, mode=%s)", err, c.Mode())
|
||||
}
|
||||
|
||||
// 空模式默认 single
|
||||
opt := newMini(t)
|
||||
opt.Mode = ""
|
||||
c2, err := New(*opt)
|
||||
if err != nil || c2.Mode() != ModeSingle {
|
||||
t.Errorf("默认模式应为 single: (%v, %s)", err, c2.Mode())
|
||||
}
|
||||
}
|
||||
|
||||
// TestNew_InvalidOptions 验证非法配置拦截。
|
||||
func TestNew_InvalidOptions(t *testing.T) {
|
||||
if _, err := New(Options{Mode: "proxy"}); err == nil {
|
||||
t.Error("未知模式应报错")
|
||||
}
|
||||
if _, err := New(Options{Mode: ModeSingle}); err == nil {
|
||||
t.Error("空地址应报错")
|
||||
}
|
||||
if _, err := New(Options{Mode: ModeSentinel, Addrs: []string{"127.0.0.1:26379"}}); err == nil {
|
||||
t.Error("sentinel 缺少 master_name 应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// TestClient_Expire 验证 TTL 生效(miniredis FastForward 加速)。
|
||||
func TestClient_Expire(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
c, err := New(Options{Mode: ModeSingle, Addrs: []string{mr.Addr()}})
|
||||
if err != nil {
|
||||
t.Fatalf("New 失败: %v", err)
|
||||
}
|
||||
defer func() { _ = c.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
if err := c.Set(ctx, "k", "v", 2*time.Second).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !mr.Exists("k") {
|
||||
t.Fatal("SET 后 key 应存在")
|
||||
}
|
||||
mr.FastForward(3 * time.Second)
|
||||
if mr.Exists("k") {
|
||||
t.Fatal("TTL 过期后 key 应消失")
|
||||
}
|
||||
}
|
||||
Vendored
+125
@@ -0,0 +1,125 @@
|
||||
// Package cache Redis 缓存与多级缓存封装。
|
||||
// MultiLevel:本地 freecache → Redis 两级读取,写/删双层同步(Day 16)。
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/coocood/freecache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// MultiLevel 两级缓存:本地 freecache(进程内,零网络)优先,Miss 查 Redis。
|
||||
// 适用:账号配置/路由规则等读多写少的强读数据;本地 TTL 应短于远端以控制一致性窗口。
|
||||
type MultiLevel struct {
|
||||
local *freecache.Cache
|
||||
remote redis.UniversalClient
|
||||
log *zap.Logger
|
||||
localTTL time.Duration // 默认本地 TTL
|
||||
remoteTTL time.Duration // 默认远端 TTL
|
||||
stats MultiLevelStats
|
||||
}
|
||||
|
||||
// MultiLevelStats 命中统计(验收与自观测)。
|
||||
type MultiLevelStats struct {
|
||||
HitLocal uint64 `json:"hit_local"`
|
||||
HitRemote uint64 `json:"hit_remote"`
|
||||
Miss uint64 `json:"miss"`
|
||||
}
|
||||
|
||||
// MultiLevelOptions 可选项。
|
||||
type MultiLevelOptions struct {
|
||||
LocalSizeMB int // 本地缓存容量(默认 16MB)
|
||||
LocalTTL time.Duration // 默认本地 TTL(默认 30s)
|
||||
RemoteTTL time.Duration // 默认远端 TTL(默认 10min)
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewMultiLevel 构造(rdb 为 nil 时退化为纯本地)。
|
||||
func NewMultiLevel(rdb redis.UniversalClient, opt MultiLevelOptions) *MultiLevel {
|
||||
if opt.LocalSizeMB <= 0 {
|
||||
opt.LocalSizeMB = 16
|
||||
}
|
||||
if opt.LocalTTL <= 0 {
|
||||
opt.LocalTTL = 30 * time.Second
|
||||
}
|
||||
if opt.RemoteTTL <= 0 {
|
||||
opt.RemoteTTL = 10 * time.Minute
|
||||
}
|
||||
if opt.Logger == nil {
|
||||
opt.Logger = zap.NewNop()
|
||||
}
|
||||
return &MultiLevel{
|
||||
local: freecache.NewCache(opt.LocalSizeMB * 1024 * 1024),
|
||||
remote: rdb, log: opt.Logger,
|
||||
localTTL: opt.LocalTTL, remoteTTL: opt.RemoteTTL,
|
||||
}
|
||||
}
|
||||
|
||||
// Get 两级读取:优先本地,Miss 查 Redis;命中远端时回填本地。
|
||||
// 返回 (值, 是否命中)。
|
||||
func (m *MultiLevel) Get(ctx context.Context, key string) ([]byte, bool) {
|
||||
if v, err := m.local.Get([]byte(key)); err == nil {
|
||||
m.stats.HitLocal++
|
||||
return v, true
|
||||
}
|
||||
if m.remote == nil {
|
||||
m.stats.Miss++
|
||||
return nil, false
|
||||
}
|
||||
v, err := m.remote.Get(ctx, key).Bytes()
|
||||
if err == nil {
|
||||
m.stats.HitRemote++
|
||||
_ = m.local.Set([]byte(key), v, int(m.localTTL.Seconds()))
|
||||
return v, true
|
||||
}
|
||||
m.stats.Miss++
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// GetOrLoad 两级读取,全 Miss 时回源 src 并双层回填(负缓存可由调用方空值实现)。
|
||||
func (m *MultiLevel) GetOrLoad(ctx context.Context, key string, src func(ctx context.Context) ([]byte, error)) ([]byte, bool, error) {
|
||||
if v, ok := m.Get(ctx, key); ok {
|
||||
return v, true, nil
|
||||
}
|
||||
if src == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
v, err := src(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
m.Set(ctx, key, v, 0, 0)
|
||||
return v, false, nil
|
||||
}
|
||||
|
||||
// Set 双层写入(localTTL/remoteTTL 为 0 时用默认值)。
|
||||
func (m *MultiLevel) Set(ctx context.Context, key string, val []byte, localTTL, remoteTTL time.Duration) {
|
||||
if localTTL <= 0 {
|
||||
localTTL = m.localTTL
|
||||
}
|
||||
if remoteTTL <= 0 {
|
||||
remoteTTL = m.remoteTTL
|
||||
}
|
||||
_ = m.local.Set([]byte(key), val, int(localTTL.Seconds()))
|
||||
if m.remote != nil {
|
||||
if err := m.remote.Set(ctx, key, val, remoteTTL).Err(); err != nil {
|
||||
m.log.Warn("multilevel_remote_set_failed", zap.String("key", key), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 双层删除(远端失败仅告警,本地必删以保证本进程立即失效)。
|
||||
func (m *MultiLevel) Delete(ctx context.Context, key string) {
|
||||
_ = m.local.Del([]byte(key))
|
||||
if m.remote != nil {
|
||||
if err := m.remote.Del(ctx, key).Err(); err != nil {
|
||||
m.log.Warn("multilevel_remote_del_failed", zap.String("key", key), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stats 返回命中统计快照。
|
||||
func (m *MultiLevel) Stats() MultiLevelStats { return m.stats }
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// newML 构造被测对象(本地 1MB + miniredis)。
|
||||
func newML(t *testing.T) (*MultiLevel, *miniredis.Miniredis) {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
return NewMultiLevel(rdb, MultiLevelOptions{LocalSizeMB: 1, LocalTTL: 30 * time.Second, RemoteTTL: time.Minute}), mr
|
||||
}
|
||||
|
||||
// TestMultiLevel_LocalFirst 验收:Get 优先命中本地。
|
||||
func TestMultiLevel_LocalFirst(t *testing.T) {
|
||||
m, mr := newML(t)
|
||||
ctx := context.Background()
|
||||
m.Set(ctx, "k1", []byte("v1"), 0, 0)
|
||||
|
||||
// 断开 Redis 后仍可命中 → 证明走本地
|
||||
mr.Close()
|
||||
|
||||
if v, ok := m.Get(ctx, "k1"); !ok || string(v) != "v1" {
|
||||
t.Fatalf("本地命中失败: (%q,%v)", v, ok)
|
||||
}
|
||||
if s := m.Stats(); s.HitLocal != 1 || s.HitRemote != 0 {
|
||||
t.Errorf("统计 = %+v, 期望 HitLocal=1", s)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMultiLevel_MissHitsRedis 验收:本地 Miss 时查 Redis 并回填本地。
|
||||
func TestMultiLevel_MissHitsRedis(t *testing.T) {
|
||||
m, mr := newML(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 仅写远端(绕过 Set 的双层写)
|
||||
if err := m.remote.Set(ctx, "k2", []byte("v2"), time.Minute).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 第一次:本地 Miss → Redis 命中 → 回填本地
|
||||
if v, ok := m.Get(ctx, "k2"); !ok || string(v) != "v2" {
|
||||
t.Fatalf("远端命中失败: (%q,%v)", v, ok)
|
||||
}
|
||||
if s := m.Stats(); s.HitRemote != 1 || s.HitLocal != 0 {
|
||||
t.Errorf("统计 = %+v, 期望 HitRemote=1", s)
|
||||
}
|
||||
// 第二次:已被回填 → 本地命中(零网络);远端键仍在
|
||||
if v, ok := m.Get(ctx, "k2"); !ok || string(v) != "v2" {
|
||||
t.Fatalf("二次读取失败: (%q,%v)", v, ok)
|
||||
}
|
||||
if s := m.Stats(); s.HitLocal != 1 || s.HitRemote != 1 {
|
||||
t.Errorf("回填后应本地命中: %+v", s)
|
||||
}
|
||||
if !mr.Exists("k2") {
|
||||
t.Fatal("远端键应存在")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMultiLevel_GetOrLoad 全 Miss 回源并双层回填。
|
||||
func TestMultiLevel_GetOrLoad(t *testing.T) {
|
||||
m, _ := newML(t)
|
||||
ctx := context.Background()
|
||||
calls := 0
|
||||
src := func(context.Context) ([]byte, error) { calls++; return []byte("from-src"), nil }
|
||||
|
||||
v, fromCache, err := m.GetOrLoad(ctx, "k3", src)
|
||||
if err != nil || fromCache || string(v) != "from-src" || calls != 1 {
|
||||
t.Fatalf("回源异常: v=%q fromCache=%v err=%v calls=%d", v, fromCache, err, calls)
|
||||
}
|
||||
// 二次:本地命中,不再回源
|
||||
v, fromCache, err = m.GetOrLoad(ctx, "k3", src)
|
||||
if err != nil || !fromCache || calls != 1 {
|
||||
t.Fatalf("二次应命中本地: fromCache=%v calls=%d err=%v", fromCache, calls, err)
|
||||
}
|
||||
_ = v
|
||||
}
|
||||
|
||||
// TestMultiLevel_Delete 双层删除。
|
||||
func TestMultiLevel_Delete(t *testing.T) {
|
||||
m, _ := newML(t)
|
||||
ctx := context.Background()
|
||||
m.Set(ctx, "k4", []byte("v4"), 0, 0)
|
||||
m.Delete(ctx, "k4")
|
||||
if _, ok := m.Get(ctx, "k4"); ok {
|
||||
t.Fatal("删除后不应命中")
|
||||
}
|
||||
remoteExists := m.remote.Exists(ctx, "k4").Val() // IntCmd:0/1
|
||||
if remoteExists > 0 {
|
||||
t.Fatal("远端未删除")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMultiLevel_LocalOnly 无 Redis 退化为纯本地。
|
||||
func TestMultiLevel_LocalOnly(t *testing.T) {
|
||||
m := NewMultiLevel(nil, MultiLevelOptions{LocalSizeMB: 1})
|
||||
ctx := context.Background()
|
||||
m.Set(ctx, "k5", []byte("v5"), 0, 0)
|
||||
if v, ok := m.Get(ctx, "k5"); !ok || string(v) != "v5" {
|
||||
t.Fatal("纯本地模式失败")
|
||||
}
|
||||
if _, ok := m.Get(ctx, "absent"); ok {
|
||||
t.Fatal("不存在键不应命中")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// Package casbin RBAC 引擎(Day 17):内嵌 RBAC 模型,策略经 gorm-adapter
|
||||
// 持久化 casbin_rule 表;Check(accountID, resource, action) 为鉴权入口。
|
||||
package casbin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/casbin/casbin/v2"
|
||||
"github.com/casbin/casbin/v2/model"
|
||||
gormadapter "github.com/casbin/gorm-adapter/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// rbacModel RBAC 模型(内嵌,免外部文件):
|
||||
// sub=account_id(字符串);obj=resource;act=action;g 为账户-角色绑定。
|
||||
// obj 支持 * 通配(keyMatch);act 为 * 时放行任意动作。
|
||||
const rbacModel = `
|
||||
[request_definition]
|
||||
r = sub, obj, act
|
||||
|
||||
[policy_definition]
|
||||
p = sub, obj, act
|
||||
|
||||
[role_definition]
|
||||
g = _, _
|
||||
|
||||
[policy_effect]
|
||||
e = some(where (p.eft == allow))
|
||||
|
||||
[matchers]
|
||||
m = (g(r.sub, p.sub) || r.sub == p.sub) && keyMatch(r.obj, p.obj) && (p.act == "*" || regexMatch(r.act, p.act))
|
||||
`
|
||||
|
||||
// Enforcer RBAC 引擎(SyncedEnforcer 支持并发;策略持久化 casbin_rule 表)。
|
||||
type Enforcer struct {
|
||||
*casbin.SyncedEnforcer
|
||||
}
|
||||
|
||||
// New 创建 enforcer:db 上自动建 casbin_rule 表并加载既有策略。
|
||||
func New(db *gorm.DB) (*Enforcer, error) {
|
||||
m, err := model.NewModelFromString(rbacModel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("casbin: 模型解析失败: %w", err)
|
||||
}
|
||||
adapter, err := gormadapter.NewAdapterByDBUseTableName(db, "", "casbin_rule")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("casbin: 适配器创建失败: %w", err)
|
||||
}
|
||||
e, err := casbin.NewSyncedEnforcer(m, adapter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("casbin: enforcer 创建失败: %w", err)
|
||||
}
|
||||
if err := e.LoadPolicy(); err != nil {
|
||||
return nil, fmt.Errorf("casbin: 策略加载失败: %w", err)
|
||||
}
|
||||
return &Enforcer{SyncedEnforcer: e}, nil
|
||||
}
|
||||
|
||||
// Check 校验账户是否拥有 resource:action 权限(验收入口)。
|
||||
func (e *Enforcer) Check(accountID int64, resource, action string) (bool, error) {
|
||||
return e.Enforce(strconv.FormatInt(accountID, 10), resource, action)
|
||||
}
|
||||
|
||||
// BindRole 为账户绑定角色(开户/改角色时调用,持久化 casbin_rule)。
|
||||
func (e *Enforcer) BindRole(accountID int64, role string) (bool, error) {
|
||||
return e.AddRoleForUser(strconv.FormatInt(accountID, 10), role)
|
||||
}
|
||||
|
||||
// UnbindAllRoles 解绑账户全部角色。
|
||||
func (e *Enforcer) UnbindAllRoles(accountID int64) (bool, error) {
|
||||
return e.DeleteRolesForUser(strconv.FormatInt(accountID, 10))
|
||||
}
|
||||
|
||||
// AddPolicy 添加角色策略(role, resource, action)。
|
||||
func (e *Enforcer) AddPolicy(role, resource, action string) (bool, error) {
|
||||
return e.SyncedEnforcer.AddPolicy(role, resource, action)
|
||||
}
|
||||
|
||||
// GrantPermission 为账户直接授权(二级权限:sub=accountID 的 p 规则)。
|
||||
// 充值 recharge / 代发 proxy_send / 导出 export 等按账户粒度授予,区别于角色策略。
|
||||
func (e *Enforcer) GrantPermission(accountID int64, resource, action string) (bool, error) {
|
||||
return e.SyncedEnforcer.AddPolicy(strconv.FormatInt(accountID, 10), resource, action)
|
||||
}
|
||||
|
||||
// RevokePermission 撤销账户直接授权。
|
||||
func (e *Enforcer) RevokePermission(accountID int64, resource, action string) (bool, error) {
|
||||
return e.RemovePolicy(strconv.FormatInt(accountID, 10), resource, action)
|
||||
}
|
||||
|
||||
// PermissionsFor 列出账户全部直接授权(resource:action 列表,不含角色继承)。
|
||||
func (e *Enforcer) PermissionsFor(accountID int64) []string {
|
||||
polices, err := e.GetFilteredPolicy(0, strconv.FormatInt(accountID, 10))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(polices))
|
||||
for _, p := range polices {
|
||||
if len(p) >= 3 {
|
||||
out = append(out, p[1]+":"+p[2])
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package casbin
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// newTestEnforcer 内存 SQLite + 基础策略。
|
||||
func newTestEnforcer(t *testing.T) *Enforcer {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e, err := New(db)
|
||||
if err != nil {
|
||||
t.Fatalf("创建 enforcer 失败: %v", err)
|
||||
}
|
||||
// 角色策略(与 v5 种子同构)
|
||||
for _, p := range [][]string{
|
||||
{"admin", "*", "*"},
|
||||
{"ops", "*", "read"},
|
||||
{"ops", "account", "write"},
|
||||
{"ops", "channel", "write"},
|
||||
{"service", "account", "read"},
|
||||
{"service", "message", "read"},
|
||||
{"customer", "message", "write"},
|
||||
} {
|
||||
if _, err := e.AddPolicy(p[0], p[1], p[2]); err != nil {
|
||||
t.Fatalf("AddPolicy(%v): %v", p, err)
|
||||
}
|
||||
}
|
||||
// 账户绑定:1=admin,2=ops,3=service,9 无角色
|
||||
for uid, role := range map[int64]string{1: "admin", 2: "ops", 3: "service"} {
|
||||
if _, err := e.BindRole(uid, role); err != nil {
|
||||
t.Fatalf("BindRole(%d,%s): %v", uid, role, err)
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// TestCheck_AdminWildcard 验收:Casbin 校验 account_id 的 resource:action。
|
||||
func TestCheck_AdminWildcard(t *testing.T) {
|
||||
e := newTestEnforcer(t)
|
||||
cases := []struct {
|
||||
uid int64
|
||||
res string
|
||||
act string
|
||||
want bool
|
||||
desc string
|
||||
}{
|
||||
{1, "system", "config", true, "admin 通配任意资源动作"},
|
||||
{1, "channel", "delete", true, "admin */*"},
|
||||
{2, "account", "read", true, "ops */read"},
|
||||
{2, "message", "read", true, "ops */read"},
|
||||
{2, "account", "write", true, "ops account/write"},
|
||||
{2, "account", "delete", false, "ops 无 account/delete"},
|
||||
{3, "account", "write", false, "service 只读"},
|
||||
{3, "message", "read", true, "service message/read"},
|
||||
{9, "account", "read", false, "未绑定角色拒绝"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := e.Check(c.uid, c.res, c.act)
|
||||
if err != nil {
|
||||
t.Fatalf("[%s] Check 出错: %v", c.desc, err)
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("[%s] Check(%d,%s,%s) = %v, 期望 %v", c.desc, c.uid, c.res, c.act, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBindUnbind 动态绑定/解绑角色即时生效。
|
||||
func TestBindUnbind(t *testing.T) {
|
||||
e := newTestEnforcer(t)
|
||||
if ok, _ := e.Check(9, "message", "write"); ok {
|
||||
t.Fatal("未绑定前应拒绝")
|
||||
}
|
||||
if _, err := e.BindRole(9, "customer"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := e.Check(9, "message", "write"); !ok {
|
||||
t.Fatal("绑定 customer 后应放行 message/write")
|
||||
}
|
||||
if _, err := e.UnbindAllRoles(9); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := e.Check(9, "message", "write"); ok {
|
||||
t.Fatal("解绑后应拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPersistence 策略经适配器持久化(重建 enforcer 后仍在)。
|
||||
func TestPersistence(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e1, err := New(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e1.AddPolicy("qa", "message", "read"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e1.BindRole(77, "qa"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 重新加载(模拟新进程)
|
||||
e2, err := New(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok, err := e2.Check(77, "message", "read")
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("重建后策略丢失: ok=%v err=%v", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSecondaryGrantRevoke Day 25:账户直接授权(二级权限)授予/撤销/列表。
|
||||
func TestSecondaryGrantRevoke(t *testing.T) {
|
||||
e := newTestEnforcer(t)
|
||||
|
||||
if ok, _ := e.Check(9, "account", "export"); ok {
|
||||
t.Fatal("未授权前应拒绝 export")
|
||||
}
|
||||
if _, err := e.GrantPermission(9, "account", "export"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := e.Check(9, "account", "export"); !ok {
|
||||
t.Fatal("授予后应放行 export")
|
||||
}
|
||||
perms := e.PermissionsFor(9)
|
||||
if len(perms) != 1 || perms[0] != "account:export" {
|
||||
t.Fatalf("直接授权列表 = %v, 期望 [account:export]", perms)
|
||||
}
|
||||
if _, err := e.RevokePermission(9, "account", "export"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := e.Check(9, "account", "export"); ok {
|
||||
t.Fatal("撤销后应拒绝 export")
|
||||
}
|
||||
if len(e.PermissionsFor(9)) != 0 {
|
||||
t.Fatal("撤销后列表应为空")
|
||||
}
|
||||
// 幂等:重复撤销不报错
|
||||
if _, err := e.RevokePermission(9, "account", "export"); err != nil {
|
||||
t.Fatalf("重复撤销应幂等: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// Package cronx 轻量周期任务调度器(Day 27):每 Job 一协程 ticker,
|
||||
// 支持 Start/Stop 与停止后重启(验收:定时器可停启);不引第三方依赖。
|
||||
package cronx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// errPanic recover 值转错误。
|
||||
func errPanic(r any) error {
|
||||
if e, ok := r.(error); ok {
|
||||
return fmt.Errorf("cronx: job panic: %w", e)
|
||||
}
|
||||
return fmt.Errorf("cronx: job panic: %v", r)
|
||||
}
|
||||
|
||||
// Job 周期任务;Interval <=0 拒绝;Run 返回错误经 OnError 上报(nil 则忽略)。
|
||||
type Job struct {
|
||||
Name string
|
||||
Interval time.Duration
|
||||
Immediate bool // Start 后立即执行一次(不等首个 tick)
|
||||
Run func(ctx context.Context) error
|
||||
OnError func(name string, err error)
|
||||
}
|
||||
|
||||
// Scheduler 任务集合;Start 幂等,Stop 幂等且等待全部退出。
|
||||
type Scheduler struct {
|
||||
mu sync.Mutex
|
||||
jobs []Job
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
started bool
|
||||
}
|
||||
|
||||
// Add 注册任务(仅未启动时允许;启动后再 Add 返回 false)。
|
||||
func (s *Scheduler) Add(j Job) bool {
|
||||
if j.Interval <= 0 || j.Run == nil {
|
||||
return false
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.started {
|
||||
return false
|
||||
}
|
||||
s.jobs = append(s.jobs, j)
|
||||
return true
|
||||
}
|
||||
|
||||
// Start 启动全部任务(每任务一协程);重复调用无效果。
|
||||
func (s *Scheduler) Start() {
|
||||
s.mu.Lock()
|
||||
if s.started {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
s.started = true
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
s.cancel = cancel
|
||||
jobs := append([]Job(nil), s.jobs...)
|
||||
s.wg.Add(len(jobs))
|
||||
s.mu.Unlock()
|
||||
|
||||
for _, j := range jobs {
|
||||
go func(j Job) {
|
||||
defer s.wg.Done()
|
||||
if j.Immediate {
|
||||
s.runOnce(ctx, j)
|
||||
}
|
||||
t := time.NewTicker(j.Interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
s.runOnce(ctx, j)
|
||||
}
|
||||
}
|
||||
}(j)
|
||||
}
|
||||
}
|
||||
|
||||
// runOnce 执行一次并兜底 panic,错误交 OnError。
|
||||
func (s *Scheduler) runOnce(ctx context.Context, j Job) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if j.OnError != nil {
|
||||
j.OnError(j.Name, errPanic(r))
|
||||
}
|
||||
}
|
||||
}()
|
||||
if err := j.Run(ctx); err != nil && j.OnError != nil {
|
||||
j.OnError(j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop 停止并等待全部任务退出;可再次 Start(重启)。
|
||||
func (s *Scheduler) Stop() {
|
||||
s.mu.Lock()
|
||||
if !s.started {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
cancel := s.cancel
|
||||
s.started = false
|
||||
s.cancel = nil
|
||||
s.mu.Unlock()
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
s.wg.Wait()
|
||||
}
|
||||
|
||||
// Started 是否运行中(测试用)。
|
||||
func (s *Scheduler) Started() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.started
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// cronx_test.go Day 27:调度器启停/重启/立即执行/panic 兜底。
|
||||
package cronx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestScheduler_StartStopRestart 验收:定时器可停启;停止后计数不再增长,重启恢复。
|
||||
func TestScheduler_StartStopRestart(t *testing.T) {
|
||||
var n atomic.Int64
|
||||
s := &Scheduler{}
|
||||
if !s.Add(Job{
|
||||
Name: "tick", Interval: 30 * time.Millisecond,
|
||||
Run: func(ctx context.Context) error { n.Add(1); return nil },
|
||||
}) {
|
||||
t.Fatal("Add 应成功")
|
||||
}
|
||||
s.Start()
|
||||
s.Start() // 幂等:重复 Start 不重复起协程
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
s.Stop()
|
||||
after := n.Load()
|
||||
if after < 2 {
|
||||
t.Fatalf("运行期计数 = %d, 期望 >=2", after)
|
||||
}
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
if n.Load() != after {
|
||||
t.Fatalf("停止后仍在执行(%d -> %d)", after, n.Load())
|
||||
}
|
||||
// 重启
|
||||
s.Start()
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
if n.Load() <= after {
|
||||
t.Fatalf("重启后未恢复执行(%d <= %d)", n.Load(), after)
|
||||
}
|
||||
s.Stop()
|
||||
if s.Started() {
|
||||
t.Fatal("Stop 后 Started 应为 false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_ImmediateAndPanic Immediate 立即执行;panic 不拖垮循环且经 OnError 上报。
|
||||
func TestScheduler_ImmediateAndPanic(t *testing.T) {
|
||||
var n, panics atomic.Int64
|
||||
s := &Scheduler{}
|
||||
s.Add(Job{Name: "boom", Interval: 20 * time.Millisecond, Immediate: true,
|
||||
Run: func(ctx context.Context) error {
|
||||
if n.Add(1) == 1 {
|
||||
panic("first run boom")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
OnError: func(name string, err error) { panics.Add(1) },
|
||||
})
|
||||
s.Start()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
s.Stop()
|
||||
if n.Load() < 3 {
|
||||
t.Fatalf("panic 后循环应继续, 计数 = %d", n.Load())
|
||||
}
|
||||
if panics.Load() == 0 {
|
||||
t.Fatal("panic 应经 OnError 上报")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_AddGuard 非法任务拒绝;启动后不可追加。
|
||||
func TestScheduler_AddGuard(t *testing.T) {
|
||||
s := &Scheduler{}
|
||||
if s.Add(Job{Name: "bad", Interval: 0, Run: func(context.Context) error { return nil }}) {
|
||||
t.Fatal("Interval<=0 应拒绝")
|
||||
}
|
||||
if s.Add(Job{Name: "nilrun", Interval: time.Second}) {
|
||||
t.Fatal("Run=nil 应拒绝")
|
||||
}
|
||||
s.Start()
|
||||
defer s.Stop()
|
||||
if s.Add(Job{Name: "late", Interval: time.Second, Run: func(context.Context) error { return nil }}) {
|
||||
t.Fatal("启动后 Add 应返回 false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_ErrorPropagation Run 错误经 OnError 透出。
|
||||
func TestScheduler_ErrorPropagation(t *testing.T) {
|
||||
want := errors.New("boom")
|
||||
got := make(chan error, 4)
|
||||
s := &Scheduler{}
|
||||
s.Add(Job{Name: "err", Interval: 15 * time.Millisecond, Immediate: true,
|
||||
Run: func(ctx context.Context) error { return want },
|
||||
OnError: func(name string, err error) { got <- err },
|
||||
})
|
||||
s.Start()
|
||||
defer s.Stop()
|
||||
select {
|
||||
case err := <-got:
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("错误透出失真: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("未收到 OnError")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
// Package errors 平台错误码体系。
|
||||
// 格式:ISC:xxx(ISC 为平台标识,xxx 三位数字);000 成功。
|
||||
// 段位:001-099 发送链路(网关/账号/通道准入、回执与终端状态、路由与内容合规);
|
||||
//
|
||||
// 400 段通用请求错误;500 段系统错误。
|
||||
//
|
||||
// 权威业务码表由产品侧定义(Day 15 修订),与 system_error_code 表
|
||||
// 经 v4 迁移以本包 All() 为单一事实来源对齐。
|
||||
package errors
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Error 平台错误:错误码 + 中英文描述 + 建议 HTTP 状态码。
|
||||
type Error struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
MsgEn string `json:"msg_en"`
|
||||
HTTPStatus int `json:"-"`
|
||||
}
|
||||
|
||||
// Error 实现 error 接口。
|
||||
func (e *Error) Error() string { return e.Code + " " + e.Msg }
|
||||
|
||||
// WithMsg 派生错误:保留错误码,覆盖中文消息(场景化提示)。
|
||||
func (e *Error) WithMsg(msg string) *Error {
|
||||
return &Error{Code: e.Code, Msg: msg, MsgEn: e.MsgEn, HTTPStatus: e.HTTPStatus}
|
||||
}
|
||||
|
||||
var codePattern = regexp.MustCompile(`^ISC:\d{3}$`)
|
||||
|
||||
// IsValidCode 校验错误码格式(ISC:xxx,三位数字)。
|
||||
func IsValidCode(code string) bool { return codePattern.MatchString(code) }
|
||||
|
||||
var (
|
||||
OK = &Error{Code: "ISC:000", Msg: "成功", MsgEn: "OK", HTTPStatus: http.StatusOK}
|
||||
|
||||
// ---- 网关/账号/通道准入(001-009,权威码表)----
|
||||
GatewayError = &Error{"ISC:001", "网关错误", "gateway error", http.StatusBadGateway}
|
||||
AccountDisabled = &Error{"ISC:002", "账号被禁用", "account disabled", http.StatusForbidden}
|
||||
SignatureDisabled = &Error{"ISC:003", "签名被禁用", "signature disabled", http.StatusForbidden}
|
||||
SignatureInvalid = &Error{"ISC:004", "签名非法", "invalid signature", http.StatusBadRequest}
|
||||
ChannelInvalid = &Error{"ISC:005", "通道非法", "invalid channel", http.StatusBadRequest}
|
||||
Blacklist = &Error{"ISC:006", "黑名单", "blacklisted number", http.StatusForbidden}
|
||||
Arrears = &Error{"ISC:007", "欠费", "arrears / insufficient balance", http.StatusPaymentRequired}
|
||||
SMSCInternalError = &Error{"ISC:009", "短信网关内部错误", "SMSC internal error", http.StatusBadGateway}
|
||||
|
||||
// ---- 回执与终端状态(010-026,权威码表)----
|
||||
ReportTimeout = &Error{"ISC:010", "发送回执超时", "report timeout", http.StatusGatewayTimeout}
|
||||
UserOffline = &Error{"ISC:011", "用户不在线", "user offline", http.StatusOK}
|
||||
UserSuspended = &Error{"ISC:012", "用户停机", "user suspended", http.StatusOK}
|
||||
NoRoutePortability = &Error{"ISC:013", "没有路由,携号转网", "no route, number portability", http.StatusOK}
|
||||
UserOutOfService = &Error{"ISC:014", "用户不在服务区", "user out of service area", http.StatusOK}
|
||||
PortOffline = &Error{"ISC:015", "端口下线", "port offline", http.StatusBadGateway}
|
||||
TerminalRespTimeout = &Error{"ISC:016", "等待5G消息终端响应超时", "5G terminal response timeout", http.StatusGatewayTimeout}
|
||||
EncodingInvalid = &Error{"ISC:017", "消息编码非法", "invalid message encoding", http.StatusBadRequest}
|
||||
FiveGNotEnabled = &Error{"ISC:018", "用户未开通5G消息", "5G messaging not enabled", http.StatusOK}
|
||||
MMSDownloadTimeout = &Error{"ISC:019", "彩信下发成功,下载超时", "MMS delivered, download timeout", http.StatusOK}
|
||||
TerminalStorageFull = &Error{"ISC:020", "终端存储空间满", "terminal storage full", http.StatusOK}
|
||||
MMSFallbackDelayed = &Error{"ISC:021", "回落彩信延迟获取", "fallback MMS delayed", http.StatusOK}
|
||||
MMSNotSupported = &Error{"ISC:022", "不支持彩信", "MMS not supported", http.StatusOK}
|
||||
NumberingDataError = &Error{"ISC:023", "码号局数据错误", "numbering plan data error", http.StatusBadGateway}
|
||||
TerminalFailure = &Error{"ISC:024", "短信已下发,但是终端故障", "delivered but terminal failure", http.StatusOK}
|
||||
CarrierUnknown = &Error{"ISC:025", "找不到归属运营商,可能是新号段", "carrier unknown, possibly new number segment", http.StatusBadRequest}
|
||||
MMSReportUnrecognized = &Error{"ISC:026", "无法识别彩信回执", "unrecognized MMS report", http.StatusBadGateway}
|
||||
EmptyNumber = &Error{"ISC:027", "空号", "empty number", http.StatusBadRequest}
|
||||
|
||||
// ---- 路由与内容合规(029-043,权威码表)----
|
||||
NoChannelRoute = &Error{"ISC:029", "无通道路由", "no channel route", http.StatusServiceUnavailable}
|
||||
ChannelRejected = &Error{"ISC:030", "通道拒绝", "channel rejected", http.StatusForbidden}
|
||||
ContentNonCompliant = &Error{"ISC:031", "内容不合规拦截", "non-compliant content blocked", http.StatusForbidden}
|
||||
SendLimited = &Error{"ISC:032", "发送限制", "send limit exceeded", http.StatusTooManyRequests}
|
||||
TextTooLong = &Error{"ISC:033", "超出账号设置的最大文本长度", "text exceeds account max length", http.StatusBadRequest}
|
||||
DailyLimitExceeded = &Error{"ISC:034", "超出账号每天发送量限制", "account daily limit exceeded", http.StatusTooManyRequests}
|
||||
CreditLimitExceeded = &Error{"ISC:035", "超出账号授信额限制", "account credit limit exceeded", http.StatusPaymentRequired}
|
||||
NightRateLimited = &Error{"ISC:040", "夜间频控", "nightly rate control", http.StatusTooManyRequests}
|
||||
FiveGBlocked = &Error{"ISC:043", "5G消息拦截", "5G message blocked", http.StatusForbidden}
|
||||
BlockedRegion = &Error{"ISC:036", "地区屏蔽", "region blocked", http.StatusForbidden}
|
||||
AuditStatusConflict = &Error{"ISC:037", "审核状态冲突(非待审)", "audit status conflict", http.StatusConflict}
|
||||
TaskStatusConflict = &Error{"ISC:038", "任务状态冲突(不可取消/改期)", "task status conflict", http.StatusConflict}
|
||||
|
||||
// ---- 通用请求错误(400 段,段值与 HTTP 语义对齐)----
|
||||
InvalidParam = &Error{"ISC:400", "参数错误", "invalid parameter", http.StatusBadRequest}
|
||||
Unauthorized = &Error{"ISC:401", "未认证", "unauthorized", http.StatusUnauthorized}
|
||||
TokenExpired = &Error{"ISC:402", "Token 已过期", "token expired", http.StatusUnauthorized}
|
||||
Forbidden = &Error{"ISC:403", "无权限", "forbidden", http.StatusForbidden}
|
||||
NotFound = &Error{"ISC:404", "资源不存在", "not found", http.StatusNotFound}
|
||||
MethodNotAllowed = &Error{"ISC:405", "方法不允许", "method not allowed", http.StatusMethodNotAllowed}
|
||||
RateLimited = &Error{"ISC:429", "请求过于频繁", "rate limited", http.StatusTooManyRequests}
|
||||
|
||||
// ---- 系统错误(500 段,500 语义为权威码表定义)----
|
||||
InternalError = &Error{"ISC:500", "其他错误,需要人工核查", "other error, manual review required", http.StatusInternalServerError}
|
||||
DBError = &Error{"ISC:501", "数据库错误", "database error", http.StatusInternalServerError}
|
||||
CacheError = &Error{"ISC:502", "缓存错误", "cache error", http.StatusInternalServerError}
|
||||
QueueError = &Error{"ISC:503", "消息队列错误", "message queue error", http.StatusInternalServerError}
|
||||
ServiceUnavailable = &Error{"ISC:504", "服务暂不可用", "service unavailable", http.StatusServiceUnavailable}
|
||||
)
|
||||
|
||||
// registry 全量注册(ByCode 查询)。
|
||||
var registry = func() map[string]*Error {
|
||||
all := []*Error{
|
||||
OK,
|
||||
// 准入
|
||||
GatewayError, AccountDisabled, SignatureDisabled, SignatureInvalid,
|
||||
ChannelInvalid, Blacklist, Arrears, SMSCInternalError,
|
||||
// 回执与终端
|
||||
ReportTimeout, UserOffline, UserSuspended, NoRoutePortability,
|
||||
UserOutOfService, PortOffline, TerminalRespTimeout, EncodingInvalid,
|
||||
FiveGNotEnabled, MMSDownloadTimeout, TerminalStorageFull, MMSFallbackDelayed,
|
||||
MMSNotSupported, NumberingDataError, TerminalFailure, CarrierUnknown,
|
||||
MMSReportUnrecognized, EmptyNumber,
|
||||
// 路由与内容
|
||||
NoChannelRoute, ChannelRejected, ContentNonCompliant, SendLimited,
|
||||
TextTooLong, DailyLimitExceeded, CreditLimitExceeded, NightRateLimited, FiveGBlocked,
|
||||
BlockedRegion,
|
||||
// 审核与任务(Day 43)
|
||||
AuditStatusConflict, TaskStatusConflict,
|
||||
// 通用
|
||||
InvalidParam, Unauthorized, TokenExpired, Forbidden, NotFound,
|
||||
MethodNotAllowed, RateLimited,
|
||||
// 系统
|
||||
InternalError, DBError, CacheError, QueueError, ServiceUnavailable,
|
||||
}
|
||||
m := make(map[string]*Error, len(all))
|
||||
for _, e := range all {
|
||||
m[e.Code] = e
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
// ByCode 按错误码查询(未知码返回 InternalError)。
|
||||
func ByCode(code string) *Error {
|
||||
if e, ok := registry[code]; ok {
|
||||
return e
|
||||
}
|
||||
return InternalError
|
||||
}
|
||||
|
||||
// All 返回全部预定义错误(管理接口/迁移种子/对账用)。
|
||||
func All() []*Error {
|
||||
out := make([]*Error, 0, len(registry))
|
||||
for _, e := range registry {
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestCodeFormat 验收项:全部预定义错误码符合 ISC:xxx 格式。
|
||||
func TestCodeFormat(t *testing.T) {
|
||||
for _, e := range All() {
|
||||
if !IsValidCode(e.Code) {
|
||||
t.Errorf("错误码格式非法: %q", e.Code)
|
||||
}
|
||||
if e.Msg == "" || e.MsgEn == "" || e.HTTPStatus == 0 {
|
||||
t.Errorf("错误 %s 缺少描述或状态码", e.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBlacklist 验收锚点:黑名单错误码定义。
|
||||
func TestBlacklist(t *testing.T) {
|
||||
if Blacklist.Code != "ISC:006" || Blacklist.Msg != "黑名单" {
|
||||
t.Fatalf("Blacklist = %+v, 期望 ISC:006 黑名单", Blacklist)
|
||||
}
|
||||
if Blacklist.HTTPStatus != http.StatusForbidden {
|
||||
t.Errorf("Blacklist.HTTPStatus = %d, 期望 403", Blacklist.HTTPStatus)
|
||||
}
|
||||
}
|
||||
|
||||
// TestByCode 覆盖查询与未知码回退。
|
||||
func TestByCode(t *testing.T) {
|
||||
if got := ByCode("ISC:006"); got != Blacklist {
|
||||
t.Errorf("ByCode(ISC:006) = %v", got)
|
||||
}
|
||||
if got := ByCode("ISC:999"); got != InternalError {
|
||||
t.Errorf("未知码应回退 InternalError, 实际 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWithMsg 派生错误保留错误码。
|
||||
func TestWithMsg(t *testing.T) {
|
||||
e := Blacklist.WithMsg("号码 138**** 已加入黑名单")
|
||||
if e.Code != Blacklist.Code || e.Msg == Blacklist.Msg {
|
||||
t.Errorf("WithMsg 派生异常: %+v", e)
|
||||
}
|
||||
if e.Error() == "" {
|
||||
t.Error("Error() 不应为空")
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsValidCode 覆盖非法格式。
|
||||
func TestIsValidCode(t *testing.T) {
|
||||
for _, bad := range []string{"UT:0006", "ISC:00", "ISC:0006", "isc:006", "", "ISC:0a6"} {
|
||||
if IsValidCode(bad) {
|
||||
t.Errorf("%q 应为非法格式", bad)
|
||||
}
|
||||
}
|
||||
for _, ok := range []string{"ISC:000", "ISC:500"} {
|
||||
if !IsValidCode(ok) {
|
||||
t.Errorf("%q 应为合法格式", ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuthoritativeCatalog 权威码表锚点:产品侧清单关键码逐条核对。
|
||||
func TestAuthoritativeCatalog(t *testing.T) {
|
||||
want := map[string]string{
|
||||
"ISC:001": "网关错误",
|
||||
"ISC:004": "签名非法",
|
||||
"ISC:007": "欠费",
|
||||
"ISC:009": "短信网关内部错误",
|
||||
"ISC:013": "没有路由,携号转网",
|
||||
"ISC:018": "用户未开通5G消息",
|
||||
"ISC:025": "找不到归属运营商,可能是新号段",
|
||||
"ISC:029": "无通道路由",
|
||||
"ISC:031": "内容不合规拦截",
|
||||
"ISC:035": "超出账号授信额限制",
|
||||
"ISC:040": "夜间频控",
|
||||
"ISC:043": "5G消息拦截",
|
||||
"ISC:500": "其他错误,需要人工核查",
|
||||
}
|
||||
for code, msg := range want {
|
||||
if got := ByCode(code); got.Code != code || got.Msg != msg {
|
||||
t.Errorf("ByCode(%s) = {%s %s}, 期望 {%s %s}", code, got.Code, got.Msg, code, msg)
|
||||
}
|
||||
}
|
||||
// 全量 51 条 = 成功码 ISC:000 + 错误码 50 条(权威清单;Day42 +ISC:036;Day43 +037/038)。
|
||||
if n := len(All()); n != 51 {
|
||||
t.Errorf("预定义码数量 = %d, 期望 51(含成功码)", n)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// openapi_sync_test.go Day 41(清债②):codes.go ↔ openapi.yaml x-isc-dict 双向锁定,
|
||||
// 任一侧改动未同步即测试失败(再跑 go run ./tools/gen-dict 重生成前端字典)。
|
||||
package errors
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestOpenApiIscDictSync(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "..", "api", "openapi.yaml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var doc struct {
|
||||
IscDict map[string]string `yaml:"x-isc-dict"`
|
||||
}
|
||||
if err := yaml.Unmarshal(raw, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(doc.IscDict) == 0 {
|
||||
t.Fatal("openapi.yaml 缺 x-isc-dict")
|
||||
}
|
||||
all := All()
|
||||
if len(all) != len(doc.IscDict) {
|
||||
t.Fatalf("码表数量不一致: codes.go=%d openapi=%d(跑 go run ./tools/gen-dict 同步)", len(all), len(doc.IscDict))
|
||||
}
|
||||
for _, e := range all {
|
||||
cn, ok := doc.IscDict[e.Code]
|
||||
if !ok {
|
||||
t.Errorf("codes.go 码 %s(%s) 未进 openapi x-isc-dict", e.Code, e.Msg)
|
||||
continue
|
||||
}
|
||||
if cn != e.Msg {
|
||||
t.Errorf("码 %s 中文不一致: codes.go=%q openapi=%q", e.Code, e.Msg, cn)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// Package jwt JWT 签发与解析(Day 17):
|
||||
// Claims 携带 account_id / roles / permissions,供网关鉴权与 RBAC 快速判定。
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// Claims go-sms JWT 载荷。
|
||||
type Claims struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Username string `json:"username"`
|
||||
Roles []string `json:"roles"` // 角色码(admin/ops/service/group/customer)
|
||||
Permissions []string `json:"permissions"` // 权限码快照(resource:action)
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// Manager JWT 管理器(签发 + 解析,统一 HMAC-SHA256)。
|
||||
// 字段导出:允许跨包直接构造(绕过 NewManager 默认值,如负 TTL 测试场景)。
|
||||
type Manager struct {
|
||||
Secret string
|
||||
TTL time.Duration
|
||||
Issuer string
|
||||
}
|
||||
|
||||
// NewManager 构造;ttl<=0 使用默认 2h。
|
||||
func NewManager(secret string, ttl time.Duration) *Manager {
|
||||
if ttl <= 0 {
|
||||
ttl = 2 * time.Hour
|
||||
}
|
||||
return &Manager{Secret: secret, TTL: ttl, Issuer: "go-sms"}
|
||||
}
|
||||
|
||||
// Validity 返回令牌有效期。
|
||||
func (m *Manager) Validity() time.Duration { return m.TTL }
|
||||
|
||||
// Sign 签发访问令牌。
|
||||
func (m *Manager) Sign(accountID int64, username string, roles, permissions []string) (string, error) {
|
||||
if username == "" {
|
||||
return "", errors.New("jwt: username 不能为空")
|
||||
}
|
||||
if len(roles) == 0 {
|
||||
return "", errors.New("jwt: roles 不能为空")
|
||||
}
|
||||
now := time.Now()
|
||||
claims := &Claims{
|
||||
AccountID: accountID,
|
||||
Username: username,
|
||||
Roles: roles,
|
||||
Permissions: permissions,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ID: newJTI(),
|
||||
Issuer: m.Issuer,
|
||||
Subject: fmt.Sprintf("%d", accountID),
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(m.TTL)),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
},
|
||||
}
|
||||
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(m.Secret))
|
||||
}
|
||||
|
||||
// newJTI 令牌唯一标识(crypto/rand 16 字节 hex)。
|
||||
// 确保同一秒内多次签发的令牌互不相同——否则刷新轮换(旧令牌拉黑 +
|
||||
// 重签)在同一秒会产生与旧令牌完全相同的串,导致新令牌被自己的黑名单误杀。
|
||||
func newJTI() string {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return fmt.Sprintf("t%d", nowNano())
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// nowNano rand 失败时的时间戳退化源。
|
||||
func nowNano() int64 { return time.Now().UnixNano() }
|
||||
|
||||
// Parse 解析并校验令牌(强制 HMAC 系列签名,防 alg 混淆)。
|
||||
func (m *Manager) Parse(token string) (*Claims, error) {
|
||||
claims := &Claims{}
|
||||
parsed, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (any, error) {
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.ErrSignatureInvalid
|
||||
}
|
||||
return []byte(m.Secret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("jwt: 令牌无效: %w", err)
|
||||
}
|
||||
if !parsed.Valid {
|
||||
return nil, errors.New("jwt: 令牌无效")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestSignParseRoundTrip 验收:生成 Token 可正确解析(含 roles/permissions)。
|
||||
func TestSignParseRoundTrip(t *testing.T) {
|
||||
m := NewManager("day17-secret", time.Hour)
|
||||
tok, err := m.Sign(1001, "alice", []string{"ops", "service"}, []string{"account:read", "channel:write"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tok == "" {
|
||||
t.Fatal("令牌为空")
|
||||
}
|
||||
claims, err := m.Parse(tok)
|
||||
if err != nil {
|
||||
t.Fatalf("解析失败: %v", err)
|
||||
}
|
||||
if claims.AccountID != 1001 || claims.Username != "alice" {
|
||||
t.Fatalf("账户字段 = %+v", claims)
|
||||
}
|
||||
if len(claims.Roles) != 2 || claims.Roles[0] != "ops" {
|
||||
t.Errorf("roles = %v", claims.Roles)
|
||||
}
|
||||
if len(claims.Permissions) != 2 || claims.Permissions[1] != "channel:write" {
|
||||
t.Errorf("permissions = %v", claims.Permissions)
|
||||
}
|
||||
if claims.Issuer != "go-sms" || claims.Subject != "1001" {
|
||||
t.Errorf("注册字段: issuer=%s sub=%s", claims.Issuer, claims.Subject)
|
||||
}
|
||||
if claims.ExpiresAt.Before(time.Now()) {
|
||||
t.Error("过期时间应在未来")
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseExpired 过期令牌拒绝(直接构造负 TTL,绕过构造器默认值)。
|
||||
func TestParseExpired(t *testing.T) {
|
||||
m := &Manager{Secret: "s", TTL: -time.Minute, Issuer: "go-sms"}
|
||||
tok, _ := m.Sign(1, "u", []string{"customer"}, nil)
|
||||
if _, err := m.Parse(tok); err == nil {
|
||||
t.Fatal("过期令牌应解析失败")
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseWrongSecret 错误密钥拒绝。
|
||||
func TestParseWrongSecret(t *testing.T) {
|
||||
tok, _ := NewManager("right", time.Hour).Sign(1, "u", []string{"admin"}, nil)
|
||||
if _, err := NewManager("wrong", time.Hour).Parse(tok); err == nil {
|
||||
t.Fatal("错误密钥应解析失败")
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseGarbage 垃圾串/空令牌拒绝。
|
||||
func TestParseGarbage(t *testing.T) {
|
||||
m := NewManager("s", time.Hour)
|
||||
for _, bad := range []string{"", "not.a.jwt", "a.b.c"} {
|
||||
if _, err := m.Parse(bad); err == nil {
|
||||
t.Errorf("垃圾串 %q 应解析失败", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSignValidation 入参校验。
|
||||
func TestSignValidation(t *testing.T) {
|
||||
m := NewManager("s", time.Hour)
|
||||
if _, err := m.Sign(1, "", []string{"admin"}, nil); err == nil {
|
||||
t.Error("空 username 应报错")
|
||||
}
|
||||
if _, err := m.Sign(1, "u", nil, nil); err == nil {
|
||||
t.Error("空 roles 应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultTTL 默认 TTL。
|
||||
func TestDefaultTTL(t *testing.T) {
|
||||
if got := NewManager("s", 0).Validity(); got != 2*time.Hour {
|
||||
t.Errorf("默认 TTL = %v, 期望 2h", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
// Package lock 基于 Redis 的分布式锁(Day 6)。
|
||||
// 安全性:value 使用随机令牌,释放与续期均通过 Lua 脚本原子校验,
|
||||
// 防止误删他人锁;可用性:后台 watchdog 按间隔自动续期,防止业务未完成锁先过期。
|
||||
package lock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// 错误定义。
|
||||
var (
|
||||
// ErrLockHeld 锁已被其他持有者占用。
|
||||
ErrLockHeld = errors.New("lock: 已被其他持有者持有")
|
||||
// ErrLockNotHeld 当前持有者已不再持有该锁(已释放/已过期)。
|
||||
ErrLockNotHeld = errors.New("lock: 当前持有者未持有该锁")
|
||||
)
|
||||
|
||||
// 释放脚本:仅当 value 匹配时删除(原子,防误删)。
|
||||
const releaseScript = `if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("del", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end`
|
||||
|
||||
// 续期脚本:仅当 value 匹配时重置 TTL(原子)。
|
||||
const renewScript = `if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("pexpire", KEYS[1], ARGV[2])
|
||||
else
|
||||
return 0
|
||||
end`
|
||||
|
||||
// 默认参数。
|
||||
const (
|
||||
defaultRetryInterval = 100 * time.Millisecond // Lock 阻塞获取的轮询间隔
|
||||
watchdogFactor = 3 // 续期间隔 = TTL / 3
|
||||
)
|
||||
|
||||
// Locker 分布式锁入口。
|
||||
type Locker struct {
|
||||
client redis.UniversalClient
|
||||
// RetryInterval 阻塞式 Lock 的轮询间隔(默认 100ms,可按需调整)。
|
||||
RetryInterval time.Duration
|
||||
}
|
||||
|
||||
// New 创建 Locker;client 可直接使用 cache.Client(其嵌入了 UniversalClient)。
|
||||
func New(client redis.UniversalClient) *Locker {
|
||||
return &Locker{client: client, RetryInterval: defaultRetryInterval}
|
||||
}
|
||||
|
||||
// Lock 表示一次成功获取的锁;Unlock 后应停止使用。
|
||||
type Lock struct {
|
||||
client redis.UniversalClient
|
||||
key string
|
||||
token string
|
||||
ttl time.Duration
|
||||
stop chan struct{}
|
||||
stopOne sync.Once
|
||||
}
|
||||
|
||||
// TryLock 非阻塞获取锁:SET key token NX PX ttl。
|
||||
// 被他人占用返回 ErrLockHeld;获取成功即启动 watchdog 自动续期。
|
||||
func (l *Locker) TryLock(ctx context.Context, key string, ttl time.Duration) (*Lock, error) {
|
||||
if ttl <= 0 {
|
||||
return nil, errors.New("lock: ttl 必须大于 0")
|
||||
}
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ok, err := l.client.SetNX(ctx, key, token, ttl).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, ErrLockHeld
|
||||
}
|
||||
lk := &Lock{
|
||||
client: l.client,
|
||||
key: key,
|
||||
token: token,
|
||||
ttl: ttl,
|
||||
stop: make(chan struct{}),
|
||||
}
|
||||
lk.startWatchdog()
|
||||
return lk, nil
|
||||
}
|
||||
|
||||
// Lock 阻塞获取锁:轮询 TryLock 直到成功或 ctx 取消/超时。
|
||||
func (l *Locker) Lock(ctx context.Context, key string, ttl time.Duration) (*Lock, error) {
|
||||
interval := l.RetryInterval
|
||||
if interval <= 0 {
|
||||
interval = defaultRetryInterval
|
||||
}
|
||||
timer := time.NewTimer(interval)
|
||||
defer timer.Stop()
|
||||
for {
|
||||
lk, err := l.TryLock(ctx, key, ttl)
|
||||
if err == nil {
|
||||
return lk, nil
|
||||
}
|
||||
if !errors.Is(err, ErrLockHeld) {
|
||||
return nil, err // 连接等底层错误,直接返回
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-timer.C:
|
||||
timer.Reset(interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Key 返回锁键名。
|
||||
func (lk *Lock) Key() string { return lk.key }
|
||||
|
||||
// TTL 返回锁的初始 TTL。
|
||||
func (lk *Lock) TTL() time.Duration { return lk.ttl }
|
||||
|
||||
// Unlock 释放锁并停止续期;未持有(已释放/被覆盖)返回 ErrLockNotHeld。
|
||||
func (lk *Lock) Unlock(ctx context.Context) error {
|
||||
lk.stopWatchdog()
|
||||
res, err := lk.client.Eval(ctx, releaseScript, []string{lk.key}, lk.token).Int64()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res != 1 {
|
||||
return ErrLockNotHeld
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// startWatchdog 启动自动续期:每 ttl/3 续期一次,Unlock 或续期失败时退出。
|
||||
func (lk *Lock) startWatchdog() {
|
||||
interval := lk.ttl / watchdogFactor
|
||||
if interval < 10*time.Millisecond {
|
||||
interval = 10 * time.Millisecond
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-lk.stop:
|
||||
return
|
||||
case <-ticker.C:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), interval)
|
||||
res, err := lk.client.Eval(ctx, renewScript,
|
||||
[]string{lk.key}, lk.token, lk.ttl.Milliseconds()).Int64()
|
||||
cancel()
|
||||
if err != nil || res != 1 {
|
||||
// 锁已丢失(过期/被删/网络错误):停止续期,业务侧应尽快结束临界区
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// stopWatchdog 幂等停止续期协程。
|
||||
func (lk *Lock) stopWatchdog() {
|
||||
lk.stopOne.Do(func() { close(lk.stop) })
|
||||
}
|
||||
|
||||
// newToken 生成 16 字节随机令牌,确保不同持有者 value 不同。
|
||||
func newToken() (string, error) {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// newMiniClient 启动 miniredis 并返回直连客户端。
|
||||
func newMiniClient(t *testing.T) redis.UniversalClient {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
return redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
}
|
||||
|
||||
// TestTryLock_MutualExclusion 验证锁互斥与释放后可再获取。
|
||||
func TestTryLock_MutualExclusion(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
l1, err := lk.TryLock(ctx, "go-sms:lock:1", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("首次 TryLock 失败: %v", err)
|
||||
}
|
||||
if _, err := lk.TryLock(ctx, "go-sms:lock:1", 10*time.Second); !errors.Is(err, ErrLockHeld) {
|
||||
t.Fatalf("重复 TryLock 应返回 ErrLockHeld, 实际: %v", err)
|
||||
}
|
||||
if err := l1.Unlock(ctx); err != nil {
|
||||
t.Fatalf("Unlock 失败: %v", err)
|
||||
}
|
||||
l2, err := lk.TryLock(ctx, "go-sms:lock:1", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("释放后 TryLock 应成功, 实际: %v", err)
|
||||
}
|
||||
_ = l2.Unlock(ctx)
|
||||
}
|
||||
|
||||
// TestUnlock_NotHeld 验证误删保护:value 不匹配时不删除他人锁。
|
||||
func TestUnlock_NotHeld(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
l1, err := lk.TryLock(ctx, "go-sms:lock:2", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock 失败: %v", err)
|
||||
}
|
||||
|
||||
// 模拟锁被覆盖(他人以不同 token 抢到)
|
||||
if err := c.Set(ctx, "go-sms:lock:2", "other-token", 10*time.Second).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := l1.Unlock(ctx); !errors.Is(err, ErrLockNotHeld) {
|
||||
t.Fatalf("value 不匹配的 Unlock 应返回 ErrLockNotHeld, 实际: %v", err)
|
||||
}
|
||||
// 他人锁不应被误删
|
||||
if c.Get(ctx, "go-sms:lock:2").Val() != "other-token" {
|
||||
t.Fatal("误删了他人锁")
|
||||
}
|
||||
// 已释放的锁再次 Unlock
|
||||
if err := l1.Unlock(ctx); !errors.Is(err, ErrLockNotHeld) {
|
||||
t.Fatalf("二次 Unlock 应返回 ErrLockNotHeld, 实际: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWatchdog_AutoRenew 验证 watchdog 自动续期:TTL 内未 Unlock,锁不过期。
|
||||
func TestWatchdog_AutoRenew(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
const ttl = 200 * time.Millisecond
|
||||
l1, err := lk.TryLock(ctx, "go-sms:lock:3", ttl)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock 失败: %v", err)
|
||||
}
|
||||
// 等待远超 TTL(续期间隔 ttl/3≈67ms,期间应完成多次续期)
|
||||
time.Sleep(3 * ttl)
|
||||
if !mr.Exists("go-sms:lock:3") {
|
||||
t.Fatal("watchdog 未续期:锁在 TTL 后丢失")
|
||||
}
|
||||
if err := l1.Unlock(ctx); err != nil {
|
||||
t.Fatalf("Unlock 失败: %v", err)
|
||||
}
|
||||
if mr.Exists("go-sms:lock:3") {
|
||||
t.Fatal("Unlock 后 key 应删除")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWatchdog_StopsAfterUnlock 验证 Unlock 后续期协程停止(不再复活 key)。
|
||||
func TestWatchdog_StopsAfterUnlock(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
l1, err := lk.TryLock(ctx, "go-sms:lock:4", 300*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock 失败: %v", err)
|
||||
}
|
||||
if err := l1.Unlock(ctx); err != nil {
|
||||
t.Fatalf("Unlock 失败: %v", err)
|
||||
}
|
||||
time.Sleep(400 * time.Millisecond) // 超过原 TTL
|
||||
if mr.Exists("go-sms:lock:4") {
|
||||
t.Fatal("Unlock 后 watchdog 不应再续期复活 key")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLock_Blocking 验证阻塞获取:他人释放后等待者获得锁。
|
||||
func TestLock_Blocking(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
l1, err := lk.TryLock(ctx, "go-sms:lock:5", 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock 失败: %v", err)
|
||||
}
|
||||
|
||||
var got int32
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
ctx2, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
l2, err := lk.Lock(ctx2, "go-sms:lock:5", 5*time.Second)
|
||||
if err != nil {
|
||||
t.Errorf("阻塞 Lock 失败: %v", err)
|
||||
return
|
||||
}
|
||||
atomic.StoreInt32(&got, 1)
|
||||
_ = l2.Unlock(context.Background())
|
||||
}()
|
||||
|
||||
time.Sleep(300 * time.Millisecond) // 确保等待者已在轮询
|
||||
if atomic.LoadInt32(&got) != 0 {
|
||||
t.Fatal("锁未释放前等待者不应获得锁")
|
||||
}
|
||||
if err := l1.Unlock(ctx); err != nil {
|
||||
t.Fatalf("Unlock 失败: %v", err)
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("等待者未在超时内获得锁")
|
||||
}
|
||||
if atomic.LoadInt32(&got) != 1 {
|
||||
t.Fatal("等待者应成功获得锁")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLock_ContextCanceled 验证 ctx 取消时阻塞 Lock 返回错误。
|
||||
func TestLock_ContextCanceled(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
l1, _ := lk.TryLock(ctx, "go-sms:lock:6", 5*time.Second)
|
||||
defer func() { _ = l1.Unlock(context.Background()) }()
|
||||
|
||||
go func() {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
cancel()
|
||||
}()
|
||||
start := time.Now()
|
||||
if _, err := lk.Lock(ctx, "go-sms:lock:6", 5*time.Second); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("ctx 取消应返回 context.Canceled, 实际: %v", err)
|
||||
}
|
||||
if time.Since(start) > time.Second {
|
||||
t.Fatal("取消后应迅速返回")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTryLock_Concurrency 并发场景:同一 key 仅一个持有者成功。
|
||||
func TestTryLock_Concurrency(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
ctx := context.Background()
|
||||
|
||||
const n = 16
|
||||
var mu sync.Mutex
|
||||
acquired := 0
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
got, err := lk.TryLock(ctx, "go-sms:lock:7", 2*time.Second)
|
||||
if err == nil {
|
||||
mu.Lock()
|
||||
acquired++
|
||||
mu.Unlock()
|
||||
time.Sleep(50 * time.Millisecond) // 模拟临界区
|
||||
_ = got.Unlock(context.Background())
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if acquired == 0 {
|
||||
t.Fatal("并发下至少应有一次获取成功")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTryLock_InvalidTTL 验证非法 TTL 拦截。
|
||||
func TestTryLock_InvalidTTL(t *testing.T) {
|
||||
c := newMiniClient(t)
|
||||
lk := New(c)
|
||||
if _, err := lk.TryLock(context.Background(), "k", 0); err == nil {
|
||||
t.Fatal("ttl=0 应报错")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package logger 结构化日志封装(zap + lumberjack)。Day 5 实现。
|
||||
package logger
|
||||
@@ -0,0 +1,170 @@
|
||||
// Package logger 结构化日志组件:zap + lumberjack(Day 5)。
|
||||
// 支持 JSON/Console 格式,stdout/文件双通道输出,文件按大小/天数滚动切割与压缩;
|
||||
// 通过 logger.Init 初始化全局实例,业务代码使用 logger.L() 获取。
|
||||
package logger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// std 全局日志实例(Init 后可用;未 Init 时降级为 Nop)。
|
||||
var std *zap.Logger = zap.NewNop()
|
||||
|
||||
// stdLevel 全局动态级别(Day 12 热更新:app 通过 logger.Level().SetLevel 调整)。
|
||||
var stdLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
|
||||
// FileOptions 日志文件滚动切割配置(对应 config.Log.File)。
|
||||
type FileOptions struct {
|
||||
Path string `mapstructure:"path"` // 文件路径
|
||||
MaxSizeMB int `mapstructure:"max_size_mb"` // 单文件最大 MB,超出切割
|
||||
MaxAgeDays int `mapstructure:"max_age_days"` // 旧文件保留天数
|
||||
MaxBackups int `mapstructure:"max_backups"` // 旧文件最大个数
|
||||
Compress bool `mapstructure:"compress"` // 是否 gzip 压缩旧文件
|
||||
}
|
||||
|
||||
// Options 日志配置(对应 config.Log)。
|
||||
type Options struct {
|
||||
Level string // debug | info | warn | error
|
||||
Format string // json | console
|
||||
Output []string // stdout / file(可同时)
|
||||
File FileOptions
|
||||
}
|
||||
|
||||
// Init 按配置构建并安装全局日志实例(级别可经 Level() 动态调整)。
|
||||
func Init(opt Options) error {
|
||||
lg, level, err := NewWithLevel(opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
std, stdLevel = lg, level
|
||||
return nil
|
||||
}
|
||||
|
||||
// L 返回全局日志实例。
|
||||
func L() *zap.Logger { return std }
|
||||
|
||||
// Level 返回全局动态级别(热更新入口:SetLevel 即时生效,无需重启)。
|
||||
func Level() zap.AtomicLevel { return stdLevel }
|
||||
|
||||
// Sync 刷新缓冲(进程退出前调用;文件通道确保落盘)。
|
||||
func Sync() { _ = std.Sync() }
|
||||
|
||||
// New 构造独立日志实例(测试或特殊场景使用)。
|
||||
func New(opt Options, opts ...zap.Option) (*zap.Logger, error) {
|
||||
level, err := parseLevel(opt.Level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newWithCoreLevel(opt, level, opts...)
|
||||
}
|
||||
|
||||
// NewWithLevel 同 New,并返回动态级别句柄(配置热更新时 SetLevel 即时生效)。
|
||||
func NewWithLevel(opt Options, opts ...zap.Option) (*zap.Logger, zap.AtomicLevel, error) {
|
||||
level, err := parseLevel(opt.Level)
|
||||
if err != nil {
|
||||
return nil, zap.AtomicLevel{}, err
|
||||
}
|
||||
atomic := zap.NewAtomicLevelAt(level)
|
||||
lg, err := newWithCoreLevel(opt, atomic, opts...)
|
||||
if err != nil {
|
||||
return nil, zap.AtomicLevel{}, err
|
||||
}
|
||||
return lg, atomic, nil
|
||||
}
|
||||
|
||||
// newWithCoreLevel 以指定 Core Level 构造(复用 New 的装配逻辑)。
|
||||
func newWithCoreLevel(opt Options, level zapcore.LevelEnabler, opts ...zap.Option) (*zap.Logger, error) {
|
||||
|
||||
encoderCfg := zapcore.EncoderConfig{
|
||||
TimeKey: "ts",
|
||||
LevelKey: "level",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
MessageKey: "msg",
|
||||
StacktraceKey: "stack",
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||
EncodeDuration: zapcore.MillisDurationEncoder,
|
||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||
}
|
||||
|
||||
var encoder = zapcore.NewJSONEncoder(encoderCfg)
|
||||
if opt.Format == "console" {
|
||||
encoder = zapcore.NewConsoleEncoder(encoderCfg)
|
||||
}
|
||||
|
||||
var cores []zapcore.Core
|
||||
|
||||
for _, out := range opt.Output {
|
||||
switch strings.ToLower(strings.TrimSpace(out)) {
|
||||
case "stdout":
|
||||
cores = append(cores, zapcore.NewCore(encoder, zapcore.Lock(os.Stdout), level))
|
||||
case "file":
|
||||
if opt.File.Path == "" {
|
||||
return nil, errors.New("log.output 含 file 但 log.file.path 为空")
|
||||
}
|
||||
if err := ensureWritable(opt.File.Path); err != nil {
|
||||
// 降级:目录不可写时跳过文件通道,避免持续 write error 刷屏
|
||||
fmt.Fprintf(os.Stderr, "[logger] WARN 日志文件 %s 不可写(%v),已降级为仅 stdout 输出\n", opt.File.Path, err)
|
||||
continue
|
||||
}
|
||||
w := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: opt.File.Path,
|
||||
MaxSize: opt.File.MaxSizeMB,
|
||||
MaxAge: opt.File.MaxAgeDays,
|
||||
MaxBackups: opt.File.MaxBackups,
|
||||
Compress: opt.File.Compress,
|
||||
})
|
||||
cores = append(cores, zapcore.NewCore(encoder, w, level))
|
||||
case "":
|
||||
// 跳过空项
|
||||
default:
|
||||
return nil, fmt.Errorf("未知日志输出通道: %q(可选 stdout/file)", out)
|
||||
}
|
||||
}
|
||||
if len(cores) == 0 { // file 全部降级时兜底 stdout,保证至少有日志
|
||||
cores = append(cores, zapcore.NewCore(encoder, zapcore.Lock(os.Stdout), level))
|
||||
}
|
||||
|
||||
opts = append(opts, zap.AddCaller()) // 输出调用位置,便于排障
|
||||
return zap.New(zapcore.NewTee(cores...), opts...), nil
|
||||
}
|
||||
|
||||
// parseLevel 解析日志级别字符串。
|
||||
func parseLevel(s string) (zapcore.Level, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(s)) {
|
||||
case "debug":
|
||||
return zapcore.DebugLevel, nil
|
||||
case "info", "":
|
||||
return zapcore.InfoLevel, nil
|
||||
case "warn":
|
||||
return zapcore.WarnLevel, nil
|
||||
case "error":
|
||||
return zapcore.ErrorLevel, nil
|
||||
default:
|
||||
return zapcore.InfoLevel, fmt.Errorf("未知日志级别: %q(可选 debug/info/warn/error)", s)
|
||||
}
|
||||
}
|
||||
|
||||
// ensureWritable 探测日志文件可写性:自动建目录并以追加模式试开一次。
|
||||
// 不可写返回错误(触发降级),成功则关闭句柄交由 lumberjack 接管。
|
||||
func ensureWritable(path string) error {
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// fileOptsFor 测试用文件配置。
|
||||
func fileOptsFor(path string) Options {
|
||||
return Options{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
Output: []string{"file"},
|
||||
File: FileOptions{Path: path, MaxSizeMB: 5, MaxAgeDays: 1, MaxBackups: 1},
|
||||
}
|
||||
}
|
||||
|
||||
// TestFileOutput_JSON 验证文件通道输出结构化 JSON 且字段完整。
|
||||
func TestFileOutput_JSON(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "app.log")
|
||||
lg, err := New(fileOptsFor(path))
|
||||
if err != nil {
|
||||
t.Fatalf("New 失败: %v", err)
|
||||
}
|
||||
lg.Info("hello_go-sms", zap.String("key", "value"))
|
||||
if err := lg.Sync(); err != nil {
|
||||
t.Fatalf("Sync 失败: %v", err)
|
||||
}
|
||||
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("读取日志文件失败: %v", err)
|
||||
}
|
||||
var entry map[string]any
|
||||
if err := json.Unmarshal(raw, &entry); err != nil {
|
||||
t.Fatalf("日志行不是合法 JSON: %v\n内容: %s", err, raw)
|
||||
}
|
||||
for _, key := range []string{"ts", "level", "msg", "caller", "key"} {
|
||||
if _, ok := entry[key]; !ok {
|
||||
t.Errorf("日志缺少字段 %q: %s", key, raw)
|
||||
}
|
||||
}
|
||||
if entry["level"] != "info" || entry["msg"] != "hello_go-sms" || entry["key"] != "value" {
|
||||
t.Errorf("日志字段值不符: %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInit_GlobalAndSync 验证全局实例安装与 L()/Sync() 可用。
|
||||
func TestInit_GlobalAndSync(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "app.log")
|
||||
if err := Init(fileOptsFor(path)); err != nil {
|
||||
t.Fatalf("Init 失败: %v", err)
|
||||
}
|
||||
L().Warn("global_warning", zap.Int("code", 1001))
|
||||
Sync()
|
||||
}
|
||||
|
||||
// TestNew_InvalidOptions 验证非法配置拦截与降级兜底。
|
||||
func TestNew_InvalidOptions(t *testing.T) {
|
||||
if _, err := New(Options{Level: "trace", Output: []string{"stdout"}}); err == nil {
|
||||
t.Error("非法 level 应返回错误")
|
||||
}
|
||||
if _, err := New(Options{Level: "info", Output: []string{"file"}, File: FileOptions{}}); err == nil {
|
||||
t.Error("output 含 file 但 path 为空应返回错误")
|
||||
}
|
||||
if _, err := New(Options{Level: "info", Output: []string{"kafka"}}); err == nil {
|
||||
t.Error("未知输出通道应返回错误")
|
||||
}
|
||||
// 无可用通道时兜底 stdout(不报错),保证至少有日志
|
||||
if lg, err := New(Options{Level: "info"}); err != nil || lg == nil {
|
||||
t.Errorf("空 output 应兜底 stdout: (%v, %v)", lg, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNew_FileDegradedToStdout 验证目录不可写时降级:构造成功且不 panic。
|
||||
// (Windows/权限差异环境无法稳定构造只读路径,用 /proc 下的非法路径触发)
|
||||
func TestNew_FileDegradedToStdout(t *testing.T) {
|
||||
lg, err := New(Options{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
Output: []string{"file"},
|
||||
File: FileOptions{Path: "/proc/gosms-definitely-not-writable/app.log"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("目录不可写应降级而非报错: %v", err)
|
||||
}
|
||||
if lg == nil {
|
||||
t.Fatal("降级后仍应返回可用 logger")
|
||||
}
|
||||
lg.Info("degraded_mode_ok")
|
||||
_ = lg.Sync()
|
||||
}
|
||||
|
||||
// TestParseLevel 覆盖级别解析分支。
|
||||
func TestParseLevel(t *testing.T) {
|
||||
for s, want := range map[string]int{"debug": -1, "INFO": 0, "warn": 1, "error": 2, "": 0} {
|
||||
got, err := parseLevel(s)
|
||||
if err != nil {
|
||||
t.Errorf("parseLevel(%q) 不应报错: %v", s, err)
|
||||
continue
|
||||
}
|
||||
if int(got) != want {
|
||||
t.Errorf("parseLevel(%q) = %d, 期望 %d", s, got, want)
|
||||
}
|
||||
}
|
||||
if _, err := parseLevel("fatal-x"); err == nil || !strings.Contains(err.Error(), "未知日志级别") {
|
||||
t.Errorf("非法级别应报错,实际: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewWithLevel_DynamicLevel 验证动态级别:SetLevel 即时生效(Day 12)。
|
||||
func TestNewWithLevel_DynamicLevel(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "app.log")
|
||||
lg, level, err := NewWithLevel(Options{
|
||||
Level: "info", Format: "json", Output: []string{"file"},
|
||||
File: FileOptions{Path: path},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lg.Debug("before_adjust") // info 级别下不可见
|
||||
level.SetLevel(zap.DebugLevel)
|
||||
lg.Debug("after_adjust") // 动态调级后可见
|
||||
level.SetLevel(zap.ErrorLevel)
|
||||
lg.Info("after_error") // 收紧后不可见
|
||||
_ = lg.Sync()
|
||||
|
||||
raw, _ := os.ReadFile(path)
|
||||
body := string(raw)
|
||||
if strings.Contains(body, "before_adjust") {
|
||||
t.Error("调级前 debug 不应输出")
|
||||
}
|
||||
if !strings.Contains(body, "after_adjust") {
|
||||
t.Error("SetLevel(Debug) 后 debug 应输出")
|
||||
}
|
||||
if strings.Contains(body, "after_error") {
|
||||
t.Error("SetLevel(Error) 后 info 不应输出")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
// Package metrics Prometheus 指标中心(Day 13):
|
||||
// 独立 Registry(不污染全局)+ HTTP 服务指标三件套 + 业务指标预留。
|
||||
// 暴露方式:app 装配 metrics.Handler() 至 GET /metrics。
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// durationBuckets 耗时直方图分桶(对齐 P99<50ms 目标精化低区间)。
|
||||
var durationBuckets = []float64{
|
||||
0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10,
|
||||
}
|
||||
|
||||
// HTTP 指标标签维度。
|
||||
const (
|
||||
LabelMethod = "method"
|
||||
LabelRoute = "route"
|
||||
LabelStatus = "status"
|
||||
LabelTopic = "topic"
|
||||
LabelKind = "kind"
|
||||
)
|
||||
|
||||
var (
|
||||
// HTTPRequestsTotal HTTP 请求总数(method/route/status)。
|
||||
HTTPRequestsTotal *prometheus.CounterVec
|
||||
// HTTPRequestDuration HTTP 请求耗时分布(秒,method/route)。
|
||||
HTTPRequestDuration *prometheus.HistogramVec
|
||||
// HTTPRequestsInFlight 当前在途请求数。
|
||||
HTTPRequestsInFlight prometheus.Gauge
|
||||
|
||||
// SMSSubmitTotal 短信提交总数(阶段 4 发送链路埋点:result)。
|
||||
SMSSubmitTotal *prometheus.CounterVec
|
||||
// SMSReportTotal 状态回执处理总数(阶段 5:status)。
|
||||
SMSReportTotal *prometheus.CounterVec
|
||||
// KafkaProducerMessages Kafka 生产结果(topic/kind=ok|retry|dlq)。
|
||||
KafkaProducerMessages *prometheus.CounterVec
|
||||
|
||||
// ---- 资金域(Day54 R1,前缀 go_sms_balance_ 与 alert_rules.yml/评审纪要三方锁定) ----
|
||||
// BalanceReconcileDiff 对账差额(Redis 余额 vs 流水重算;覆写前采样,{account})。
|
||||
BalanceReconcileDiff *prometheus.GaugeVec
|
||||
// BalanceMode 当前预扣模式(1=redis Lua/0=mysql 行锁;flag 切换即置)。
|
||||
BalanceMode prometheus.Gauge
|
||||
// BalanceFallbackTotal Redis 基础设施错回退 MySQL 次数(kill Redis 验收观测点)。
|
||||
BalanceFallbackTotal prometheus.Counter
|
||||
// BalanceFlowlogPending 异步流水队列水位(批写链路健康度)。
|
||||
BalanceFlowlogPending prometheus.Gauge
|
||||
|
||||
// ---- 消费域(Day55,挂账清偿) ----
|
||||
// KafkaLag submit 消费组 lag 合计({topic};背压 L1 告警源,Prometheus 抓取)。
|
||||
KafkaLag *prometheus.GaugeVec
|
||||
|
||||
// ---- 长短信聚合域(Day 69,百万级压测观测;Day 71 挂起制语义更新) ----
|
||||
// LongSmsMergedTotal 入向长短信聚合成功数(组到齐合并单次受理)。
|
||||
LongSmsMergedTotal prometheus.Counter
|
||||
// LongSmsDegradeTotal 退化直发片数({reason}: overflow=并发组超上限新组拒绝/
|
||||
// ref_conflict=同组 total 不一致协议异常片直发。Day 71 起 timeout 语义废除,
|
||||
// 挂起组到期走 LongSmsExpireFailTotal)。
|
||||
LongSmsDegradeTotal *prometheus.CounterVec
|
||||
// LongSmsExpireFailTotal Day 71:挂起组 72h 到期失败终态数(不齐不发,
|
||||
// 建 LP task+失败 detail+UNDELIV 回执给下家)。
|
||||
LongSmsExpireFailTotal prometheus.Counter
|
||||
// LongSmsDupFragTotal 同 idx 重复片幂等覆盖数(客户端重发观测)。
|
||||
LongSmsDupFragTotal prometheus.Counter
|
||||
// ReportRequeueTotal unpaired 回执短重投次数(秒回竞态窗口观测)。
|
||||
ReportRequeueTotal prometheus.Counter
|
||||
|
||||
// ---- 回执聚合域(Day 69 二轮,百万级压测观测) ----
|
||||
// ReportPendingTotal 多段聚合未到齐挂起事件数。
|
||||
ReportPendingTotal prometheus.Counter
|
||||
// ReportRecheckTotal pending 后新事务复查结果({result}: finalized=补刀
|
||||
// 终态/still_pending=真未到齐;双盲竞态健康度=finalized 占比)。
|
||||
ReportRecheckTotal *prometheus.CounterVec
|
||||
)
|
||||
|
||||
var registry = prometheus.NewRegistry()
|
||||
|
||||
func init() {
|
||||
HTTPRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "http_requests_total",
|
||||
Help: "HTTP 请求总数",
|
||||
}, []string{LabelMethod, LabelRoute, LabelStatus})
|
||||
|
||||
HTTPRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "http_request_duration_seconds",
|
||||
Help: "HTTP 请求耗时分布(秒)",
|
||||
Buckets: durationBuckets,
|
||||
}, []string{LabelMethod, LabelRoute})
|
||||
|
||||
HTTPRequestsInFlight = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "http_requests_in_flight",
|
||||
Help: "当前在途请求数",
|
||||
})
|
||||
|
||||
SMSSubmitTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "sms_submit_total",
|
||||
Help: "短信提交总数(阶段 4 发送链路)",
|
||||
}, []string{"result"})
|
||||
|
||||
SMSReportTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "sms_report_total",
|
||||
Help: "状态回执处理总数(阶段 5)",
|
||||
}, []string{"status"})
|
||||
|
||||
KafkaProducerMessages = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "kafka_producer_messages_total",
|
||||
Help: "Kafka 生产消息结果(ok/retry/dlq)",
|
||||
}, []string{LabelTopic, LabelKind})
|
||||
|
||||
BalanceReconcileDiff = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "go_sms_balance_reconcile_diff",
|
||||
Help: "R1 对账差额:Redis 预扣余额-流水重算值(覆写前采样;0=一致)",
|
||||
}, []string{"account"})
|
||||
|
||||
BalanceMode = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "go_sms_balance_mode",
|
||||
Help: "预扣模式(1=redis Lua/0=mysql 行锁;GOSMS_BALANCE_MODE 切换)",
|
||||
})
|
||||
|
||||
BalanceFallbackTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_balance_fallback_total",
|
||||
Help: "Redis 预扣基础设施错单次回退 MySQL 行锁次数",
|
||||
})
|
||||
|
||||
BalanceFlowlogPending = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "go_sms_balance_flowlog_pending",
|
||||
Help: "Redis 模式异步流水队列待写条数",
|
||||
})
|
||||
|
||||
KafkaLag = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "go_sms_kafka_lag",
|
||||
Help: "Kafka 消费组 lag 合计(高水位-已提交;背压 L1 告警源,Day53 挂账 Day55 清偿)",
|
||||
}, []string{LabelTopic})
|
||||
|
||||
LongSmsMergedTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_longsms_merged_total",
|
||||
Help: "入向长短信聚合成功数(UDH 组到齐合并单次受理,Day69)",
|
||||
})
|
||||
|
||||
LongSmsDegradeTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "go_sms_longsms_degrade_total",
|
||||
Help: "长短信退化直发片数(reason: overflow=并发组超上限/ref_conflict=协议异常;Day71 起 timeout 废除)",
|
||||
}, []string{LabelKind})
|
||||
|
||||
LongSmsExpireFailTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_longsms_expire_fail_total",
|
||||
Help: "长短信挂起组 72h 到期失败终态数(不齐不发,LP task+UNDELIV 回执,Day71)",
|
||||
})
|
||||
|
||||
LongSmsDupFragTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_longsms_dup_frag_total",
|
||||
Help: "长短信同 idx 重复片幂等覆盖数(客户端重发观测)",
|
||||
})
|
||||
|
||||
ReportRequeueTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_report_requeue_total",
|
||||
Help: "unpaired 回执短重投次数(seg 落库竞态窗口,Day69 三轮)",
|
||||
})
|
||||
|
||||
ReportPendingTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_report_pending_total",
|
||||
Help: "多段回执聚合未到齐挂起事件数(Day69 二轮)",
|
||||
})
|
||||
|
||||
ReportRecheckTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "go_sms_report_recheck_total",
|
||||
Help: "pending 后新事务复查结果(result: finalized=补刀终态/still_pending=真未到齐)",
|
||||
}, []string{LabelKind})
|
||||
|
||||
registry.MustRegister(
|
||||
HTTPRequestsTotal, HTTPRequestDuration, HTTPRequestsInFlight,
|
||||
SMSSubmitTotal, SMSReportTotal, KafkaProducerMessages,
|
||||
BalanceReconcileDiff, BalanceMode, BalanceFallbackTotal, BalanceFlowlogPending,
|
||||
KafkaLag,
|
||||
LongSmsMergedTotal, LongSmsDegradeTotal, LongSmsDupFragTotal,
|
||||
LongSmsExpireFailTotal,
|
||||
ReportPendingTotal, ReportRecheckTotal, ReportRequeueTotal,
|
||||
)
|
||||
// Go 运行时与进程指标
|
||||
registry.MustRegister(
|
||||
collectors.NewGoCollector(),
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
)
|
||||
}
|
||||
|
||||
// Registry 返回指标注册中心(测试或自托管采集用)。
|
||||
func Registry() *prometheus.Registry { return registry }
|
||||
|
||||
// Handler /metrics 输出 handler(app 装配:gin.WrapH(metrics.Handler()))。
|
||||
func Handler() http.Handler {
|
||||
return promhttp.HandlerFor(registry, promhttp.HandlerOpts{EnableOpenMetrics: true})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||
)
|
||||
|
||||
// TestRegistry_ContainsCoreMetrics 验收项:Registry 含核心指标定义。
|
||||
// Vec 类指标需先实例化标签(Handler 场景下由请求埋点产生)再计数。
|
||||
func TestRegistry_ContainsCoreMetrics(t *testing.T) {
|
||||
HTTPRequestsTotal.WithLabelValues("GET", "/t", "200").Inc()
|
||||
HTTPRequestDuration.WithLabelValues("GET", "/t").Observe(0.01)
|
||||
SMSSubmitTotal.WithLabelValues("ok").Inc()
|
||||
SMSReportTotal.WithLabelValues("DELIVRD").Inc()
|
||||
KafkaProducerMessages.WithLabelValues("sms-submit", "ok").Inc()
|
||||
|
||||
for _, name := range []string{
|
||||
"http_requests_total",
|
||||
"http_request_duration_seconds",
|
||||
"http_requests_in_flight",
|
||||
"sms_submit_total",
|
||||
"sms_report_total",
|
||||
"kafka_producer_messages_total",
|
||||
} {
|
||||
if got := testutil.CollectAndCount(Registry(), name); got == 0 {
|
||||
t.Errorf("Registry 缺少指标 %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandler_ExposesMetrics 验收项:Handler 输出含 HELP/TYPE 与指标族名。
|
||||
func TestHandler_ExposesMetrics(t *testing.T) {
|
||||
// 预置数据
|
||||
HTTPRequestsTotal.WithLabelValues("GET", "/health", "200").Inc()
|
||||
HTTPRequestDuration.WithLabelValues("GET", "/health").Observe(0.012)
|
||||
HTTPRequestsInFlight.Set(2)
|
||||
|
||||
srv := httptest.NewServer(Handler())
|
||||
defer srv.Close()
|
||||
|
||||
resp, err := srv.Client().Get(srv.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body := make([]byte, 1<<20)
|
||||
n, _ := resp.Body.Read(body)
|
||||
out := string(body[:n])
|
||||
|
||||
for _, want := range []string{
|
||||
"http_requests_total",
|
||||
"http_request_duration_seconds",
|
||||
"http_request_duration_seconds_bucket",
|
||||
"http_requests_in_flight",
|
||||
`{method="GET",route="/health",status="200"}`,
|
||||
`{method="GET",route="/health"}`,
|
||||
"go_goroutines", // Go 运行时指标
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("/metrics 输出缺少 %q", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// Package notify 通知渠道(Day 27):企微/钉钉群机器人 webhook 推送。
|
||||
//
|
||||
// 语义:Notifier 无状态可并发复用;Send 失败返回错误由调用方决定重试策略
|
||||
// (余额预警服务在冷却期内不写 LastAlertAt,下轮扫描自动重试)。
|
||||
package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Notifier 通知渠道抽象(测试以假实现注入)。
|
||||
type Notifier interface {
|
||||
Send(ctx context.Context, title, content string) error
|
||||
}
|
||||
|
||||
// httpCli 包级复用(短超时,避免告警拖垮调度循环)。
|
||||
var httpCli = &http.Client{Timeout: 5 * time.Second}
|
||||
|
||||
// Webhook 企微/钉钉群机器人(Type: wechat|dingtalk;URL 为机器人地址)。
|
||||
type Webhook struct {
|
||||
Type string
|
||||
URL string
|
||||
}
|
||||
|
||||
// NewWebhook 构造;typ 为空时默认 wechat。
|
||||
func NewWebhook(typ, url string) *Webhook {
|
||||
if typ == "" {
|
||||
typ = "wechat"
|
||||
}
|
||||
return &Webhook{Type: typ, URL: url}
|
||||
}
|
||||
|
||||
// payload 按渠道组装消息体。
|
||||
func (w *Webhook) payload(title, content string) any {
|
||||
text := title + "\n" + content
|
||||
switch strings.ToLower(w.Type) {
|
||||
case "dingtalk":
|
||||
return map[string]any{"msgtype": "text", "text": map[string]string{"content": text}}
|
||||
default: // wechat
|
||||
return map[string]any{"msgtype": "markdown", "markdown": map[string]string{"content": text}}
|
||||
}
|
||||
}
|
||||
|
||||
// Send POST JSON 到机器人地址;非 2xx 或业务 errcode≠0 视为失败。
|
||||
func (w *Webhook) Send(ctx context.Context, title, content string) error {
|
||||
if w.URL == "" {
|
||||
return fmt.Errorf("notify: webhook 地址为空(%s)", w.Type)
|
||||
}
|
||||
body, err := json.Marshal(w.payload(title, content))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.URL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := httpCli.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("notify: webhook 请求失败: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("notify: webhook 状态 %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
// 企微/钉钉均以 errcode 表达业务失败
|
||||
if s := string(raw); strings.Contains(s, `"errcode":0`) || strings.TrimSpace(s) == "" {
|
||||
return nil
|
||||
} else if strings.Contains(s, `"errcode"`) {
|
||||
return fmt.Errorf("notify: webhook 业务失败: %s", s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Multi 聚合多渠道:逐个发送,全部失败才返回错误(部分成功即视为通知已达)。
|
||||
type Multi []Notifier
|
||||
|
||||
// Send 逐渠道发送,聚合错误。
|
||||
func (m Multi) Send(ctx context.Context, title, content string) error {
|
||||
var errs []string
|
||||
for _, n := range m {
|
||||
if err := n.Send(ctx, title, content); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
}
|
||||
if len(errs) == len(m) && len(m) > 0 {
|
||||
return fmt.Errorf("notify: 全部渠道失败: %s", strings.Join(errs, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// webhook_test.go Day 27:企微/钉钉 webhook 消息体与错误分支。
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestWebhook_WeChatPayload 企微:markdown 消息体 + errcode 0 成功。
|
||||
func TestWebhook_WeChatPayload(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
mu.Lock()
|
||||
_ = json.Unmarshal(raw, &body)
|
||||
mu.Unlock()
|
||||
_, _ = w.Write([]byte(`{"errcode":0,"errmsg":"ok"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if err := NewWebhook("wechat", srv.URL).Send(context.Background(), "标题", "内容"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if body["msgtype"] != "markdown" {
|
||||
t.Fatalf("msgtype = %v, 期望 markdown", body["msgtype"])
|
||||
}
|
||||
md := body["markdown"].(map[string]any)
|
||||
if !strings.Contains(md["content"].(string), "标题") {
|
||||
t.Fatalf("内容缺标题: %v", md)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhook_DingTalkPayload 钉钉:text 消息体。
|
||||
func TestWebhook_DingTalkPayload(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
mu.Lock()
|
||||
_ = json.Unmarshal(raw, &body)
|
||||
mu.Unlock()
|
||||
_, _ = w.Write([]byte(`{"errcode":0}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if err := NewWebhook("dingtalk", srv.URL).Send(context.Background(), "T", "C"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if body["msgtype"] != "text" {
|
||||
t.Fatalf("msgtype = %v, 期望 text", body["msgtype"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhook_Errors 空 URL / 业务 errcode≠0 / HTTP 5xx。
|
||||
func TestWebhook_Errors(t *testing.T) {
|
||||
if err := NewWebhook("wechat", "").Send(context.Background(), "t", "c"); err == nil {
|
||||
t.Fatal("空 URL 应失败")
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"errcode":93000,"errmsg":"invalid webhook"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
if err := NewWebhook("wechat", srv.URL).Send(context.Background(), "t", "c"); err == nil {
|
||||
t.Fatal("errcode≠0 应失败")
|
||||
}
|
||||
srv5 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer srv5.Close()
|
||||
if err := NewWebhook("dingtalk", srv5.URL).Send(context.Background(), "t", "c"); err == nil {
|
||||
t.Fatal("5xx 应失败")
|
||||
}
|
||||
}
|
||||
|
||||
// fakeN 测试假通知器。
|
||||
type fakeN struct {
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (f *fakeN) Send(context.Context, string, string) error { f.calls++; return f.err }
|
||||
|
||||
// TestMulti_PartialSuccess 部分成功即成功;全部失败才报错。
|
||||
func TestMulti_PartialSuccess(t *testing.T) {
|
||||
ok := &fakeN{}
|
||||
bad := &fakeN{err: context.Canceled}
|
||||
m1 := Multi{ok, bad}
|
||||
if err := m1.Send(context.Background(), "t", "c"); err != nil {
|
||||
t.Fatalf("部分成功不应报错: %v", err)
|
||||
}
|
||||
if ok.calls != 1 {
|
||||
t.Fatal("ok 渠道应已发送")
|
||||
}
|
||||
bad2 := &fakeN{err: context.DeadlineExceeded}
|
||||
m2 := Multi{bad, bad2}
|
||||
if err := m2.Send(context.Background(), "t", "c"); err == nil {
|
||||
t.Fatal("全部失败应报错")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// 起步 offset 模式(Day58:起步 offset 可配,dev 存量治理配套)。
|
||||
const (
|
||||
OffsetModeOldest = "oldest" // 最早(历史积压回放;生产默认语义)
|
||||
OffsetModeLatest = "latest" // 最新(跳过积压;dev 止血用)
|
||||
)
|
||||
|
||||
// Handler 消息处理函数:返回 nil 视为处理成功并提交 offset;
|
||||
// 返回错误则不提交(at-least-once,重平衡/重启后重新投递)。
|
||||
type Handler func(ctx context.Context, msg *sarama.ConsumerMessage) error
|
||||
|
||||
// ConsumerConfig 消费者组配置。
|
||||
type ConsumerConfig struct {
|
||||
Brokers []string
|
||||
GroupID string
|
||||
Topics []string
|
||||
Handler Handler
|
||||
Logger *zap.Logger
|
||||
// OffsetMode 起步 offset:oldest|latest(大小写不敏感,非法值创建报错)。
|
||||
// 仅对无已提交 offset 的消费组生效(新组/offset 重置后);显式指定时
|
||||
// 优先于 Oldest bool;空则回退 Oldest 语义(历史调用方兼容)。
|
||||
OffsetMode string
|
||||
Oldest bool // true 从最早 offset 开始(新组/集成测试);默认从最新
|
||||
// CommitEveryN 每成功处理 N 条显式提交一次 offset(默认 100)。
|
||||
CommitEveryN int
|
||||
// WorkersPerPartition 每分区并行 worker 数(Day 65;默认 0/1=串行原行为)。
|
||||
// >1 时同分区消息乱序并行处理,offset 按连续前缀有序 Mark——at-least-once
|
||||
// 语义不变(失败不 Mark 由后继越过;退出/重平衡窗口内不 Mark 的在途消息重投)。
|
||||
// 适用:单条处理为 DB 慢事务链(sms-submit Deferred 补全),吞吐=并发÷延迟。
|
||||
WorkersPerPartition int
|
||||
// PaceEvery/PaceSleep 消费 pacing(Day58):每成功处理 PaceEvery 条
|
||||
// sleep PaceSleep(默认 0=不限速)。冷启动 oldest 存量回放时压制消费
|
||||
// 速率,防落库风暴打爆下游(dev MySQL);等待可被会话取消即时打断。
|
||||
PaceEvery int
|
||||
PaceSleep time.Duration
|
||||
}
|
||||
|
||||
// resolveInitialOffset 解析起步 offset:OffsetMode 显式优先(非法值报错,
|
||||
// 防误配 oldest 引发存量回放风暴);空回退 Oldest bool;默认最新。
|
||||
func (c ConsumerConfig) resolveInitialOffset() (int64, error) {
|
||||
if c.OffsetMode != "" {
|
||||
switch strings.ToLower(strings.TrimSpace(c.OffsetMode)) {
|
||||
case OffsetModeOldest:
|
||||
return sarama.OffsetOldest, nil
|
||||
case OffsetModeLatest:
|
||||
return sarama.OffsetNewest, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("queue: offset_mode 非法 %q(合法:oldest|latest)", c.OffsetMode)
|
||||
}
|
||||
}
|
||||
if c.Oldest {
|
||||
return sarama.OffsetOldest, nil
|
||||
}
|
||||
return sarama.OffsetNewest, nil
|
||||
}
|
||||
|
||||
// Consumer 消费者组封装:自动应对重平衡,手动提交 offset。
|
||||
type Consumer struct {
|
||||
group sarama.ConsumerGroup
|
||||
cfg ConsumerConfig
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
// NewConsumer 创建消费者组(连接 broker)。
|
||||
func NewConsumer(cfg ConsumerConfig) (*Consumer, error) {
|
||||
if len(cfg.Brokers) == 0 {
|
||||
return nil, errors.New("queue: kafka.brokers 不能为空")
|
||||
}
|
||||
if cfg.GroupID == "" {
|
||||
return nil, errors.New("queue: group_id 不能为空")
|
||||
}
|
||||
if len(cfg.Topics) == 0 {
|
||||
return nil, errors.New("queue: topics 不能为空")
|
||||
}
|
||||
if cfg.Handler == nil {
|
||||
return nil, errors.New("queue: handler 不能为空")
|
||||
}
|
||||
if cfg.CommitEveryN <= 0 {
|
||||
cfg.CommitEveryN = 100
|
||||
}
|
||||
if cfg.Logger == nil {
|
||||
cfg.Logger = zap.NewNop()
|
||||
}
|
||||
|
||||
initial, err := cfg.resolveInitialOffset()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sc := sarama.NewConfig()
|
||||
sc.Version = sarama.V2_8_0_0
|
||||
sc.Consumer.Offsets.AutoCommit.Enable = false // 手动提交
|
||||
sc.Consumer.Offsets.Initial = initial // 起步 offset(Day58 可配;仅新组/重置后生效)
|
||||
sc.Consumer.Group.Heartbeat.Interval = 3 * time.Second
|
||||
sc.Consumer.Group.Rebalance.Retry.Max = 3 // 重平衡重试
|
||||
sc.Consumer.Group.Rebalance.Timeout = 60 * time.Second
|
||||
|
||||
group, err := sarama.NewConsumerGroup(cfg.Brokers, cfg.GroupID, sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Consumer{group: group, cfg: cfg, log: cfg.Logger}, nil
|
||||
}
|
||||
|
||||
// Run 阻塞消费直至 ctx 取消;正常退出(含 Close 后)返回 nil。
|
||||
func (c *Consumer) Run(ctx context.Context) error {
|
||||
h := &groupHandler{cfg: c.cfg, log: c.log}
|
||||
for {
|
||||
if err := c.group.Consume(ctx, c.cfg.Topics, h); err != nil {
|
||||
if errors.Is(err, sarama.ErrClosedConsumerGroup) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close 关闭消费者组。
|
||||
func (c *Consumer) Close() error { return c.group.Close() }
|
||||
|
||||
// groupHandler sarama.ConsumerGroupHandler 实现。
|
||||
type groupHandler struct {
|
||||
cfg ConsumerConfig
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func (h *groupHandler) Setup(sarama.ConsumerGroupSession) error { return nil }
|
||||
func (h *groupHandler) Cleanup(sarama.ConsumerGroupSession) error { return nil }
|
||||
|
||||
// ConsumeClaim 逐条处理:成功 Mark + 周期 Commit;失败跳过不提交;
|
||||
// 可选 pacing(每 PaceEvery 条成功消息歇 PaceSleep,存量回放防落库风暴)。
|
||||
// WorkersPerPartition>1 时转并行路径(consumeParallel)。
|
||||
func (h *groupHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
if h.cfg.WorkersPerPartition > 1 {
|
||||
return h.consumeParallel(sess, claim)
|
||||
}
|
||||
n := 0
|
||||
paced := 0
|
||||
pace := h.cfg.PaceEvery > 0 && h.cfg.PaceSleep > 0
|
||||
for {
|
||||
select {
|
||||
case <-sess.Context().Done():
|
||||
return nil
|
||||
case msg, ok := <-claim.Messages():
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if err := h.cfg.Handler(sess.Context(), msg); err != nil {
|
||||
// 不 Mark:offset 不前进,重平衡/重启后重新投递(at-least-once)
|
||||
h.log.Warn("kafka_consume_handler_failed",
|
||||
zap.String("topic", msg.Topic), zap.Int32("partition", msg.Partition),
|
||||
zap.Int64("offset", msg.Offset), zap.Error(err))
|
||||
continue
|
||||
}
|
||||
sess.MarkMessage(msg, "")
|
||||
n++
|
||||
if n >= h.cfg.CommitEveryN {
|
||||
sess.Commit() // 手动提交已 Mark 的 offset
|
||||
n = 0
|
||||
}
|
||||
// Day58 pacing:以成功消息计数驱动(失败消息不歇,快速跳过毒丸),
|
||||
// 等待期可被会话取消即时打断(不阻塞优雅退出/重平衡)。
|
||||
if pace {
|
||||
paced++
|
||||
if paced >= h.cfg.PaceEvery {
|
||||
select {
|
||||
case <-time.After(h.cfg.PaceSleep):
|
||||
case <-sess.Context().Done():
|
||||
return nil
|
||||
}
|
||||
paced = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// consumeParallel 每分区 worker 池(Day 65):消息乱序并行处理,提交严格按
|
||||
// offset 连续前缀推进——仅 Mark「自身与前驱均已完成」的消息,at-least-once
|
||||
// 与串行版等价:
|
||||
// - 失败消息不 Mark 但允许前缀越过(重试语义交 ConsumerGroup wrap 层,
|
||||
// 与串行版「失败 continue、后继 Mark 越过」一致);
|
||||
// - 会话结束/通道关闭时窗口内在途消息一律不 Mark,重平衡/重启后重投。
|
||||
//
|
||||
// 背压:tasks 通道容量=workers,打满时取消息阻塞(worker 消化后自续)。
|
||||
func (h *groupHandler) consumeParallel(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
workers := h.cfg.WorkersPerPartition
|
||||
type outcome struct {
|
||||
msg *sarama.ConsumerMessage // nil=在途占位(已取未完成)
|
||||
err error
|
||||
}
|
||||
tasks := make(chan *sarama.ConsumerMessage, workers)
|
||||
// 容量须 ≥ 2×workers+2(在途上限=tasks 缓冲+worker 手中各一):否则主循环
|
||||
// 阻塞投 tasks 时无人消费 done,worker 全阻塞在 done<- 形成死锁环(单测实证)。
|
||||
done := make(chan outcome, workers*2+2)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < workers; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for m := range tasks {
|
||||
out := outcome{msg: m, err: h.cfg.Handler(sess.Context(), m)}
|
||||
select {
|
||||
case done <- out:
|
||||
case <-sess.Context().Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() { wg.Wait(); close(done) }()
|
||||
|
||||
n, paced := 0, 0
|
||||
pace := h.cfg.PaceEvery > 0 && h.cfg.PaceSleep > 0
|
||||
next := int64(-1) // 待推进起始 offset(首条消息锚定)
|
||||
window := map[int64]outcome{} // offset→处理结果(占位/完成)
|
||||
// stop 关闭取数通道并等 worker 全部收尾(结果通道关闭),防 goroutine 泄漏。
|
||||
stop := func() {
|
||||
close(tasks)
|
||||
for range done { /* 丢弃在途结果:会话已结束,Mark 无意义 */
|
||||
}
|
||||
}
|
||||
|
||||
// advance 推进连续完成前缀:成功 Mark+周期 Commit+可选 pacing;失败仅记日志越过。
|
||||
// 返回 false=会话取消(pacing 等待被打断),调用方须立即退出。
|
||||
advance := func() bool {
|
||||
for {
|
||||
v, exists := window[next]
|
||||
if !exists || v.msg == nil {
|
||||
return true
|
||||
}
|
||||
if v.err == nil {
|
||||
sess.MarkMessage(v.msg, "")
|
||||
n++
|
||||
if n >= h.cfg.CommitEveryN {
|
||||
sess.Commit()
|
||||
n = 0
|
||||
}
|
||||
if pace {
|
||||
paced++
|
||||
if paced >= h.cfg.PaceEvery {
|
||||
select {
|
||||
case <-time.After(h.cfg.PaceSleep):
|
||||
case <-sess.Context().Done():
|
||||
return false
|
||||
}
|
||||
paced = 0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 不 Mark:offset 不前进,重平衡/重启后重新投递(at-least-once)
|
||||
h.log.Warn("kafka_consume_handler_failed",
|
||||
zap.String("topic", v.msg.Topic), zap.Int32("partition", v.msg.Partition),
|
||||
zap.Int64("offset", v.msg.Offset), zap.Error(v.err))
|
||||
}
|
||||
delete(window, next)
|
||||
next++
|
||||
}
|
||||
}
|
||||
|
||||
// pending pattern(Day65 死锁修复):投 tasks 阻塞期间仍须消费 done——若投递
|
||||
// 用嵌套 select 独占等待,done 满后 worker 阻塞发送、不再取 tasks,与主循环
|
||||
// 互锁(生产实证:16 分区 worker 卡 done<-、主循环卡 tasks<-,进程全静默)。
|
||||
var pending *sarama.ConsumerMessage
|
||||
for {
|
||||
if pending == nil {
|
||||
select {
|
||||
case <-sess.Context().Done():
|
||||
stop()
|
||||
return nil
|
||||
case msg, ok := <-claim.Messages():
|
||||
if !ok {
|
||||
stop()
|
||||
return nil
|
||||
}
|
||||
if next < 0 {
|
||||
next = msg.Offset
|
||||
}
|
||||
window[msg.Offset] = outcome{} // 在途占位:阻挡前缀越过未完成消息
|
||||
pending = msg
|
||||
case r, ok := <-done:
|
||||
if !ok {
|
||||
return nil // 仅 stop 后到达:worker 已全部收尾
|
||||
}
|
||||
window[r.msg.Offset] = r
|
||||
if !advance() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case <-sess.Context().Done():
|
||||
stop()
|
||||
return nil
|
||||
case tasks <- pending:
|
||||
pending = nil
|
||||
case r, ok := <-done:
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
window[r.msg.Offset] = r
|
||||
if !advance() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go-sms/internal/pkg/metrics"
|
||||
)
|
||||
|
||||
// DefaultMaxConsumeRetry 消费失败重试上限(含首次,超过转死信)。
|
||||
const DefaultMaxConsumeRetry = 3
|
||||
|
||||
// DLQSender 死信转投接口(生产为 *Producer;测试可注入 mock)。
|
||||
type DLQSender interface {
|
||||
Send(topic string, key, value []byte) error
|
||||
}
|
||||
|
||||
// ConsumerGroupConfig 消费组配置(包装 Consumer/Producer 配置)。
|
||||
type ConsumerGroupConfig struct {
|
||||
Consumer ConsumerConfig
|
||||
Producer ProducerConfig // 死信转投用(可为 nil:不转投仅丢弃计数)
|
||||
MaxRetry int // 消费失败次数上限(默认 3,含首次)
|
||||
}
|
||||
|
||||
// ConsumerGroup 优雅消费循环(Day 16):
|
||||
// - 暂停/恢复:Pause 期间阻塞分区消息处理(心跳与 offset 不受影响);
|
||||
// - 失败重试:Handler 返回错误不提交 offset(重平衡/重启后重投);
|
||||
// - 死信转投:同一消息失败达 MaxRetry 次后转投 <topic>.dlq 并视为处理完成,
|
||||
// 避免毒丸消息无限阻塞分区(重试计数为进程内存态,重启归零属预期)。
|
||||
type ConsumerGroup struct {
|
||||
cons *Consumer
|
||||
dlq DLQSender
|
||||
cfg ConsumerGroupConfig
|
||||
log *zap.Logger
|
||||
maxRetry int
|
||||
|
||||
mu sync.RWMutex
|
||||
gate chan struct{} // 关闭=恢复(可消费);新建(未关闭)=暂停
|
||||
paused bool
|
||||
|
||||
running atomic.Bool
|
||||
}
|
||||
|
||||
// NewConsumerGroup 创建消费组(消费 + 死信转投一体)。
|
||||
// MaxRetry<=0 取默认 3(Day48 修复:零值曾致重试循环不执行,死信分支
|
||||
// 以 nil err 调 deadLetterValue panic——消费存量毒丸消息即触发)。
|
||||
func NewConsumerGroup(cfg ConsumerGroupConfig) (*ConsumerGroup, error) {
|
||||
if cfg.MaxRetry <= 0 {
|
||||
cfg.MaxRetry = DefaultMaxConsumeRetry
|
||||
}
|
||||
cons, err := NewConsumer(cfg.Consumer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g := &ConsumerGroup{
|
||||
cons: cons, cfg: cfg, log: cons.log,
|
||||
maxRetry: cfg.MaxRetry,
|
||||
gate: make(chan struct{}),
|
||||
}
|
||||
close(g.gate) // 初始为恢复态
|
||||
if cfg.Producer.Brokers != nil {
|
||||
p, err := NewProducer(cfg.Producer)
|
||||
if err != nil {
|
||||
_ = cons.Close()
|
||||
return nil, err
|
||||
}
|
||||
g.dlq = p
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// Run 阻塞消费直至 ctx 取消;handler 由 cfg.Consumer.Handler 提供,
|
||||
// 内部经失败重试与死信包装。
|
||||
func (g *ConsumerGroup) Run(ctx context.Context) error {
|
||||
g.running.Store(true)
|
||||
defer g.running.Store(false)
|
||||
wrapped := g.wrap(g.cfg.Consumer.Handler)
|
||||
inner := g.cons.cfg
|
||||
inner.Handler = wrapped
|
||||
g.cons.cfg = inner
|
||||
return g.cons.Run(ctx)
|
||||
}
|
||||
|
||||
// Pause 暂停消费:阻塞各分区的消息处理(已建立连接与心跳保持)。
|
||||
func (g *ConsumerGroup) Pause() {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if !g.paused {
|
||||
g.paused = true
|
||||
g.gate = make(chan struct{})
|
||||
g.log.Info("consumer_group_paused")
|
||||
}
|
||||
}
|
||||
|
||||
// Resume 恢复消费。
|
||||
func (g *ConsumerGroup) Resume() {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if g.paused {
|
||||
g.paused = false
|
||||
close(g.gate)
|
||||
g.log.Info("consumer_group_resumed")
|
||||
}
|
||||
}
|
||||
|
||||
// Paused 当前是否暂停。
|
||||
func (g *ConsumerGroup) Paused() bool {
|
||||
g.mu.RLock()
|
||||
defer g.mu.RUnlock()
|
||||
return g.paused
|
||||
}
|
||||
|
||||
// Close 关闭消费者与死信生产者。
|
||||
func (g *ConsumerGroup) Close() error {
|
||||
if p, ok := g.dlq.(*Producer); ok && p != nil {
|
||||
defer func() { _ = p.Close() }()
|
||||
}
|
||||
return g.cons.Close()
|
||||
}
|
||||
|
||||
// waitGate 等待恢复信号;暂停期间阻塞(ctx 取消时返回错误以释放分区)。
|
||||
func (g *ConsumerGroup) waitGate(ctx context.Context) error {
|
||||
g.mu.RLock()
|
||||
gate := g.gate
|
||||
g.mu.RUnlock()
|
||||
select {
|
||||
case <-gate:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// wrap 包装业务 handler:暂停门 → 同步重试(指数退避)→ 死信转投。
|
||||
func (g *ConsumerGroup) wrap(h Handler) Handler {
|
||||
return func(ctx context.Context, msg *sarama.ConsumerMessage) error {
|
||||
if err := g.waitGate(ctx); err != nil {
|
||||
return err // 会话取消,交由上层退出
|
||||
}
|
||||
var err error
|
||||
for attempt := 1; attempt <= g.maxRetry; attempt++ {
|
||||
if attempt > 1 { // 退避:100ms * 2^(n-1),上限 1s
|
||||
backoff := 100 * time.Millisecond << uint(attempt-2)
|
||||
if backoff > time.Second {
|
||||
backoff = time.Second
|
||||
}
|
||||
select {
|
||||
case <-time.After(backoff):
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
if err = h(ctx, msg); err == nil {
|
||||
return nil
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return err // 会话结束导致的失败不转死信
|
||||
}
|
||||
g.log.Warn("consume_attempt_failed",
|
||||
zap.String("topic", msg.Topic), zap.Int32("partition", msg.Partition),
|
||||
zap.Int64("offset", msg.Offset), zap.Int("attempt", attempt),
|
||||
zap.Int("max", g.maxRetry), zap.Error(err))
|
||||
}
|
||||
|
||||
// 重试耗尽:转投死信并视为完成(防毒丸阻塞分区)
|
||||
dlqTopic := DLQTopic(msg.Topic)
|
||||
g.log.Error("consume_dead_letter",
|
||||
zap.String("topic", msg.Topic), zap.String("dlq_topic", dlqTopic),
|
||||
zap.Int64("offset", msg.Offset), zap.Int("retries", g.maxRetry), zap.Error(err))
|
||||
if g.dlq != nil {
|
||||
_ = g.dlq.Send(dlqTopic, msg.Key, deadLetterValue(msg, err, g.maxRetry))
|
||||
metrics.KafkaProducerMessages.WithLabelValues(dlqTopic, "dlq").Inc()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// deadLetterValue 死信载荷:原值 + 元信息头(简单前缀方案,阶段 5 解码消费)。
|
||||
func deadLetterValue(m *sarama.ConsumerMessage, cause error, retries int) []byte {
|
||||
meta := "dlq;retries=" + strconv.Itoa(retries) + ";cause=" + cause.Error() + ";payload="
|
||||
return append([]byte(meta), m.Value...)
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// mockDLQ 记录死信转投。
|
||||
type mockDLQ struct {
|
||||
mu sync.Mutex
|
||||
sent []string // topic:key
|
||||
}
|
||||
|
||||
func (m *mockDLQ) Send(topic string, key, value []byte) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.sent = append(m.sent, topic+":"+string(key))
|
||||
return nil
|
||||
}
|
||||
|
||||
// newTestGroup 直接构造(绕过 broker 连接;wrap/gate 不依赖 Consumer 连接)。
|
||||
func newTestGroup(maxRetry int, dlq DLQSender) *ConsumerGroup {
|
||||
g := &ConsumerGroup{log: zap.NewNop(), dlq: dlq, maxRetry: maxRetry, gate: make(chan struct{})}
|
||||
close(g.gate)
|
||||
return g
|
||||
}
|
||||
|
||||
// TestWrap_RetryThenDeadLetter 验收:失败达上限转投 <topic>.dlq。
|
||||
func TestWrap_RetryThenDeadLetter(t *testing.T) {
|
||||
dlq := &mockDLQ{}
|
||||
g := newTestGroup(3, dlq)
|
||||
msg := &sarama.ConsumerMessage{Topic: "t-dlq", Partition: 0, Offset: 7, Key: []byte("poison"), Value: []byte("payload-1")}
|
||||
|
||||
boom := errors.New("biz fail")
|
||||
calls := 0
|
||||
// 恒败 handler:一次 wrap 内同步重试 3 次,耗尽后转死信并返回 nil
|
||||
err := g.wrap(func(context.Context, *sarama.ConsumerMessage) error {
|
||||
calls++
|
||||
return boom
|
||||
})(context.Background(), msg)
|
||||
if err != nil {
|
||||
t.Fatalf("重试耗尽应转死信返回 nil, 实际: %v", err)
|
||||
}
|
||||
if calls != 3 {
|
||||
t.Errorf("handler 调用次数 = %d, 期望 3", calls)
|
||||
}
|
||||
dlq.mu.Lock()
|
||||
defer dlq.mu.Unlock()
|
||||
if len(dlq.sent) != 1 || dlq.sent[0] != "t-dlq.dlq:poison" {
|
||||
t.Fatalf("死信记录 = %v, 期望 [t-dlq.dlq:poison]", dlq.sent)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWrap_SecondAttemptSucceeds 第二次尝试成功即不转投(瞬时故障自愈)。
|
||||
func TestWrap_SecondAttemptSucceeds(t *testing.T) {
|
||||
dlq := &mockDLQ{}
|
||||
g := newTestGroup(3, dlq)
|
||||
msg := &sarama.ConsumerMessage{Topic: "t2", Partition: 0, Offset: 1}
|
||||
calls := 0
|
||||
if err := g.wrap(func(context.Context, *sarama.ConsumerMessage) error {
|
||||
calls++
|
||||
if calls < 2 {
|
||||
return errors.New("transient")
|
||||
}
|
||||
return nil
|
||||
})(context.Background(), msg); err != nil {
|
||||
t.Fatalf("瞬时失败应重试成功: %v", err)
|
||||
}
|
||||
if calls != 2 || len(dlq.sent) != 0 {
|
||||
t.Errorf("calls=%d dlq=%d, 期望 2 次成功且无死信", calls, len(dlq.sent))
|
||||
}
|
||||
}
|
||||
|
||||
// TestPauseResumeGate 暂停/恢复门语义。
|
||||
func TestPauseResumeGate(t *testing.T) {
|
||||
g := newTestGroup(3, nil)
|
||||
ctx := context.Background()
|
||||
|
||||
if g.Paused() {
|
||||
t.Fatal("初始应为恢复态")
|
||||
}
|
||||
if err := g.waitGate(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
g.Pause()
|
||||
if !g.Paused() {
|
||||
t.Fatal("Pause 后应为暂停态")
|
||||
}
|
||||
// 暂停中 waitGate 阻塞,ctx 取消可释放
|
||||
cctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := g.waitGate(cctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("暂停中应阻塞至 ctx 超时, 实际: %v", err)
|
||||
}
|
||||
|
||||
g.Resume()
|
||||
if g.Paused() {
|
||||
t.Fatal("Resume 后应为恢复态")
|
||||
}
|
||||
if err := g.waitGate(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 幂等:重复 Pause/Resume 不 panic
|
||||
g.Resume()
|
||||
g.Pause()
|
||||
g.Pause()
|
||||
g.Resume()
|
||||
}
|
||||
|
||||
// TestDeadLetterValue 死信载荷含元信息与原值。
|
||||
func TestDeadLetterValue(t *testing.T) {
|
||||
m := &sarama.ConsumerMessage{Value: []byte("BODY")}
|
||||
v := deadLetterValue(m, errors.New("x"), 3)
|
||||
if string(v[:3]) != "dlq" || string(v[len(v)-4:]) != "BODY" {
|
||||
t.Fatalf("死信载荷 = %q", v)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// paceOK 恒成功 handler(Day58 pacing 测试)。
|
||||
func paceOK(context.Context, *sarama.ConsumerMessage) error { return nil }
|
||||
|
||||
// TestResolveInitialOffset Day58:起步 offset 解析(显式优先/非法报错/bool 回退)。
|
||||
func TestResolveInitialOffset(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg ConsumerConfig
|
||||
want int64
|
||||
wantErr bool
|
||||
}{
|
||||
{"空且Oldest=false默认最新", ConsumerConfig{}, sarama.OffsetNewest, false},
|
||||
{"Oldest bool 回退", ConsumerConfig{Oldest: true}, sarama.OffsetOldest, false},
|
||||
{"显式 oldest 覆盖 bool", ConsumerConfig{OffsetMode: "oldest", Oldest: false}, sarama.OffsetOldest, false},
|
||||
{"显式 LATEST 大小写不敏感覆盖 bool", ConsumerConfig{OffsetMode: " LATEST ", Oldest: true}, sarama.OffsetNewest, false},
|
||||
{"非法值报错", ConsumerConfig{OffsetMode: "mid"}, 0, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got, err := tc.cfg.resolveInitialOffset()
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("[%s] 非法 offset_mode 应报错", tc.name)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("[%s] 意外报错: %v", tc.name, err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Fatalf("[%s] initial = %d, 期望 %d", tc.name, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeClaim_Pacing Day58:pacing 生效——10 条/PaceEvery 2/歇 60ms
|
||||
// = 5 次歇 ≥300ms,且全部 Mark(成功计数驱动)。
|
||||
func TestConsumeClaim_Pacing(t *testing.T) {
|
||||
msgs := make([]*sarama.ConsumerMessage, 10)
|
||||
for i := range msgs {
|
||||
msgs[i] = &sarama.ConsumerMessage{Topic: "it", Value: []byte("ok")}
|
||||
}
|
||||
start := time.Now()
|
||||
sess := runHandler(t, ConsumerConfig{
|
||||
Handler: paceOK, CommitEveryN: 100,
|
||||
PaceEvery: 2, PaceSleep: 60 * time.Millisecond,
|
||||
}, msgs)
|
||||
if elapsed := time.Since(start); elapsed < 260*time.Millisecond { // 5×60ms,留调度余量
|
||||
t.Fatalf("pacing 未生效: 10 条耗时 %v < 260ms(期望 ≥5 次歇 ×60ms)", elapsed)
|
||||
}
|
||||
if len(sess.marked) != 10 {
|
||||
t.Fatalf("Mark 数 = %d, 期望 10", len(sess.marked))
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeClaim_PaceInterruptedByCtx pacing 等待可被会话取消即时打断
|
||||
// (不阻塞优雅退出/重平衡)。
|
||||
func TestConsumeClaim_PaceInterruptedByCtx(t *testing.T) {
|
||||
claim := &mockClaim{topic: "it", msgs: make(chan *sarama.ConsumerMessage, 2)}
|
||||
claim.msgs <- &sarama.ConsumerMessage{Topic: "it", Value: []byte("a")}
|
||||
claim.msgs <- &sarama.ConsumerMessage{Topic: "it", Value: []byte("b")}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
sess := &mockSession{ctx: ctx}
|
||||
h := &groupHandler{
|
||||
cfg: ConsumerConfig{Handler: paceOK, CommitEveryN: 100, PaceEvery: 1, PaceSleep: 5 * time.Second},
|
||||
log: zap.NewNop(),
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); _ = h.ConsumeClaim(sess, claim) }()
|
||||
time.Sleep(150 * time.Millisecond) // 首条成功后进入 5s 歇
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("pacing 等待未被 ctx 取消打断(>1s 仍阻塞)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeClaim_NoPaceByDefault 默认(未配 pacing)全速消费无歇。
|
||||
func TestConsumeClaim_NoPaceByDefault(t *testing.T) {
|
||||
msgs := make([]*sarama.ConsumerMessage, 50)
|
||||
for i := range msgs {
|
||||
msgs[i] = &sarama.ConsumerMessage{Topic: "it", Value: []byte("ok")}
|
||||
}
|
||||
start := time.Now()
|
||||
sess := runHandler(t, ConsumerConfig{Handler: paceOK, CommitEveryN: 100}, msgs)
|
||||
if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
|
||||
t.Fatalf("未配 pacing 却耗时 %v(应全速)", elapsed)
|
||||
}
|
||||
if len(sess.marked) != 50 {
|
||||
t.Fatalf("Mark 数 = %d, 期望 50", len(sess.marked))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// parallelSession 嵌入接口仅覆写 consumeParallel 依赖的三个方法(未用方法 nil panic 即测试失败)。
|
||||
type parallelSession struct {
|
||||
sarama.ConsumerGroupSession
|
||||
ctx context.Context
|
||||
mu sync.Mutex
|
||||
marked []int64
|
||||
commits int
|
||||
}
|
||||
|
||||
func (s *parallelSession) MarkMessage(msg *sarama.ConsumerMessage, _ string) {
|
||||
s.mu.Lock()
|
||||
s.marked = append(s.marked, msg.Offset)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
func (s *parallelSession) Commit() {
|
||||
s.mu.Lock()
|
||||
s.commits++
|
||||
s.mu.Unlock()
|
||||
}
|
||||
func (s *parallelSession) Context() context.Context { return s.ctx }
|
||||
func (s *parallelSession) markedSnapshot() []int64 {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return append([]int64(nil), s.marked...)
|
||||
}
|
||||
|
||||
// parallelClaim 单分区消息通道。
|
||||
type parallelClaim struct{ ch chan *sarama.ConsumerMessage }
|
||||
|
||||
func (c *parallelClaim) Topic() string { return "t-parallel" }
|
||||
func (c *parallelClaim) Partition() int32 { return 0 }
|
||||
func (c *parallelClaim) InitialOffset() int64 { return 0 }
|
||||
func (c *parallelClaim) HighWaterMarkOffset() int64 { return 0 }
|
||||
func (c *parallelClaim) Messages() <-chan *sarama.ConsumerMessage { return c.ch }
|
||||
|
||||
// runParallel 驱动 consumeParallel:投消息→等 handler 全部完成且 Mark 达期望→
|
||||
// 关闭消息通道触发退出(通道关闭=会话结束路径,与生产语义一致)。
|
||||
func runParallel(t *testing.T, workers, n int, h Handler, wantMarked int) *parallelSession {
|
||||
t.Helper()
|
||||
msgs := make([]*sarama.ConsumerMessage, n)
|
||||
for i := range msgs {
|
||||
msgs[i] = &sarama.ConsumerMessage{Topic: "t-parallel", Partition: 0, Offset: int64(i), Value: []byte("v")}
|
||||
}
|
||||
sess := ¶llelSession{ctx: context.Background()}
|
||||
claim := ¶llelClaim{ch: make(chan *sarama.ConsumerMessage, n)}
|
||||
gh := &groupHandler{
|
||||
cfg: ConsumerConfig{Handler: h, WorkersPerPartition: workers, CommitEveryN: 5},
|
||||
log: zap.NewNop(),
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() { _ = gh.consumeParallel(sess, claim); close(done) }()
|
||||
for _, m := range msgs {
|
||||
claim.ch <- m
|
||||
}
|
||||
// 等 Mark 追平期望(处理完成与 Mark 之间有主循环异步窗口,轮询避免竞态)。
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if len(sess.markedSnapshot()) >= wantMarked {
|
||||
break
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
close(claim.ch)
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("consumeParallel 未在超时内退出")
|
||||
}
|
||||
return sess
|
||||
}
|
||||
|
||||
// TestConsumeParallel_OrderedMark Day65 核心不变量:多 worker 乱序处理,
|
||||
// MarkMessage 严格按 offset 升序(仅连续完成前缀),全部消息各处理一次。
|
||||
func TestConsumeParallel_OrderedMark(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(64))
|
||||
delays := make([]time.Duration, 30)
|
||||
for i := range delays {
|
||||
delays[i] = time.Duration(rng.Intn(8)) * time.Millisecond // 预生成(*Rand 非并发安全)
|
||||
}
|
||||
var mu sync.Mutex
|
||||
seen := map[int64]int{}
|
||||
h := func(_ context.Context, m *sarama.ConsumerMessage) error {
|
||||
time.Sleep(delays[m.Offset]) // 乱序完成
|
||||
mu.Lock()
|
||||
seen[m.Offset]++
|
||||
mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
sess := runParallel(t, 4, 30, h, 30)
|
||||
|
||||
got := sess.markedSnapshot()
|
||||
if len(got) != 30 {
|
||||
t.Fatalf("Mark 数 = %d, 期望 30(got=%v)", len(got), got)
|
||||
}
|
||||
for i, off := range got {
|
||||
if off != int64(i) {
|
||||
t.Fatalf("Mark 序列须严格升序连续:第 %d 个 = offset %d(序列 %v)", i, off, got)
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(seen) != 30 {
|
||||
t.Fatalf("处理消息数 = %d, 期望 30", len(seen))
|
||||
}
|
||||
for off, c := range seen {
|
||||
if c != 1 {
|
||||
t.Fatalf("offset %d 处理 %d 次, 期望 1(至少一次语义,不应重复)", off, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeParallel_FailureNotMarkedButAdvances 失败消息不 Mark 但窗口越过
|
||||
// (与串行版「失败 continue、后继 Mark 越过」等价),其余按序 Mark。
|
||||
func TestConsumeParallel_FailureNotMarkedButAdvances(t *testing.T) {
|
||||
h := func(_ context.Context, m *sarama.ConsumerMessage) error {
|
||||
if m.Offset == 3 || m.Offset == 7 {
|
||||
return errors.New("biz fail")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
sess := runParallel(t, 3, 10, h, 8)
|
||||
|
||||
got := sess.markedSnapshot()
|
||||
want := []int64{0, 1, 2, 4, 5, 6, 8, 9}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("Mark 数 = %d, 期望 %d(got=%v)", len(got), len(want), got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("Mark 序列 = %v, 期望 %v(失败 offset 3/7 跳过且不阻塞)", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeParallel_AllFail 全部失败:零 Mark、正常退出不卡死(毒丸不阻塞分区)。
|
||||
func TestConsumeParallel_AllFail(t *testing.T) {
|
||||
h := func(_ context.Context, _ *sarama.ConsumerMessage) error { return errors.New("boom") }
|
||||
sess := runParallel(t, 2, 6, h, 0)
|
||||
if got := sess.markedSnapshot(); len(got) != 0 {
|
||||
t.Fatalf("全败应零 Mark, 实际 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeParallel_NoDeadlockUnderLoad Day65 死锁回归:大量消息+快速 handler,
|
||||
// done 结果积压期间主循环仍须持续消费(修复前:投 tasks 嵌套 select 独占等待,
|
||||
// done 满后与 worker 互锁,生产实证进程全静默)。超时即死锁。
|
||||
func TestConsumeParallel_NoDeadlockUnderLoad(t *testing.T) {
|
||||
const n = 500
|
||||
h := func(_ context.Context, m *sarama.ConsumerMessage) error {
|
||||
if m.Offset%97 == 0 {
|
||||
return errors.New("flaky")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
sess := runParallel(t, 4, n, h, n-6) // 500 条中 6 条(offset 0,97,194,291,388,486?)失败
|
||||
got := sess.markedSnapshot()
|
||||
if len(got) == 0 {
|
||||
t.Fatal("零 Mark:消费未推进(疑似死锁)")
|
||||
}
|
||||
for i := 1; i < len(got); i++ {
|
||||
if got[i] <= got[i-1] {
|
||||
t.Fatalf("Mark 序列须严格升序: %v", got[:i+1])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// mockSession ConsumerGroupSession 的轻量 mock。
|
||||
type mockSession struct {
|
||||
ctx context.Context
|
||||
marked []*sarama.ConsumerMessage
|
||||
commits int
|
||||
}
|
||||
|
||||
func (s *mockSession) Claims() map[string][]int32 { return nil }
|
||||
func (s *mockSession) MemberID() string { return "mock" }
|
||||
func (s *mockSession) GenerationID() int32 { return 1 }
|
||||
func (s *mockSession) MarkOffset(string, int32, int64, string) {}
|
||||
func (s *mockSession) ResetOffset(string, int32, int64, string) {}
|
||||
func (s *mockSession) MarkMessage(m *sarama.ConsumerMessage, _ string) {
|
||||
s.marked = append(s.marked, m)
|
||||
}
|
||||
func (s *mockSession) Context() context.Context { return s.ctx }
|
||||
func (s *mockSession) Commit() { s.commits++ }
|
||||
|
||||
// mockClaim ConsumerGroupClaim 的轻量 mock。
|
||||
type mockClaim struct {
|
||||
topic string
|
||||
partition int32
|
||||
msgs chan *sarama.ConsumerMessage
|
||||
}
|
||||
|
||||
func (c *mockClaim) Topic() string { return c.topic }
|
||||
func (c *mockClaim) Partition() int32 { return c.partition }
|
||||
func (c *mockClaim) InitialOffset() int64 { return 0 }
|
||||
func (c *mockClaim) HighWaterMarkOffset() int64 { return 0 }
|
||||
func (c *mockClaim) Messages() <-chan *sarama.ConsumerMessage { return c.msgs }
|
||||
|
||||
// runHandler 在 mock session/claim 上执行 ConsumeClaim 并等待结束。
|
||||
func runHandler(t *testing.T, cfg ConsumerConfig, msgs []*sarama.ConsumerMessage) *mockSession {
|
||||
t.Helper()
|
||||
sess := &mockSession{ctx: context.Background()}
|
||||
claim := &mockClaim{topic: "it", partition: 0, msgs: make(chan *sarama.ConsumerMessage, len(msgs))}
|
||||
for _, m := range msgs {
|
||||
claim.msgs <- m
|
||||
}
|
||||
close(claim.msgs) // 关闭后 ConsumeClaim 自然返回
|
||||
|
||||
h := &groupHandler{cfg: cfg, log: zap.NewNop()}
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); _ = h.ConsumeClaim(sess, claim) }()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("ConsumeClaim 未退出")
|
||||
}
|
||||
return sess
|
||||
}
|
||||
|
||||
// TestConsumeClaim_MarkAndCommit 验证:成功才 Mark,按 N 条提交。
|
||||
func TestConsumeClaim_MarkAndCommit(t *testing.T) {
|
||||
failOn := "bad"
|
||||
cfg := ConsumerConfig{
|
||||
Handler: func(_ context.Context, m *sarama.ConsumerMessage) error {
|
||||
if string(m.Value) == failOn {
|
||||
return errors.New("biz error")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
CommitEveryN: 2,
|
||||
}
|
||||
msgs := []*sarama.ConsumerMessage{
|
||||
{Topic: "it", Value: []byte("ok1")},
|
||||
{Topic: "it", Value: []byte(failOn)}, // 失败:不 Mark 不计入提交
|
||||
{Topic: "it", Value: []byte("ok2")}, // 凑满 2 条 → 第 1 次提交
|
||||
{Topic: "it", Value: []byte("ok3")},
|
||||
{Topic: "it", Value: []byte("ok4")}, // 凑满 2 条 → 第 2 次提交
|
||||
}
|
||||
sess := runHandler(t, cfg, msgs)
|
||||
|
||||
if len(sess.marked) != 4 {
|
||||
t.Errorf("Mark 数 = %d, 期望 4(失败消息不应 Mark)", len(sess.marked))
|
||||
}
|
||||
if sess.commits != 2 {
|
||||
t.Errorf("Commit 次数 = %d, 期望 2", sess.commits)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumeClaim_AllFail 验证全部失败时零提交(at-least-once 重投)。
|
||||
func TestConsumeClaim_AllFail(t *testing.T) {
|
||||
cfg := ConsumerConfig{
|
||||
Handler: func(_ context.Context, _ *sarama.ConsumerMessage) error {
|
||||
return errors.New("always")
|
||||
},
|
||||
CommitEveryN: 1,
|
||||
}
|
||||
sess := runHandler(t, cfg, []*sarama.ConsumerMessage{
|
||||
{Topic: "it", Value: []byte("a")},
|
||||
{Topic: "it", Value: []byte("b")},
|
||||
})
|
||||
if len(sess.marked) != 0 || sess.commits != 0 {
|
||||
t.Errorf("全部失败不应有 Mark/Commit, 实际 %d/%d", len(sess.marked), sess.commits)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewConsumer_ConfigValidation 验证配置校验。
|
||||
func TestNewConsumer_ConfigValidation(t *testing.T) {
|
||||
h := func(context.Context, *sarama.ConsumerMessage) error { return nil }
|
||||
cases := []ConsumerConfig{
|
||||
{GroupID: "g", Topics: []string{"t"}, Handler: h}, // 无 brokers
|
||||
{Brokers: []string{"b"}, Topics: []string{"t"}, Handler: h}, // 无 group
|
||||
{Brokers: []string{"b"}, GroupID: "g", Handler: h}, // 无 topics
|
||||
{Brokers: []string{"b"}, GroupID: "g", Topics: []string{"t"}}, // 无 handler
|
||||
}
|
||||
for i, cfg := range cases {
|
||||
if _, err := NewConsumer(cfg); err == nil {
|
||||
t.Errorf("用例 %d 应返回配置错误", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package queue Kafka 生产者/消费者封装(Sarama,重试+死信)。Day 7 实现。
|
||||
package queue
|
||||
@@ -0,0 +1,147 @@
|
||||
// lag.go Day 48:Kafka 消费组 lag 读取(背压水位采样源)。
|
||||
// Σ(topic 各分区 高水位 − 已提交 offset);组未提交过的分区按 0 计
|
||||
// (新组起步口径,与 Kafka Consumer Group 命令行 lag 一致)。
|
||||
// Day 56 根治(Day55 挂账清偿):提交 offset 改 OffsetFetchRequest(V1)
|
||||
// 直连 coordinator——sarama OffsetManager.NextOffset 对 coordinator fetch
|
||||
// 失败静默返回 -1(与「组从未提交」不可区分,压测期 gauge 冻结假值实证);
|
||||
// 协议级 Err 字段可鉴别真错误并上抛,gauge 假冻结场景消除。
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// LagReader lag 采样器(轻量 client+coordinator 直连 offset fetch,只读)。
|
||||
type LagReader struct {
|
||||
client sarama.Client
|
||||
group string
|
||||
log *zap.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
coord *sarama.Broker // 缓存的 coordinator 连接(失败即弃,下轮重发现)
|
||||
}
|
||||
|
||||
// NewLagReader 构造(brokers/group)。
|
||||
func NewLagReader(brokers []string, group string, log *zap.Logger) (*LagReader, error) {
|
||||
cfg := sarama.NewConfig()
|
||||
cfg.Net.DialTimeout = 3 * time.Second
|
||||
cfg.Net.ReadTimeout = 3 * time.Second
|
||||
cfg.Version = sarama.V2_1_0_0 // OffsetFetch V1 需 ≥0.8.2+;2.1 与消费组一致
|
||||
client, err := sarama.NewClient(brokers, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if log == nil {
|
||||
log = zap.NewNop()
|
||||
}
|
||||
return &LagReader{client: client, group: group, log: log}, nil
|
||||
}
|
||||
|
||||
// Close 释放连接。
|
||||
func (r *LagReader) Close() error {
|
||||
r.mu.Lock()
|
||||
if r.coord != nil {
|
||||
_ = r.coord.Close()
|
||||
r.coord = nil
|
||||
}
|
||||
r.mu.Unlock()
|
||||
return r.client.Close()
|
||||
}
|
||||
|
||||
// coordinator 取组协调者(缓存复用;nil 时发现并连接)。
|
||||
// 注:client.Coordinator 返回的 broker 由 client 连接池管理(registerBroker
|
||||
// 已 Open);二次 Open 报 ErrAlreadyConnected 属正常——视为已连接。
|
||||
func (r *LagReader) coordinator() (*sarama.Broker, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.coord != nil {
|
||||
if ok, _ := r.coord.Connected(); ok {
|
||||
return r.coord, nil
|
||||
}
|
||||
_ = r.coord.Close()
|
||||
r.coord = nil
|
||||
}
|
||||
br, err := r.client.Coordinator(r.group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("coordinator 发现失败: %w", err)
|
||||
}
|
||||
if err := br.Open(r.client.Config()); err != nil && !errors.Is(err, sarama.ErrAlreadyConnected) {
|
||||
return nil, fmt.Errorf("coordinator 连接失败: %w", err)
|
||||
}
|
||||
r.coord = br
|
||||
return br, nil
|
||||
}
|
||||
|
||||
// dropCoordinator 连接异常时弃置(下轮重新发现)。
|
||||
func (r *LagReader) dropCoordinator() {
|
||||
r.mu.Lock()
|
||||
if r.coord != nil {
|
||||
_ = r.coord.Close()
|
||||
r.coord = nil
|
||||
}
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
// Total 全 topic lag 合计(topics 为关心的消费域,如 [sms-submit])。
|
||||
// 提交 offset 单次 OffsetFetchRequest V1 拉齐全部目标分区;
|
||||
// 协议错误(coordinator 不可用/鉴权失败等)上抛——调用方保留上次采样值,
|
||||
// 不再出现「静默 -1 当 0」的假冻结。
|
||||
func (r *LagReader) Total(ctx context.Context, topics []string) (int64, error) {
|
||||
if ctx.Err() != nil {
|
||||
return 0, ctx.Err()
|
||||
}
|
||||
if len(topics) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
req := sarama.NewOffsetFetchRequest(sarama.V2_1_0_0, r.group, nil)
|
||||
topicParts := make(map[string][]int32, len(topics))
|
||||
for _, topic := range topics {
|
||||
pids, err := r.client.Partitions(topic)
|
||||
if err != nil {
|
||||
return 0, err // topic 不存在/元数据不可用:真错误上抛
|
||||
}
|
||||
topicParts[topic] = pids
|
||||
for _, pid := range pids {
|
||||
req.AddPartition(topic, pid) // V1 显式分区列表(-1 全分区属 v8+ KIP-822)
|
||||
}
|
||||
}
|
||||
br, err := r.coordinator()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
resp, err := br.FetchOffset(req)
|
||||
if err != nil {
|
||||
r.dropCoordinator()
|
||||
return 0, fmt.Errorf("offset fetch 失败: %w", err)
|
||||
}
|
||||
var lag int64
|
||||
for topic, pids := range topicParts {
|
||||
for _, pid := range pids {
|
||||
blk := resp.GetBlock(topic, pid)
|
||||
if blk == nil {
|
||||
return 0, fmt.Errorf("offset fetch 响应缺分区 topic=%s pid=%d", topic, pid)
|
||||
}
|
||||
if blk.Err != sarama.ErrNoError {
|
||||
// 协议级错误(coordinator 漂移/鉴权失败/未知)→上抛;
|
||||
// 「组从未提交」在 V1 语义下=Offset=-1 且 Err=NoError→按 0 计。
|
||||
return 0, fmt.Errorf("offset fetch 分区错误 topic=%s pid=%d err=%s",
|
||||
topic, pid, blk.Err.Error())
|
||||
}
|
||||
hw, err := r.client.GetOffset(topic, pid, sarama.OffsetNewest)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if co := blk.Offset; hw > co { // 未提交(co=−1)按高水位差累计(新组起步)
|
||||
lag += hw - co
|
||||
}
|
||||
}
|
||||
}
|
||||
return lag, nil
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
// Package queue Kafka 消息队列封装(IBM/sarama,Day 7):
|
||||
// 异步生产者(自动重试 + 死信转投)与消费者组(处理成功才提交 offset)。
|
||||
package queue
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ErrProducerClosed 生产者已关闭,拒绝新消息。
|
||||
var ErrProducerClosed = errors.New("queue: 生产者已关闭")
|
||||
|
||||
// asyncProducer Sarama 异步生产者抽象(sarama.AsyncProducer 满足;单测可注入 mock)。
|
||||
type asyncProducer interface {
|
||||
Input() chan<- *sarama.ProducerMessage
|
||||
Successes() <-chan *sarama.ProducerMessage
|
||||
Errors() <-chan *sarama.ProducerError
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ProducerConfig 生产者配置。
|
||||
type ProducerConfig struct {
|
||||
Brokers []string
|
||||
MaxRetry int // 发送失败后的重投次数(默认 3;broker 级重试另由 sarama 内部处理)
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
// Producer 异步生产者:Send 非阻塞投递;后台协程处理回执,
|
||||
// 失败自动重投,重试耗尽转投死信主题 <topic>.dlq。
|
||||
type Producer struct {
|
||||
inner asyncProducer
|
||||
maxRetry int
|
||||
log *zap.Logger
|
||||
done chan struct{}
|
||||
closeOnce sync.Once
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewProducer 连接 Kafka 并启动异步生产者。
|
||||
func NewProducer(cfg ProducerConfig) (*Producer, error) {
|
||||
if len(cfg.Brokers) == 0 {
|
||||
return nil, errors.New("queue: kafka.brokers 不能为空")
|
||||
}
|
||||
sc := sarama.NewConfig()
|
||||
sc.Version = sarama.V2_8_0_0
|
||||
sc.Producer.RequiredAcks = sarama.WaitForAll // 等待全部副本确认,防丢
|
||||
sc.Producer.Idempotent = true // 幂等生产者,防重乱序
|
||||
sc.Producer.Retry.Max = 5 // broker 级重试
|
||||
sc.Producer.Return.Successes = true // 回传成功(需处理两通道)
|
||||
sc.Producer.Return.Errors = true
|
||||
sc.Net.MaxOpenRequests = 1 // 幂等模式要求串行请求
|
||||
// P1 压测优化(09-27):sarama 默认零 linger——每条消息独立 request,
|
||||
// broker(同 HDD)随机写放大,洪峰下 Input chan 打满反向阻塞 SubmitFast
|
||||
// (fast_path p99 907ms 实测根因)。攒批 5ms/200 条+snappy 压缩后
|
||||
// broker 请求数 ÷N,顺带提升单请求吞吐;ChannelBufferSize 拉平毛刺。
|
||||
sc.Producer.Flush.Frequency = 5 * time.Millisecond
|
||||
sc.Producer.Flush.MaxMessages = 200
|
||||
sc.Producer.Compression = sarama.CompressionSnappy
|
||||
sc.ChannelBufferSize = 1024
|
||||
inner, err := sarama.NewAsyncProducer(cfg.Brokers, sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newProducer(inner, cfg), nil
|
||||
}
|
||||
|
||||
// newProducer 装配(注入 mock 时使用)。
|
||||
func newProducer(inner asyncProducer, cfg ProducerConfig) *Producer {
|
||||
if cfg.MaxRetry <= 0 {
|
||||
cfg.MaxRetry = 3
|
||||
}
|
||||
if cfg.Logger == nil {
|
||||
cfg.Logger = zap.NewNop()
|
||||
}
|
||||
p := &Producer{
|
||||
inner: inner,
|
||||
maxRetry: cfg.MaxRetry,
|
||||
log: cfg.Logger,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
p.wg.Add(1)
|
||||
go p.loop()
|
||||
return p
|
||||
}
|
||||
|
||||
// Send 异步投递一条消息;返回 nil 仅代表已进入发送通道。
|
||||
func (p *Producer) Send(topic string, key, value []byte) error {
|
||||
if topic == "" {
|
||||
return errors.New("queue: topic 不能为空")
|
||||
}
|
||||
if len(value) == 0 {
|
||||
return errors.New("queue: value 不能为空")
|
||||
}
|
||||
msg := &sarama.ProducerMessage{
|
||||
Topic: topic,
|
||||
Key: sarama.ByteEncoder(key),
|
||||
Value: sarama.ByteEncoder(value),
|
||||
Metadata: retryMeta{},
|
||||
}
|
||||
select {
|
||||
case p.inner.Input() <- msg:
|
||||
return nil
|
||||
case <-p.done:
|
||||
return ErrProducerClosed
|
||||
}
|
||||
}
|
||||
|
||||
// Close 优雅关闭:拒绝新消息 → 排空内部通道 → 退出后台协程。
|
||||
func (p *Producer) Close() error {
|
||||
var err error
|
||||
p.closeOnce.Do(func() {
|
||||
close(p.done)
|
||||
err = p.inner.Close() // 触发 Successes/Errors 关闭并排空
|
||||
p.wg.Wait()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// loop 后台处理发送回执:成功记日志,失败重投,耗尽转死信。
|
||||
func (p *Producer) loop() {
|
||||
defer p.wg.Done()
|
||||
succClosed, errClosed := false, false
|
||||
for {
|
||||
select {
|
||||
case <-p.done:
|
||||
return
|
||||
case m, ok := <-p.inner.Successes():
|
||||
if !ok {
|
||||
succClosed = true
|
||||
if errClosed {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
p.log.Debug("kafka_send_ok",
|
||||
zap.String("topic", m.Topic), zap.Any("offset", m.Offset))
|
||||
case e, ok := <-p.inner.Errors():
|
||||
if !ok {
|
||||
errClosed = true
|
||||
if succClosed {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
p.handleError(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleError 失败处理:未超限则重投;超限转死信;死信再失败仅告警丢弃。
|
||||
func (p *Producer) handleError(e *sarama.ProducerError) {
|
||||
meta := metaOf(e.Msg)
|
||||
switch {
|
||||
case meta.dlq: // 死信消息自身发送失败:最后防线,仅记录
|
||||
p.log.Error("kafka_dlq_failed",
|
||||
zap.String("topic", e.Msg.Topic), zap.Error(e.Err))
|
||||
case meta.retry < p.maxRetry: // 重投
|
||||
meta.retry++
|
||||
e.Msg.Metadata = meta
|
||||
select {
|
||||
case p.inner.Input() <- e.Msg:
|
||||
p.log.Warn("kafka_send_retry",
|
||||
zap.String("topic", e.Msg.Topic), zap.Int("retry", meta.retry), zap.Error(e.Err))
|
||||
case <-p.done:
|
||||
}
|
||||
default: // 重试耗尽 → 死信
|
||||
p.inner.Input() <- &sarama.ProducerMessage{
|
||||
Topic: DLQTopic(e.Msg.Topic),
|
||||
Key: e.Msg.Key,
|
||||
Value: e.Msg.Value,
|
||||
Metadata: retryMeta{dlq: true},
|
||||
}
|
||||
p.log.Error("kafka_send_to_dlq",
|
||||
zap.String("topic", e.Msg.Topic),
|
||||
zap.String("dlq_topic", DLQTopic(e.Msg.Topic)),
|
||||
zap.Int("retries", meta.retry), zap.Error(e.Err))
|
||||
}
|
||||
}
|
||||
|
||||
// DLQTopic 返回主题对应的死信主题名。
|
||||
func DLQTopic(topic string) string { return topic + DLQSuffix }
|
||||
|
||||
// retryMeta 消息重试元数据(挂载于 ProducerMessage.Metadata)。
|
||||
type retryMeta struct {
|
||||
retry int
|
||||
dlq bool
|
||||
}
|
||||
|
||||
// metaOf 安全取出消息的重试元数据。
|
||||
func metaOf(m *sarama.ProducerMessage) retryMeta {
|
||||
if meta, ok := m.Metadata.(retryMeta); ok {
|
||||
return meta
|
||||
}
|
||||
return retryMeta{}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// mockAsync 可控的异步生产者 mock:按脚本对 input 消息回 success / error。
|
||||
// Close 只关闭 input;script 协程排空后自行关闭 succ/errs(避免 close/send 竞争)。
|
||||
type mockAsync struct {
|
||||
input chan *sarama.ProducerMessage
|
||||
succ chan *sarama.ProducerMessage
|
||||
errs chan *sarama.ProducerError
|
||||
script func(m *sarama.ProducerMessage)
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func newMockAsync(script func(m *sarama.ProducerMessage)) *mockAsync {
|
||||
m := &mockAsync{
|
||||
input: make(chan *sarama.ProducerMessage, 64),
|
||||
succ: make(chan *sarama.ProducerMessage, 64),
|
||||
errs: make(chan *sarama.ProducerError, 64),
|
||||
script: script,
|
||||
}
|
||||
go func() {
|
||||
defer close(m.succ)
|
||||
defer close(m.errs)
|
||||
for msg := range m.input {
|
||||
m.script(msg)
|
||||
}
|
||||
}()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *mockAsync) Input() chan<- *sarama.ProducerMessage { return m.input }
|
||||
func (m *mockAsync) Successes() <-chan *sarama.ProducerMessage { return m.succ }
|
||||
func (m *mockAsync) Errors() <-chan *sarama.ProducerError { return m.errs }
|
||||
func (m *mockAsync) Close() error {
|
||||
m.closeOnce.Do(func() { close(m.input) })
|
||||
return nil
|
||||
}
|
||||
|
||||
// replyErr 将消息以错误回执返回(保留原 Metadata)。
|
||||
func replyErr(m *mockAsync, msg *sarama.ProducerMessage, err error) {
|
||||
m.errs <- &sarama.ProducerError{Msg: msg, Err: err}
|
||||
}
|
||||
|
||||
// TestProducer_Success 验证发送成功路径。
|
||||
func TestProducer_Success(t *testing.T) {
|
||||
var mock *mockAsync
|
||||
mock = newMockAsync(func(msg *sarama.ProducerMessage) {
|
||||
mock.succ <- msg
|
||||
})
|
||||
p := newProducer(mock, ProducerConfig{MaxRetry: 2, Logger: zap.NewNop()})
|
||||
defer func() { _ = p.Close() }()
|
||||
|
||||
if err := p.Send("t1", []byte("k"), []byte("v")); err != nil {
|
||||
t.Fatalf("Send 失败: %v", err)
|
||||
}
|
||||
select {
|
||||
case m := <-mock.succ:
|
||||
if m.Topic != "t1" || string(m.Value.(sarama.ByteEncoder)) != "v" {
|
||||
t.Errorf("成功回执内容不符: %+v", m)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("未收到成功回执")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProducer_RetryThenSuccess 验证失败后重投并最终成功。
|
||||
func TestProducer_RetryThenSuccess(t *testing.T) {
|
||||
var mock *mockAsync
|
||||
mock = newMockAsync(func(msg *sarama.ProducerMessage) {
|
||||
if metaOf(msg).retry < 1 { // 首次失败,重投后成功
|
||||
replyErr(mock, msg, errors.New("broker down"))
|
||||
return
|
||||
}
|
||||
mock.succ <- msg
|
||||
})
|
||||
p := newProducer(mock, ProducerConfig{MaxRetry: 3, Logger: zap.NewNop()})
|
||||
defer func() { _ = p.Close() }()
|
||||
|
||||
if err := p.Send("t2", []byte("k"), []byte("v")); err != nil {
|
||||
t.Fatalf("Send 失败: %v", err)
|
||||
}
|
||||
select {
|
||||
case m := <-mock.succ:
|
||||
if meta := metaOf(m); meta.retry != 1 {
|
||||
t.Errorf("最终成功应经历 1 次重试, 实际 %d", meta.retry)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("重试后未成功")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProducer_DeadLetter 验证重试耗尽转投死信主题。
|
||||
func TestProducer_DeadLetter(t *testing.T) {
|
||||
dlqCh := make(chan *sarama.ProducerMessage, 1)
|
||||
var mock *mockAsync
|
||||
mock = newMockAsync(func(msg *sarama.ProducerMessage) {
|
||||
if meta := metaOf(msg); meta.dlq { // 死信消息:直接确认成功
|
||||
dlqCh <- msg
|
||||
mock.succ <- msg
|
||||
return
|
||||
}
|
||||
replyErr(mock, msg, errors.New("always fail")) // 原消息永远失败
|
||||
})
|
||||
p := newProducer(mock, ProducerConfig{MaxRetry: 2, Logger: zap.NewNop()})
|
||||
defer func() { _ = p.Close() }()
|
||||
|
||||
if err := p.Send("t3", []byte("k"), []byte("v")); err != nil {
|
||||
t.Fatalf("Send 失败: %v", err)
|
||||
}
|
||||
var got *sarama.ProducerMessage
|
||||
select {
|
||||
case got = <-dlqCh:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("重试耗尽后未进入死信主题")
|
||||
}
|
||||
if got.Topic != "t3.dlq" {
|
||||
t.Errorf("死信主题 = %q, 期望 t3.dlq", got.Topic)
|
||||
}
|
||||
if string(got.Value.(sarama.ByteEncoder)) != "v" {
|
||||
t.Error("死信应保留原消息体")
|
||||
}
|
||||
if meta := metaOf(got); !meta.dlq {
|
||||
t.Error("死信消息应带 dlq 标记")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProducer_SendValidation 验证入参校验。
|
||||
func TestProducer_SendValidation(t *testing.T) {
|
||||
var mock *mockAsync
|
||||
mock = newMockAsync(func(msg *sarama.ProducerMessage) { mock.succ <- msg })
|
||||
p := newProducer(mock, ProducerConfig{Logger: zap.NewNop()})
|
||||
defer func() { _ = p.Close() }()
|
||||
if err := p.Send("", nil, []byte("v")); err == nil {
|
||||
t.Error("空 topic 应报错")
|
||||
}
|
||||
if err := p.Send("t", nil, nil); err == nil {
|
||||
t.Error("空 value 应报错")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package queue
|
||||
|
||||
// Topic 键:config.Kafka.Topics 的键名(业务事件类型)。
|
||||
const (
|
||||
KeySubmit = "submit" // 下行发送任务
|
||||
KeyReport = "report" // 状态回执
|
||||
KeyUp = "up" // 上行消息
|
||||
KeyStats = "stats" // 统计事件
|
||||
KeyDeliver = "deliver" // Day 61:CMPP Deliver 回执下发(终态→协议侧推送桥)
|
||||
)
|
||||
|
||||
// 默认 Topic 名(与实施计划 A-0.4 验收项一致:四个核心 Topic)。
|
||||
const (
|
||||
TopicSubmit = "sms-submit"
|
||||
TopicReport = "sms-report"
|
||||
TopicUp = "sms-up"
|
||||
TopicStats = "stats"
|
||||
TopicDeliver = "cmpp-deliver"
|
||||
)
|
||||
|
||||
// DLQSuffix 死信主题后缀:发送重试耗尽的消息进入 <topic>.dlq。
|
||||
const DLQSuffix = ".dlq"
|
||||
|
||||
// DefaultTopics 返回键→默认主题名映射(供配置缺省时使用)。
|
||||
func DefaultTopics() map[string]string {
|
||||
return map[string]string{
|
||||
KeySubmit: TopicSubmit,
|
||||
KeyReport: TopicReport,
|
||||
KeyUp: TopicUp,
|
||||
KeyStats: TopicStats,
|
||||
KeyDeliver: TopicDeliver,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package queue
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestTopicConstants 验证四个核心 Topic 与实施计划 A-0.4 一致。
|
||||
func TestTopicConstants(t *testing.T) {
|
||||
want := map[string]string{
|
||||
KeySubmit: TopicSubmit,
|
||||
KeyReport: TopicReport,
|
||||
KeyUp: TopicUp,
|
||||
KeyStats: TopicStats,
|
||||
}
|
||||
for k, v := range want {
|
||||
if got := DefaultTopics()[k]; got != v {
|
||||
t.Errorf("DefaultTopics()[%s] = %q, 期望 %q", k, got, v)
|
||||
}
|
||||
}
|
||||
if TopicSubmit != "sms-submit" || TopicReport != "sms-report" ||
|
||||
TopicUp != "sms-up" || TopicStats != "stats" {
|
||||
t.Fatal("核心 Topic 名与实施计划 A-0.4 不一致")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDLQTopic 验证死信主题命名。
|
||||
func TestDLQTopic(t *testing.T) {
|
||||
if got := DLQTopic(TopicSubmit); got != "sms-submit.dlq" {
|
||||
t.Errorf("DLQTopic = %q, 期望 sms-submit.dlq", got)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user