抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,36 @@
|
||||
// Package failure 在组件边界包装并记录一次错误,避免上层重复记录。
|
||||
package failure
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/fault"
|
||||
"git.zhangdeman.cn/github/ginframework/logger"
|
||||
)
|
||||
|
||||
// Wrap 在首次接收原始错误时增加场景并记录,已经分类的错误直接传播。
|
||||
// 入参:code 为场景码,message 为安全英文摘要,cause 为原始原因。
|
||||
// 返回:nil 原因返回 nil,否则保留原因链。
|
||||
func Wrap(code int, message string, cause error) error {
|
||||
return WrapContext(context.Background(), code, message, cause)
|
||||
}
|
||||
|
||||
// WrapContext 在错误首次进入组件时分类并记录可信诊断,保留原始原因链。
|
||||
// 入参:ctx 为标准上下文,code 为场景码,message 为安全摘要,cause 为原始原因。
|
||||
// 返回:nil 原因返回 nil;已分类错误原样传播,不重复记录。
|
||||
func WrapContext(ctx context.Context, code int, message string, cause error) error {
|
||||
if cause == nil || fault.Internal(cause) {
|
||||
return cause
|
||||
}
|
||||
result := fault.New(code, message, cause)
|
||||
logger.RecordErrorContext(ctx, message, result)
|
||||
return result
|
||||
}
|
||||
|
||||
// Capture 为命名返回错误提供源头分类。
|
||||
// 入参:target 为命名返回值地址,code 为场景码,message 为安全英文摘要。
|
||||
func Capture(target *error, code int, message string) {
|
||||
if target != nil {
|
||||
*target = Wrap(code, message, *target)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Package redisserver 仅为测试启动监听临时 Unix socket 的独立 Redis。
|
||||
package redisserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// New 创建隔离测试客户端,不连接部署配置中的任何实例。
|
||||
// 入参:t 为测试生命周期,protocol 为 RESP 协议版本。
|
||||
// 返回:可清空和关闭的测试客户端;环境无 redis-server 时跳过。
|
||||
func New(t *testing.T, protocol int) *redis.Client {
|
||||
var (
|
||||
server, err = exec.LookPath("redis-server")
|
||||
)
|
||||
var (
|
||||
directory string
|
||||
)
|
||||
var (
|
||||
command *exec.Cmd
|
||||
)
|
||||
var (
|
||||
client *redis.Client
|
||||
)
|
||||
var (
|
||||
deadline time.Time
|
||||
)
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Skip("redis-server is required for isolated Redis integration tests")
|
||||
}
|
||||
if directory, err = os.MkdirTemp("", "gf-"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.RemoveAll(directory) })
|
||||
command = exec.Command(server, "--port", "0", "--unixsocket", filepath.Join(directory, "redis.sock"), "--save", "", "--appendonly", "no")
|
||||
command.Stderr = os.Stderr
|
||||
if err = command.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = command.Process.Kill(); _ = command.Wait() })
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Network: "unix",
|
||||
Addr: filepath.Join(directory, "redis.sock"),
|
||||
Protocol: protocol,
|
||||
MaxRetries: -1,
|
||||
ContextTimeoutEnabled: true,
|
||||
})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
for client.Ping(context.Background()).Err() != nil {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("isolated Redis did not start")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
return client
|
||||
}
|
||||