159 lines
4.5 KiB
Markdown
159 lines
4.5 KiB
Markdown
|
|
# Context参数优化说明
|
|||
|
|
|
|||
|
|
## 优化内容
|
|||
|
|
|
|||
|
|
将 `ctx context.Context` 从 `ProcessRequests` 方法作为参数传入,而不是在每个 `ProcessXXXRequest` 方法内部创建。
|
|||
|
|
|
|||
|
|
## 优化原因
|
|||
|
|
|
|||
|
|
1. **更好的上下文管理**:可以从上层传递 context,支持超时、取消等操作
|
|||
|
|
2. **避免重复代码**:不需要在每个方法里都写 `ctx := context.Background()`
|
|||
|
|
3. **更好的可追踪性**:可以通过 context 传递请求ID、用户ID等元数据
|
|||
|
|
4. **符合Go最佳实践**:context 应该作为第一个参数传递
|
|||
|
|
|
|||
|
|
## 修改内容
|
|||
|
|
|
|||
|
|
### 1. ProcessRequests 方法签名修改
|
|||
|
|
|
|||
|
|
**修改前:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) ProcessRequests(params []byte, productID string) ([]byte, error) {
|
|||
|
|
var ctx, cancel = context.WithCancel(context.Background())
|
|||
|
|
defer cancel()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) ProcessRequests(ctx context.Context, params []byte, productID string) ([]byte, error) {
|
|||
|
|
var cancel context.CancelFunc
|
|||
|
|
ctx, cancel = context.WithCancel(ctx)
|
|||
|
|
defer cancel()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. PreprocessRequestApi 方法修改
|
|||
|
|
|
|||
|
|
**修改前:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) PreprocessRequestApi(params []byte, apiID string) ([]byte, error) {
|
|||
|
|
if processor, exists := requestProcessors[apiID]; exists {
|
|||
|
|
return processor(a, params)
|
|||
|
|
}
|
|||
|
|
return nil, errors.New("api请求, 未找到相应的处理程序")
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, params []byte, apiID string) ([]byte, error) {
|
|||
|
|
if processor, exists := requestProcessors[apiID]; exists {
|
|||
|
|
return processor(a, ctx, params)
|
|||
|
|
}
|
|||
|
|
return nil, errors.New("api请求, 未找到相应的处理程序")
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. requestProcessors map 类型定义修改
|
|||
|
|
|
|||
|
|
**修改前:**
|
|||
|
|
```go
|
|||
|
|
var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, error){
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后:**
|
|||
|
|
```go
|
|||
|
|
var requestProcessors = map[string]func(*ApiRequestService, context.Context, []byte) ([]byte, error){
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 所有 ProcessXXXRequest 方法签名修改
|
|||
|
|
|
|||
|
|
**修改前:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) ProcessYYSYBE08Request(params []byte) ([]byte, error) {
|
|||
|
|
ctx := context.Background()
|
|||
|
|
// ...
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后:**
|
|||
|
|
```go
|
|||
|
|
func (a *ApiRequestService) ProcessYYSYBE08Request(ctx context.Context, params []byte) ([]byte, error) {
|
|||
|
|
// 移除了 ctx := context.Background()
|
|||
|
|
// ...
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5. 错误码提取优化
|
|||
|
|
|
|||
|
|
**修改前:**
|
|||
|
|
```go
|
|||
|
|
if tianyuanErr, ok := err.(*tianyuanapi.Error); ok {
|
|||
|
|
errorCode = fmt.Sprintf("%d", tianyuanErr.Code)
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后:**
|
|||
|
|
```go
|
|||
|
|
if code := tianyuanapi.GetCodeByError(err); code != -1 {
|
|||
|
|
errorCode = fmt.Sprintf("%d", code)
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 修改的方法列表(32个)
|
|||
|
|
|
|||
|
|
所有以下方法都已添加 `ctx context.Context` 作为第一个参数,并移除了内部的 `ctx := context.Background()` 声明:
|
|||
|
|
|
|||
|
|
1. ProcessPersonEnterpriseProRequest
|
|||
|
|
2. ProcessBehaviorRiskScanRequest
|
|||
|
|
3. ProcessYYSYBE08Request
|
|||
|
|
4. ProcessYYSY09CDRequest
|
|||
|
|
5. ProcessFLXG0687Request
|
|||
|
|
6. ProcessFLXG3D56Request
|
|||
|
|
7. ProcessFLXG0V4BRequest
|
|||
|
|
8. ProcessQYGL8271Request
|
|||
|
|
9. ProcessIVYZ5733Request
|
|||
|
|
10. ProcessIVYZ9A2BRequest
|
|||
|
|
11. ProcessJRZQ0A03Request
|
|||
|
|
12. ProcessQYGL6F2DRequest
|
|||
|
|
13. ProcessJRZQ8203Request
|
|||
|
|
14. ProcessJRZQ4AA8Request
|
|||
|
|
15. ProcessQCXG7A2BRequest
|
|||
|
|
16. ProcessDWBG8B4DRequest
|
|||
|
|
17. ProcessDWBG6A2CRequest
|
|||
|
|
18. ProcessJRZQ4B6CRequest
|
|||
|
|
19. ProcessJRZQ09J8Request
|
|||
|
|
20. ProcessJRZQ5E9FRequest
|
|||
|
|
21. ProcessQYGL3F8ERequest
|
|||
|
|
22. ProcessIVYZ81NCRequest
|
|||
|
|
23. ProcessIVYZ7F3ARequest
|
|||
|
|
24. ProcessDWBG7F3ARequest
|
|||
|
|
25. ProcessJRZQ8A2DRequest
|
|||
|
|
26. ProcessYYSY8B1CRequest
|
|||
|
|
27. ProcessYYSY7D3ERequest
|
|||
|
|
28. ProcessFLXG7E8FRequest
|
|||
|
|
29. ProcessIVYZ8I9JRequest
|
|||
|
|
30. ProcessJRZQ7F1ARequest
|
|||
|
|
31. ProcessIVYZ3P9MRequest
|
|||
|
|
32. ProcessJRZQ6F2ARequest
|
|||
|
|
|
|||
|
|
## 调用方验证
|
|||
|
|
|
|||
|
|
调用方 `paySuccessNotify.go` 已经正确传递了 `ctx`:
|
|||
|
|
|
|||
|
|
```go
|
|||
|
|
combinedResponse, err := l.svcCtx.ApiRequestService.ProcessRequests(ctx, decryptData, product.Id)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 优势
|
|||
|
|
|
|||
|
|
1. **统一的上下文管理**:所有API调用共享同一个 context,可以统一控制超时和取消
|
|||
|
|
2. **更好的错误追踪**:可以在 context 中传递请求ID、用户ID等信息,方便日志追踪
|
|||
|
|
3. **代码更简洁**:不需要在每个方法中重复创建 context
|
|||
|
|
4. **符合Go规范**:context 作为第一个参数是 Go 的标准做法
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
- 所有方法现在都依赖外部传入的 `ctx`,不再内部创建
|
|||
|
|
- `ProcessRequests` 方法内部仍然会创建一个带 cancel 的 context,用于控制并发请求的取消
|
|||
|
|
- 如果将来需要在 context 中传递元数据(如请求ID、用户ID),可以在调用 `ProcessRequests` 之前设置
|
|||
|
|
|