Files
ai_academy/internal/runtime/shutdown.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

51 lines
1.4 KiB
Go

package runtime
import "time"
// DefaultShutdownTimeout is used when server.shutdown_timeout is unset.
const DefaultShutdownTimeout = 30 * time.Second
// CleanupBudget is reserved from the configured shutdown timeout so connection
// drain cannot consume the whole grace period before process cleanup runs.
// Supervisors often use the same number for termination grace (Kubernetes
// defaults to 30s), so drain and cleanup share one budget instead of stacking.
const CleanupBudget = 5 * time.Second
// ShutdownBudgets splits total into an HTTP drain budget and a cleanup budget.
// Their sum equals total. total <= 0 uses DefaultShutdownTimeout. When total
// is too small to reserve CleanupBudget, the two halves are equal.
func ShutdownBudgets(total time.Duration) (drain, cleanup time.Duration) {
if total <= 0 {
total = DefaultShutdownTimeout
}
cleanup = CleanupBudget
if cleanup >= total {
cleanup = total / 2
if cleanup <= 0 {
cleanup = total
}
}
drain = total - cleanup
if drain <= 0 {
drain = total
}
return drain, cleanup
}
// WaitFor reports whether ch was received before timeout. timeout <= 0 waits
// until ch is received. The boolean is false only when the timeout wins.
func WaitFor(ch <-chan struct{}, timeout time.Duration) bool {
if timeout <= 0 {
<-ch
return true
}
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-ch:
return true
case <-timer.C:
return false
}
}