增加安全字段校验提示、请求预算和就绪/存活探针, 让绑定、数据库、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
-10
@@ -79,12 +79,12 @@ func Open(ctx context.Context, cfg Config, options Options) (result *Connection,
|
||||
maxIdle = *cfg.MaxIdle
|
||||
}
|
||||
if cfg.ProbeTimeoutMS < 0 || maxOpen < 0 || maxIdle < 0 || cfg.Port < 0 || cfg.Port > 65535 || (maxOpen > 0 && maxIdle > maxOpen) {
|
||||
return nil, fault.Wrap(CodeConfig, "validate database configuration failed", errors.New("invalid pool, port or probe setting"))
|
||||
return nil, fault.WrapContext(ctx, CodeConfig, "validate database configuration failed", errors.New("invalid pool, port or probe setting"))
|
||||
}
|
||||
dialect := options.Dialector
|
||||
if dialect == nil {
|
||||
if cfg.Host == "" || cfg.Database == "" {
|
||||
return nil, fault.Wrap(CodeTarget, "validate MySQL target failed", errors.New("MySQL host and database are required"))
|
||||
return nil, fault.WrapContext(ctx, CodeTarget, "validate MySQL target failed", errors.New("MySQL host and database are required"))
|
||||
}
|
||||
if cfg.Port == 0 {
|
||||
cfg.Port = 3306
|
||||
@@ -97,7 +97,7 @@ func Open(ctx context.Context, cfg Config, options Options) (result *Connection,
|
||||
}
|
||||
location, locationErr := time.LoadLocation(cfg.Timezone)
|
||||
if locationErr != nil {
|
||||
return nil, fault.Wrap(CodeTimezone, "resolve MySQL timezone failed", locationErr)
|
||||
return nil, fault.WrapContext(ctx, CodeTimezone, "resolve MySQL timezone failed", locationErr)
|
||||
}
|
||||
driver := mysqlDriver.NewConfig()
|
||||
driver.User, driver.Passwd, driver.Net, driver.Addr, driver.DBName = cfg.Username, cfg.Password, "tcp", net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)), cfg.Database
|
||||
@@ -110,18 +110,18 @@ func Open(ctx context.Context, cfg Config, options Options) (result *Connection,
|
||||
if openErr != nil {
|
||||
if orm != nil {
|
||||
if pool, poolErr := orm.DB(); poolErr == nil {
|
||||
openErr = errors.Join(openErr, fault.Wrap(CodeClose, "close failed database pool failed", pool.Close()))
|
||||
openErr = errors.Join(openErr, fault.WrapContext(ctx, CodeClose, "close failed database pool failed", pool.Close()))
|
||||
}
|
||||
}
|
||||
return nil, fault.Wrap(CodeOpen, "open database failed", openErr)
|
||||
return nil, fault.WrapContext(ctx, CodeOpen, "open database failed", openErr)
|
||||
}
|
||||
pool, poolErr := orm.DB()
|
||||
if poolErr != nil {
|
||||
return nil, fault.Wrap(CodePool, "resolve database pool failed", poolErr)
|
||||
return nil, fault.WrapContext(ctx, CodePool, "resolve database pool failed", poolErr)
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = errors.Join(err, fault.Wrap(CodeClose, "close pending database pool failed", pool.Close()))
|
||||
err = errors.Join(err, fault.WrapContext(ctx, CodeClose, "close pending database pool failed", pool.Close()))
|
||||
}
|
||||
}()
|
||||
pool.SetMaxOpenConns(maxOpen)
|
||||
@@ -132,14 +132,14 @@ func Open(ctx context.Context, cfg Config, options Options) (result *Connection,
|
||||
probe, cancel := context.WithTimeout(ctx, time.Duration(cfg.ProbeTimeoutMS)*time.Millisecond)
|
||||
defer cancel()
|
||||
if err = pool.PingContext(probe); err != nil {
|
||||
return nil, fault.Wrap(CodeProbe, "probe database failed", err)
|
||||
return nil, fault.WrapContext(ctx, CodeProbe, "probe database failed", err)
|
||||
}
|
||||
for _, plugin := range options.Plugins {
|
||||
if plugin == nil {
|
||||
return nil, fault.Wrap(CodePlugin, "install database plugin failed", errors.New("database plugin is nil"))
|
||||
return nil, fault.WrapContext(ctx, CodePlugin, "install database plugin failed", errors.New("database plugin is nil"))
|
||||
}
|
||||
if err = orm.Use(plugin); err != nil {
|
||||
return nil, fault.Wrap(CodePlugin, "install database plugin failed", err)
|
||||
return nil, fault.WrapContext(ctx, CodePlugin, "install database plugin failed", err)
|
||||
}
|
||||
}
|
||||
return &Connection{root: orm, pool: pool}, nil
|
||||
|
||||