feat(http)!: 补齐核心请求处理与应用生命周期能力
Verify / verify (push) Canceled after 0s

增加安全字段校验提示、请求预算和就绪/存活探针,
让绑定、数据库、Redis 和出站响应解码保留可信诊断上下文。
补充完整应用装配及逆序清理示例,同步接入和迁移文档。

验证:make verify、make race-check 全部通过。

BREAKING CHANGE: 普通 Controller 默认启用 30 秒协作式请求预算,
可用 request_timeout_ms: 0 或 max-exec-time:"0" 关闭;
字段校验失败的 400 响应增加安全字段明细。
This commit is contained in:
zhangdeman committed 2026-11-26 20:41:33 -08:00
1 parent b4c3ac622c
commit 3196e45a01
22 files changed
+1294 -79

No files matched your search

+37 -6
View File
@@ -84,7 +84,7 @@ func (s *Server) Group(prefix string, middleware []gin.HandlerFunc, controllers
if meta.Tag.Get("path") == "" || meta.Tag.Get("method") == "" {
return failure.Wrap(codeRouteDeclaration, "validate route declaration failed", errors.New("path and method tags are required"))
}
for _, unsupported := range []string{"is-sse", "is-ws", "max-exec-time", "rate-limit", "hook-sync", "output_strict", "no-login"} {
for _, unsupported := range []string{"is-sse", "is-ws", "rate-limit", "hook-sync", "output_strict", "no-login"} {
if raw := meta.Tag.Get(unsupported); raw != "" {
return failure.Wrap(codeUnsupportedTag, "validate supported route tags failed", fmt.Errorf("tag %s requires an application adapter or a native route", unsupported))
}
@@ -108,6 +108,14 @@ func (s *Server) Group(prefix string, middleware []gin.HandlerFunc, controllers
// 入参:method 为注册期缓存的方法,formType 为 Form 类型,pointer 为参数形式,meta 为元数据字段。
// 返回:单路由 Handler 或声明错误。
func (s *Server) controllerHandler(method reflect.Value, formType reflect.Type, pointer bool, meta reflect.StructField) (gin.HandlerFunc, error) {
budget := time.Duration(*s.config.RequestTimeoutMS) * time.Millisecond
if raw := meta.Tag.Get("max-exec-time"); raw != "" {
seconds, err := strconv.ParseInt(raw, 10, 64)
if err != nil || seconds < 0 || seconds > int64((1<<63-1)/time.Second) {
return nil, failure.Wrap(codeRequestBudget, "validate route request budget failed", errors.New("invalid max-exec-time seconds"))
}
budget = time.Duration(seconds) * time.Second
}
limit := request.DefaultMaxBodyBytes
if raw := meta.Tag.Get("max-body-bytes"); raw != "" {
parsed, err := strconv.ParseInt(raw, 10, 64)
@@ -117,8 +125,19 @@ func (s *Server) controllerHandler(method reflect.Value, formType reflect.Type,
limit = parsed
}
return func(ctx *gin.Context) {
if budget > 0 {
requestContext, cancel := context.WithTimeout(ctx.Request.Context(), budget)
defer cancel()
ctx.Request = ctx.Request.WithContext(requestContext)
}
if s.finishCanceled(ctx, nil) {
return
}
form := reflect.New(formType)
if err := request.Form.Bind(ctx, form.Interface(), request.WithMaxBodyBytes(limit)); err != nil {
if s.finishCanceled(ctx, err) {
return
}
status := http.StatusBadRequest
var (
large *http.MaxBytesError
@@ -134,14 +153,23 @@ func (s *Server) controllerHandler(method reflect.Value, formType reflect.Type,
}
defaults.SetDefaults(form.Interface())
if err := s.inject(ctx, form.Elem()); err != nil {
if s.finishCanceled(ctx, err) {
return
}
s.writeResult(ctx, nil, err, http.StatusInternalServerError)
return
}
if err := request.Form.Validate(form.Interface()); err != nil {
if err := request.Form.ValidateContext(ctx.Request.Context(), form.Interface()); err != nil {
if s.finishCanceled(ctx, err) {
return
}
s.writeResult(ctx, nil, err, http.StatusBadRequest)
return
}
input := form
if s.finishCanceled(ctx, nil) {
return
}
if !pointer {
input = form.Elem()
}
@@ -152,6 +180,9 @@ func (s *Server) controllerHandler(method reflect.Value, formType reflect.Type,
if !isNil(result[1]) {
err = result[1].Interface().(error)
}
if s.finishCanceled(ctx, err) {
return
}
s.writeResult(ctx, result[0].Interface(), err, 0)
}, nil
}
@@ -181,7 +212,7 @@ func (s *Server) inject(ctx *gin.Context, form reflect.Value) error {
if field.Kind() == reflect.Ptr {
if field.IsNil() {
if !field.CanSet() {
return failure.Wrap(codeInject, "resolve trusted field failed", errors.New("embedded field is not writable"))
return failure.WrapContext(ctx.Request.Context(), codeInject, "resolve trusted field failed", errors.New("embedded field is not writable"))
}
field.Set(reflect.New(field.Type().Elem()))
}
@@ -190,19 +221,19 @@ func (s *Server) inject(ctx *gin.Context, form reflect.Value) error {
field = field.Field(index)
}
if !field.CanSet() {
return failure.Wrap(codeInject, "resolve trusted field failed", errors.New("trusted field is not writable"))
return failure.WrapContext(ctx.Request.Context(), codeInject, "resolve trusted field failed", errors.New("trusted field is not writable"))
}
field.SetZero()
value, err := s.options.TrustedFields[name](ctx)
if err != nil {
return failure.Wrap(codeTrustedRead, "read trusted field failed", err)
return failure.WrapContext(ctx.Request.Context(), codeTrustedRead, "read trusted field failed", err)
}
if value == nil {
continue
}
source := reflect.ValueOf(value)
if !source.Type().AssignableTo(field.Type()) {
return failure.Wrap(codeTrustedType, "assign trusted field failed", errors.New("trusted field type mismatch"))
return failure.WrapContext(ctx.Request.Context(), codeTrustedType, "assign trusted field failed", errors.New("trusted field type mismatch"))
}
field.Set(source)
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.