You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go Echo REST API异步长任务实现:返回202后执行耗时操作

正确实现方案

原代码的问题在于启动goroutine后用select等待了channel的结果,导致请求会一直阻塞到长任务完成才返回响应。要实现「ValidateFile完成后立即返回202,长任务后台执行」,只需要启动goroutine后直接返回响应即可,不需要等待channel。

修改后的核心代码

func (a *BatchHandler) Temp(c echo.Context) error {
    email, organization, file, err := validateFormInput(c)
    if err != nil {
        return c.JSON(http.StatusUnprocessableEntity, err.Error())
    }

    filePath := getFilePath(homeDir)

    err = a.BUsecase.Save(file, homeDir, filePath)
    if err != nil {
        return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
    }

    err = a.BUsecase.ValidateFile(filePath)
    if err != nil {
        return c.JSON(getStatusCode(err), ResponseError{Message: err.Error()})
    }

    // 创建独立的上下文,避免请求结束后原context被取消导致后台任务中断
    ctx, cancel := context.WithTimeout(context.Background(), a.BUsecase.contextTimeout)
    defer cancel()

    // 启动goroutine执行长任务,无需等待结果
    go func(ctx context.Context, filePath string) {
        res, err := a.BUsecase.DoSomethingForEveryLong(ctx, filePath)
        if err != nil {
            logrus.Error("Error while annotating file: ", err)
            return
        }
        logrus.Info("Successfully Annotated the file, count: ", len(res))
    }(ctx, filePath)

    // 立即返回202 Accepted响应
    return c.String(http.StatusAccepted, "File validation passed, annotation task started in background")
}

关键修改点说明

  • 移除阻塞的select和channel:不需要等待后台任务结果,启动goroutine后直接返回响应。
  • 为后台任务创建独立上下文:原请求的c.Request().Context()会在响应返回后被取消,会导致DoSomethingForEveryLong里的操作被中断,所以用context.Background()创建新上下文并设置超时,保证后台任务能正常执行。
  • 调整错误和日志处理:后台任务的结果直接通过日志记录,不需要返回给客户端(因为已经提前返回了响应)。

额外注意事项

  • 资源清理:确保DoSomethingForEveryLong里的文件、数据库连接等资源都正确关闭(原代码里已经用了defer file.Close(),这点没问题)。
  • 任务追踪:如果需要让客户端查询任务状态,可以在启动goroutine时生成一个唯一任务ID,存入数据库或缓存,提供额外的查询接口。
  • goroutine泄漏:因为用了context.WithTimeout,即使任务执行出现异常,上下文超时后也会触发取消,避免goroutine长时间挂起。

内容的提问来源于stack exchange,提问作者mad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:05:06