Go: fix context (#17277)

### Summary

Continue to add context

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-23 21:48:54 +08:00
committed by GitHub
parent 08e5d9c6be
commit 61201e437d
55 changed files with 725 additions and 642 deletions

View File

@@ -17,6 +17,7 @@
package handler
import (
"context"
"encoding/json"
"errors"
"net/http"
@@ -31,20 +32,20 @@ import (
)
type connectorServiceIface interface {
ListConnectors(userID string) (*service.ListConnectorsResponse, error)
CreateConnector(userID string, req *service.CreateConnectorRequest) (*entity.Connector, error)
GetConnector(connectorID, userID string) (*entity.Connector, common.ErrorCode, error)
ListLog(connectorID, userID string, page, pageSize int) ([]*entity.ConnectorSyncLog, int64, common.ErrorCode, error)
DeleteConnector(connectorID, userID string) (bool, common.ErrorCode, error)
RebuildConnector(connectorID, userID, kbID string) (bool, common.ErrorCode, error)
TestConnector(connectorID, userID string) error
UpdateConnector(connectorID, userID string, req *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error)
StartGoogleWebOAuth(userID, source string, req *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error)
GoogleWebOAuthCallback(source, stateID, oauthError, errorDescription, code string) string
PollGoogleWebOAuthResult(userID, source string, req *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error)
StartBoxWebOAuth(userID string, req *service.StartBoxWebOAuthRequest) (*service.StartBoxWebOAuthResponse, common.ErrorCode, error)
BoxWebOAuthCallback(flowID string, oauthError string, errorDescription string, code string) string
PollBoxWebOAuthResult(userID string, req *service.PollBoxWebOAuthResultRequest) (*service.PollBoxWebOAuthResultResponse, common.ErrorCode, error)
ListConnectors(ctx context.Context, userID string) (*service.ListConnectorsResponse, error)
CreateConnector(ctx context.Context, userID string, req *service.CreateConnectorRequest) (*entity.Connector, error)
GetConnector(ctx context.Context, connectorID, userID string) (*entity.Connector, common.ErrorCode, error)
ListLog(ctx context.Context, connectorID, userID string, page, pageSize int) ([]*entity.ConnectorSyncLog, int64, common.ErrorCode, error)
DeleteConnector(ctx context.Context, connectorID, userID string) (bool, common.ErrorCode, error)
RebuildConnector(ctx context.Context, connectorID, userID, kbID string) (bool, common.ErrorCode, error)
TestConnector(ctx context.Context, connectorID, userID string) error
UpdateConnector(ctx context.Context, connectorID, userID string, req *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error)
StartGoogleWebOAuth(ctx context.Context, userID, source string, req *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error)
GoogleWebOAuthCallback(ctx context.Context, source, stateID, oauthError, errorDescription, code string) string
PollGoogleWebOAuthResult(ctx context.Context, userID, source string, req *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error)
StartBoxWebOAuth(ctx context.Context, userID string, req *service.StartBoxWebOAuthRequest) (*service.StartBoxWebOAuthResponse, common.ErrorCode, error)
BoxWebOAuthCallback(ctx context.Context, flowID string, oauthError string, errorDescription string, code string) string
PollBoxWebOAuthResult(ctx context.Context, userID string, req *service.PollBoxWebOAuthResultRequest) (*service.PollBoxWebOAuthResultResponse, common.ErrorCode, error)
}
// ConnectorHandler connector handler
@@ -76,9 +77,10 @@ func (h *ConnectorHandler) ListConnectors(c *gin.Context) {
return
}
userID := user.ID
ctx := c.Request.Context()
// List connectors
result, err := h.connectorService.ListConnectors(userID)
result, err := h.connectorService.ListConnectors(ctx, userID)
if err != nil {
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, err.Error())
return
@@ -120,8 +122,9 @@ func (h *ConnectorHandler) GetConnector(c *gin.Context) {
common.ErrorWithCode(c, errorCode, errorMessage)
return
}
ctx := c.Request.Context()
connector, code, err := h.connectorService.GetConnector(c.Param("connector_id"), user.ID)
connector, code, err := h.connectorService.GetConnector(ctx, c.Param("connector_id"), user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -144,7 +147,9 @@ func (h *ConnectorHandler) UpdateConnector(c *gin.Context) {
return
}
connector, code, err := h.connectorService.UpdateConnector(c.Param("connector_id"), user.ID, req)
ctx := c.Request.Context()
connector, code, err := h.connectorService.UpdateConnector(ctx, c.Param("connector_id"), user.ID, req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -215,7 +220,9 @@ func (h *ConnectorHandler) ListLogs(c *gin.Context) {
pageSize = parsedPageSize
}
logs, total, code, err := h.connectorService.ListLog(c.Param("connector_id"), user.ID, page, pageSize)
ctx := c.Request.Context()
logs, total, code, err := h.connectorService.ListLog(ctx, c.Param("connector_id"), user.ID, page, pageSize)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -261,7 +268,9 @@ func (h *ConnectorHandler) CreateConnector(c *gin.Context) {
return
}
connector, err := h.connectorService.CreateConnector(user.ID, &req)
ctx := c.Request.Context()
connector, err := h.connectorService.CreateConnector(ctx, user.ID, &req)
if err != nil {
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, common.CodeServerError, nil, err.Error())
return
@@ -290,7 +299,9 @@ func (h *ConnectorHandler) TestConnector(c *gin.Context) {
return
}
err := h.connectorService.TestConnector(connectorID, user.ID)
ctx := c.Request.Context()
err := h.connectorService.TestConnector(ctx, connectorID, user.ID)
if errors.Is(err, service.ErrConnectorTestUnsupported) {
connectorErrorResponse(c, err)
return
@@ -319,7 +330,9 @@ func (h *ConnectorHandler) DeleteConnector(c *gin.Context) {
return
}
ok, code, err := h.connectorService.DeleteConnector(c.Param("connector_id"), user.ID)
ctx := c.Request.Context()
ok, code, err := h.connectorService.DeleteConnector(ctx, c.Param("connector_id"), user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -357,7 +370,8 @@ func (h *ConnectorHandler) RebuildConnector(c *gin.Context) {
return
}
ok, code, err := h.connectorService.RebuildConnector(c.Param("connector_id"), user.ID, req.KbID)
ctx := c.Request.Context()
ok, code, err := h.connectorService.RebuildConnector(ctx, c.Param("connector_id"), user.ID, req.KbID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -379,7 +393,9 @@ func (h *ConnectorHandler) StartGoogleWebOAuth(c *gin.Context) {
return
}
data, code, err := h.connectorService.StartGoogleWebOAuth(user.ID, c.DefaultQuery("type", "google-drive"), &req)
ctx := c.Request.Context()
data, code, err := h.connectorService.StartGoogleWebOAuth(ctx, user.ID, c.DefaultQuery("type", "google-drive"), &req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -401,7 +417,9 @@ func (h *ConnectorHandler) PollGoogleWebOAuthResult(c *gin.Context) {
return
}
data, code, err := h.connectorService.PollGoogleWebOAuthResult(user.ID, c.Query("type"), &req)
ctx := c.Request.Context()
data, code, err := h.connectorService.PollGoogleWebOAuthResult(ctx, user.ID, c.Query("type"), &req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -423,7 +441,9 @@ func (h *ConnectorHandler) GmailWebOAuthCallback(c *gin.Context) {
}
func (h *ConnectorHandler) googleWebOAuthCallback(c *gin.Context, source string) {
html := h.connectorService.GoogleWebOAuthCallback(
ctx := c.Request.Context()
html := h.connectorService.GoogleWebOAuthCallback(ctx,
source,
c.Query("state"),
c.Query("error"),
@@ -444,7 +464,9 @@ func (h *ConnectorHandler) StartBoxWebOAuth(c *gin.Context) {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
}
resp, code, err := h.connectorService.StartBoxWebOAuth(user.ID, &req)
ctx := c.Request.Context()
resp, code, err := h.connectorService.StartBoxWebOAuth(ctx, user.ID, &req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -458,7 +480,9 @@ func (h *ConnectorHandler) BoxWebOAuthCallback(c *gin.Context) {
errorDescription := c.Query("error_description")
code := c.Query("code")
html := h.connectorService.BoxWebOAuthCallback(flowID, oauthError, errorDescription, code)
ctx := c.Request.Context()
html := h.connectorService.BoxWebOAuthCallback(ctx, flowID, oauthError, errorDescription, code)
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(html))
}
@@ -474,7 +498,9 @@ func (h *ConnectorHandler) PollBoxWebOAuthResult(c *gin.Context) {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
}
resp, code, err := h.connectorService.PollBoxWebOAuthResult(user.ID, &req)
ctx := c.Request.Context()
resp, code, err := h.connectorService.PollBoxWebOAuthResult(ctx, user.ID, &req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return

View File

@@ -1,6 +1,7 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"net/http"
@@ -25,54 +26,54 @@ type fakeConnectorService struct {
html string
}
func (s fakeConnectorService) ListConnectors(string) (*service.ListConnectorsResponse, error) {
func (s fakeConnectorService) ListConnectors(context.Context, string) (*service.ListConnectorsResponse, error) {
return &service.ListConnectorsResponse{}, nil
}
func (s fakeConnectorService) TestConnector(string, string) error {
func (s fakeConnectorService) TestConnector(context.Context, string, string) error {
return s.err
}
func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) {
func (s fakeConnectorService) CreateConnector(context.Context, string, *service.CreateConnectorRequest) (*entity.Connector, error) {
if s.err != nil {
return nil, s.err
}
return s.connector, nil
}
func (s fakeConnectorService) GetConnector(string, string) (*entity.Connector, common.ErrorCode, error) {
func (s fakeConnectorService) GetConnector(context.Context, string, string) (*entity.Connector, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return s.connector, common.CodeSuccess, nil
}
func (s fakeConnectorService) UpdateConnector(string, string, *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error) {
func (s fakeConnectorService) UpdateConnector(context.Context, string, string, *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return s.connector, common.CodeSuccess, nil
}
func (s fakeConnectorService) StartGoogleWebOAuth(string, string, *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error) {
func (s fakeConnectorService) StartGoogleWebOAuth(context.Context, string, string, *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return &service.StartGoogleWebOAuthResponse{}, common.CodeSuccess, nil
}
func (s fakeConnectorService) GoogleWebOAuthCallback(string, string, string, string, string) string {
func (s fakeConnectorService) GoogleWebOAuthCallback(context.Context, string, string, string, string, string) string {
return ""
}
func (s fakeConnectorService) PollGoogleWebOAuthResult(string, string, *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error) {
func (s fakeConnectorService) PollGoogleWebOAuthResult(context.Context, string, string, *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return &service.PollGoogleWebOAuthResultResponse{}, common.CodeSuccess, nil
}
func (s fakeConnectorService) StartBoxWebOAuth(string, *service.StartBoxWebOAuthRequest) (*service.StartBoxWebOAuthResponse, common.ErrorCode, error) {
func (s fakeConnectorService) StartBoxWebOAuth(context.Context, string, *service.StartBoxWebOAuthRequest) (*service.StartBoxWebOAuthResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
@@ -83,35 +84,35 @@ func (s fakeConnectorService) StartBoxWebOAuth(string, *service.StartBoxWebOAuth
}, common.CodeSuccess, nil
}
func (s fakeConnectorService) BoxWebOAuthCallback(string, string, string, string) string {
func (s fakeConnectorService) BoxWebOAuthCallback(context.Context, string, string, string, string) string {
if s.html != "" {
return s.html
}
return "<html>box</html>"
}
func (s fakeConnectorService) PollBoxWebOAuthResult(string, *service.PollBoxWebOAuthResultRequest) (*service.PollBoxWebOAuthResultResponse, common.ErrorCode, error) {
func (s fakeConnectorService) PollBoxWebOAuthResult(context.Context, string, *service.PollBoxWebOAuthResultRequest) (*service.PollBoxWebOAuthResultResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return &service.PollBoxWebOAuthResultResponse{}, common.CodeSuccess, nil
}
func (s fakeConnectorService) ListLog(string, string, int, int) ([]*entity.ConnectorSyncLog, int64, common.ErrorCode, error) {
func (s fakeConnectorService) ListLog(context.Context, string, string, int, int) ([]*entity.ConnectorSyncLog, int64, common.ErrorCode, error) {
if s.err != nil {
return nil, 0, s.code, s.err
}
return s.logs, s.total, common.CodeSuccess, nil
}
func (s fakeConnectorService) DeleteConnector(string, string) (bool, common.ErrorCode, error) {
func (s fakeConnectorService) DeleteConnector(context.Context, string, string) (bool, common.ErrorCode, error) {
if s.err != nil {
return false, s.code, s.err
}
return true, common.CodeSuccess, nil
}
func (s fakeConnectorService) RebuildConnector(string, string, string) (bool, common.ErrorCode, error) {
func (s fakeConnectorService) RebuildConnector(context.Context, string, string, string) (bool, common.ErrorCode, error) {
if s.err != nil {
return false, s.code, s.err
}

View File

@@ -55,7 +55,7 @@ type listDatasetsExt struct {
ParserID string `json:"parser_id,omitempty"`
}
// NewDatasetsHandler creates a new datasets handler.
// NewDatasetsHandler creates a new datasets' handler.
func NewDatasetsHandler(datasetsService *dataset.DatasetService, metadataService *service.MetadataService) *DatasetsHandler {
h := &DatasetsHandler{
datasetsService: datasetsService,
@@ -171,8 +171,10 @@ func (h *DatasetsHandler) GetDataset(c *gin.Context) {
return
}
ctx := c.Request.Context()
datasetID := c.Param("dataset_id")
result, code, err := h.datasetsService.GetDataset(datasetID, user.ID)
result, code, err := h.datasetsService.GetDataset(ctx, datasetID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -208,19 +210,21 @@ func (h *DatasetsHandler) UpdateDataset(c *gin.Context) {
}
var req service.UpdateDatasetRequest
if err := json.Unmarshal(bodyBytes, &req); err != nil {
if err = json.Unmarshal(bodyBytes, &req); err != nil {
common.ResponseWithCodeData(c, common.CodeDataError, nil, err.Error())
return
}
// Detect an explicitly provided parser_config key (even {} or null) so it is not
// rejected as "No properties were modified", mirroring the Python contract.
// rejected as "no properties were modified", mirroring the Python contract.
var providedFields map[string]json.RawMessage
if err := json.Unmarshal(bodyBytes, &providedFields); err == nil {
if err = json.Unmarshal(bodyBytes, &providedFields); err == nil {
_, req.ParserConfigProvided = providedFields["parser_config"]
}
result, code, err := h.datasetsService.UpdateDataset(datasetID, userID, req)
ctx := c.Request.Context()
result, code, err := h.datasetsService.UpdateDataset(ctx, datasetID, userID, req)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -407,13 +411,14 @@ func (h *DatasetsHandler) GetKnowledgeGraph(c *gin.Context) {
return
}
dataset, code, err := h.datasetsService.GetDataset(datasetID, user.ID)
ctx := c.Request.Context()
datasetInstance, code, err := h.datasetsService.GetDataset(ctx, datasetID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
}
tenantID, _ := dataset["tenant_id"].(string)
tenantID, _ := datasetInstance["tenant_id"].(string)
if tenantID == "" {
common.ResponseWithCodeData(c, common.CodeDataError, nil, "tenant_id is required")
return
@@ -470,7 +475,7 @@ func (h *DatasetsHandler) GetKnowledgeGraph(c *gin.Context) {
}
var graphData map[string]interface{}
if err := json.Unmarshal([]byte(contentWithWeight), &graphData); err != nil {
if err = json.Unmarshal([]byte(contentWithWeight), &graphData); err != nil {
common.SuccessWithData(c, result, "success")
return
}
@@ -577,13 +582,14 @@ func (h *DatasetsHandler) DeleteKnowledgeGraph(c *gin.Context) {
return
}
dataset, code, err := h.datasetsService.GetDataset(datasetID, user.ID)
ctx := c.Request.Context()
datasetInstance, code, err := h.datasetsService.GetDataset(ctx, datasetID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
}
tenantID, _ := dataset["tenant_id"].(string)
tenantID, _ := datasetInstance["tenant_id"].(string)
if tenantID == "" {
common.ResponseWithCodeData(c, common.CodeDataError, nil, "tenant_id is required")
return
@@ -611,8 +617,6 @@ func (h *DatasetsHandler) DeleteKnowledgeGraph(c *gin.Context) {
// @Summary Remove Tags
// @Description Remove tags from a dataset
// @Tags datasets
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param dataset_id path string true "Dataset ID"
// @Param request body object{tags []string} true "tags to remove"
@@ -631,13 +635,14 @@ func (h *DatasetsHandler) RemoveTags(c *gin.Context) {
return
}
dataset, code, err := h.datasetsService.GetDataset(datasetID, user.ID)
ctx := c.Request.Context()
datasetInstance, code, err := h.datasetsService.GetDataset(ctx, datasetID, user.ID)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
}
tenantID, _ := dataset["tenant_id"].(string)
tenantID, _ := datasetInstance["tenant_id"].(string)
if tenantID == "" {
common.ResponseWithCodeData(c, common.CodeDataError, nil, "tenant_id is required")
return
@@ -958,8 +963,6 @@ func (h *DatasetsHandler) UpdateDocumentMetadataConfig(c *gin.Context) {
// @Summary Search Datasets
// @Description Search for relevant chunks across one or more datasets based on a question
// @Tags datasets
// @Accept json
// @Produce json
// @Param request body service.SearchDatasetsRequest true "search parameters"
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/datasets/search [post]
@@ -1034,8 +1037,6 @@ func (h *DatasetsHandler) SearchDatasets(c *gin.Context) {
// @Summary Search Dataset
// @Description Search for relevant chunks within one dataset based on a question
// @Tags datasets
// @Accept json
// @Produce json
// @Param dataset_id path string true "dataset id"
// @Param request body service.SearchDatasetRequest true "search parameters"
// @Success 200 {object} map[string]interface{}