mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
feat: redesign server startup around Run options
Replace the old RunShutdown and RunTLS style entry points with a single Run(opts...) API for v1. Add focused startup semantics tests, keep TLS and graceful shutdown independent, ensure sibling servers are cleaned up on startup failure, and update docs to match the new option-based startup model.
This commit is contained in:
parent
fca9bbd3ef
commit
e4d3eed379
13 changed files with 577 additions and 335 deletions
|
|
@ -44,7 +44,9 @@ r.SetTLSServerConfigurator(func(server *http.Server) {
|
|||
Touka 支持配置 HTTP/1.1、HTTP/2 和 H2C(HTTP/2 Cleartext):
|
||||
|
||||
```go
|
||||
// 使用默认协议配置(仅 HTTP/1.1)
|
||||
// 使用默认协议配置
|
||||
// 普通 HTTP 启动时默认为 HTTP/1.1;若使用 WithTLS(...) 且未手动覆盖协议集,
|
||||
// HTTPS 服务器会默认启用 HTTP/1.1 与 HTTP/2。
|
||||
r.SetDefaultProtocols()
|
||||
|
||||
// 自定义协议配置
|
||||
|
|
@ -57,33 +59,49 @@ r.SetProtocols(&touka.ProtocolsConfig{
|
|||
|
||||
### 启动方式
|
||||
|
||||
Touka 提供了多种服务器启动方式:
|
||||
Touka 统一通过 `Run(opts...)` 启动服务器:
|
||||
|
||||
```go
|
||||
// 1. 简单启动(无优雅停机)
|
||||
r.Run(":8080")
|
||||
r.Run(touka.WithAddr(":8080"))
|
||||
|
||||
// 2. 带优雅停机的启动
|
||||
r.RunShutdown(":8080", 10*time.Second)
|
||||
r.Run(touka.WithAddr(":8080"), touka.WithGracefulShutdown(10*time.Second))
|
||||
|
||||
// 3. 带上下文的优雅停机
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
r.RunShutdownWithContext(":8080", ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
r.Run(
|
||||
touka.WithAddr(":8080"),
|
||||
touka.WithGracefulShutdown(10*time.Second),
|
||||
touka.WithShutdownContext(ctx),
|
||||
)
|
||||
|
||||
// 4. HTTPS 启动
|
||||
tlsConfig := &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
// 其他 TLS 配置...
|
||||
}
|
||||
r.RunTLS(":443", tlsConfig, 10*time.Second)
|
||||
// WithTLS(...) 与优雅关闭相互独立;这里演示 HTTPS + 默认优雅关闭超时。
|
||||
r.Run(
|
||||
touka.WithAddr(":443"),
|
||||
touka.WithTLS(tlsConfig),
|
||||
touka.WithGracefulShutdownDefault(),
|
||||
)
|
||||
|
||||
// 5. HTTPS + HTTP 重定向
|
||||
r.RunTLSRedir(":80", ":443", tlsConfig, 10*time.Second)
|
||||
// WithHTTPRedirect(...) 需要与 WithTLS(...) 配合使用。
|
||||
r.Run(
|
||||
touka.WithAddr(":443"),
|
||||
touka.WithTLS(tlsConfig),
|
||||
touka.WithHTTPRedirect(":80"),
|
||||
touka.WithGracefulShutdown(10*time.Second),
|
||||
)
|
||||
```
|
||||
|
||||
## 优雅停机 (Graceful Shutdown)
|
||||
|
||||
在部署新版本时,我们希望服务器停止接收新请求,但能处理完当前正在进行的请求。
|
||||
在部署新版本时,我们希望服务器停止接收新请求,但能处理完当前正在进行的请求。启用优雅关闭后,Touka 会监听 `SIGINT`/`SIGTERM`,并在关闭时取消活动请求的上下文。
|
||||
|
||||
```go
|
||||
r := touka.Default()
|
||||
|
|
@ -91,7 +109,7 @@ r := touka.Default()
|
|||
|
||||
// 监听 SIGINT 和 SIGTERM 信号
|
||||
// 如果在 10 秒内未处理完,则强制关闭
|
||||
if err := r.RunShutdown(":8080", 10*time.Second); err != nil {
|
||||
if err := r.Run(touka.WithAddr(":8080"), touka.WithGracefulShutdown(10*time.Second)); err != nil {
|
||||
log.Fatal("服务器退出异常:", err)
|
||||
}
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue