mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
feat: httpc 集成改进,自动关联请求 Context
- 新增 contextHTTPClient 包装器,自动关联请求 Context - 新增 Context.HTTPC() 方法返回 contextHTTPClient - Client() 标记为 Deprecated - 添加 GetHTTPC() go:fix inline 兼容函数 当请求被取消时,出站 HTTP 请求也会自动取消。
This commit is contained in:
parent
10033f4a17
commit
f2295c3084
3 changed files with 86 additions and 7 deletions
17
compat.go
17
compat.go
|
|
@ -4,7 +4,12 @@
|
||||||
// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization.
|
// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization.
|
||||||
package touka
|
package touka
|
||||||
|
|
||||||
import "github.com/fenthope/reco"
|
import (
|
||||||
|
"github.com/WJQSERVER-STUDIO/httpc"
|
||||||
|
"github.com/fenthope/reco"
|
||||||
|
)
|
||||||
|
|
||||||
|
// --- reco 兼容函数 ---
|
||||||
|
|
||||||
// GetLogReco 返回底层的 reco.Logger 实例
|
// GetLogReco 返回底层的 reco.Logger 实例
|
||||||
// 用于需要访问 reco 特定功能的场景
|
// 用于需要访问 reco 特定功能的场景
|
||||||
|
|
@ -35,3 +40,13 @@ func (c *Context) GetLoggerReco() *reco.Logger {
|
||||||
}
|
}
|
||||||
return c.engine.LogReco
|
return c.engine.LogReco
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- httpc 兼容函数 ---
|
||||||
|
|
||||||
|
// GetHTTPC 返回底层的 httpc.Client 实例
|
||||||
|
// Deprecated: 使用 HTTPClient() 替代,新方法会自动关联请求 Context
|
||||||
|
//
|
||||||
|
//go:fix inline
|
||||||
|
func (c *Context) GetHTTPC() *httpc.Client {
|
||||||
|
return c.Client()
|
||||||
|
}
|
||||||
|
|
|
||||||
18
context.go
18
context.go
|
|
@ -865,11 +865,22 @@ func (c *Context) GetErrors() []error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client 返回 Engine 提供的 HTTPClient
|
// Client 返回 Engine 提供的 HTTPClient
|
||||||
// 方便在请求处理函数中进行出站 HTTP 请求
|
// 方便在请求处理函数中进行出站 HTTP请求
|
||||||
|
//
|
||||||
|
// Deprecated: 使用 HTTPC() 替代,新方法会自动关联请求 Context
|
||||||
func (c *Context) Client() *httpc.Client {
|
func (c *Context) Client() *httpc.Client {
|
||||||
return c.HTTPClient
|
return c.HTTPClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPC 返回自动关联请求 Context 的 HTTP 客户端
|
||||||
|
// 当请求被取消时,通过此客户端发起的出站请求也会自动取消
|
||||||
|
func (c *Context) HTTPC() *contextHTTPClient {
|
||||||
|
return &contextHTTPClient{
|
||||||
|
client: c.engine.HTTPClient,
|
||||||
|
ctx: c.ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Context() 返回请求的上下文,用于取消操作
|
// Context() 返回请求的上下文,用于取消操作
|
||||||
// 这是 Go 标准库的 `context.Context`,用于请求的取消和超时管理
|
// 这是 Go 标准库的 `context.Context`,用于请求的取消和超时管理
|
||||||
func (c *Context) Context() context.Context {
|
func (c *Context) Context() context.Context {
|
||||||
|
|
@ -1130,11 +1141,6 @@ func (c *Context) GetProtocol() string {
|
||||||
return c.Request.Proto
|
return c.Request.Proto
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHTTPC 获取框架自带传递的httpc
|
|
||||||
func (c *Context) GetHTTPC() *httpc.Client {
|
|
||||||
return c.HTTPClient
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetLogger 获取engine的Logger接口
|
// GetLogger 获取engine的Logger接口
|
||||||
func (c *Context) GetLogger() Logger {
|
func (c *Context) GetLogger() Logger {
|
||||||
return c.engine.logger
|
return c.engine.logger
|
||||||
|
|
|
||||||
58
context_httpc.go
Normal file
58
context_httpc.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
// Copyright 2024 WJQSERVER. All rights reserved.
|
||||||
|
// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization.
|
||||||
|
package touka
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/WJQSERVER-STUDIO/httpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// contextHTTPClient 包装 httpc.Client,自动关联请求的 Context
|
||||||
|
// 当请求被取消时,出站 HTTP 请求也会自动取消
|
||||||
|
type contextHTTPClient struct {
|
||||||
|
client *httpc.Client
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRequestBuilder 创建请求构建器,自动关联请求 Context
|
||||||
|
func (c *contextHTTPClient) NewRequestBuilder(method, urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.NewRequestBuilder(method, urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET 创建 GET 请求构建器
|
||||||
|
func (c *contextHTTPClient) GET(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.GET(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST 创建 POST 请求构建器
|
||||||
|
func (c *contextHTTPClient) POST(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.POST(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT 创建 PUT 请求构建器
|
||||||
|
func (c *contextHTTPClient) PUT(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.PUT(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE 创建 DELETE 请求构建器
|
||||||
|
func (c *contextHTTPClient) DELETE(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.DELETE(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH 创建 PATCH 请求构建器
|
||||||
|
func (c *contextHTTPClient) PATCH(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.PATCH(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HEAD 创建 HEAD 请求构建器
|
||||||
|
func (c *contextHTTPClient) HEAD(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.HEAD(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONS 创建 OPTIONS 请求构建器
|
||||||
|
func (c *contextHTTPClient) OPTIONS(urlStr string) *httpc.RequestBuilder {
|
||||||
|
return c.client.OPTIONS(urlStr).WithContext(c.ctx)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue