增加安全字段校验提示、请求预算和就绪/存活探针, 让绑定、数据库、Redis 和出站响应解码保留可信诊断上下文。 补充完整应用装配及逆序清理示例,同步接入和迁移文档。 验证:make verify、make race-check 全部通过。 BREAKING CHANGE: 普通 Controller 默认启用 30 秒协作式请求预算, 可用 request_timeout_ms: 0 或 max-exec-time:"0" 关闭; 字段校验失败的 400 响应增加安全字段明细。
This commit is contained in:
1 parent
b4c3ac622c
commit
3196e45a01
22 files changed
+1294
-79
No files matched your search
+10
-7
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/diagnostic"
|
||||
failure "git.zhangdeman.cn/github/ginframework/internal/failure"
|
||||
)
|
||||
|
||||
@@ -28,10 +29,11 @@ type Request struct {
|
||||
|
||||
// Response 保存有界缓冲结果,不对供应商业务码作推断。
|
||||
type Response struct {
|
||||
StatusCode int `json:"status_code" dc:"上游真实 HTTP 状态,非 2xx 也保留正文"`
|
||||
Header http.Header `json:"-" dc:"上游原生响应头,不参与默认序列化"`
|
||||
Body []byte `json:"-" dc:"完整响应字节,超限或读取失败时不返回部分正文"`
|
||||
Duration time.Duration `json:"duration" dc:"从构造后发送到正文读取关闭完成的耗时,纳秒"`
|
||||
diagnostics diagnostic.Snapshot `dc:"发送时的可信标量快照,不持有请求 Context 或可变对象"`
|
||||
StatusCode int `json:"status_code" dc:"上游真实 HTTP 状态,非 2xx 也保留正文"`
|
||||
Header http.Header `json:"-" dc:"上游原生响应头,不参与默认序列化"`
|
||||
Body []byte `json:"-" dc:"完整响应字节,超限或读取失败时不返回部分正文"`
|
||||
Duration time.Duration `json:"duration" dc:"从构造后发送到正文读取关闭完成的耗时,纳秒"`
|
||||
}
|
||||
|
||||
// Build 创建原生请求,保留多值参数和正文类型,不执行网络请求。
|
||||
@@ -144,17 +146,18 @@ func (c *Client) Send(ctx context.Context, input Request) (result *Response, err
|
||||
if int64(len(body)) > c.maxResponseBytes {
|
||||
return nil, failure.WrapContext(ctx, CodeResponseLimit, "buffer outbound response limit exceeded", ErrResponseTooLarge)
|
||||
}
|
||||
return &Response{StatusCode: response.StatusCode, Header: response.Header.Clone(), Body: body}, nil
|
||||
return &Response{StatusCode: response.StatusCode, Header: response.Header.Clone(), Body: body, diagnostics: diagnostic.From(ctx)}, nil
|
||||
}
|
||||
|
||||
// DecodeJSON 解码单份响应 JSON,保留动态数字精度,不把解析失败当作空数据。
|
||||
// 入参:target 为 encoding/json 支持的非空接收指针。
|
||||
// 返回:目标类型不匹配、无效 JSON 或多份 JSON 返回内部错误,原始正文不进入日志。
|
||||
func (r *Response) DecodeJSON(target any) error {
|
||||
ctx := diagnostic.With(context.Background(), r.diagnostics)
|
||||
decoder := json.NewDecoder(bytes.NewReader(r.Body))
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(target); err != nil {
|
||||
return failure.Wrap(CodeJSONDecode, "decode outbound JSON response failed", err)
|
||||
return failure.WrapContext(ctx, CodeJSONDecode, "decode outbound JSON response failed", err)
|
||||
}
|
||||
var (
|
||||
extra any
|
||||
@@ -163,7 +166,7 @@ func (r *Response) DecodeJSON(target any) error {
|
||||
if err == nil {
|
||||
err = errors.New("multiple JSON values in response")
|
||||
}
|
||||
return failure.Wrap(CodeJSONDocuments, "validate outbound JSON document count failed", err)
|
||||
return failure.WrapContext(ctx, CodeJSONDocuments, "validate outbound JSON document count failed", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||