关于 golang 一分钟处理百万请求的疑问

2018-09-29 19:18:05 +08:00
 chengxuyuan0917

每分钟百万级请求

比如这段是这样子的回调,http.HandleFunc("/", payloadHandler), 只是把一个 http 请求塞进 chan chan Job 里,可能并没有等到处理就返回给请求 200 了。

func payloadHandler(w http.ResponseWriter, r *http.Request) {

    if r.Method != "POST" {
        w.WriteHeader( http.StatusMethodNotAllowed)
        return
    }

    // Read the body into a string for json decoding
    var content = &PayloadCollection{}
    err := json.NewDecoder(io.LimitReader(r.Body, MaxLength)).Decode(&content)
    if err != nil {
        w.Header().Set("Content-Type", "application/json; charset=UTF-8")
        w.WriteHeader( http.StatusBadRequest)
        return
    }

    // Go through each payload and queue items individually to be posted to S3
    for _, payload := range content.Payloads {

        // let's create a job with the payload
        work := Job{Payload: payload}

        // Push the work onto the queue.
        JobQueue <- work
    }

    w.WriteHeader( http.StatusOK)
}

到了从双层队通道取 Job 时候,处理的结果并不会通知给 http 请求方,

// Start method starts the run loop for the worker, listening for a quit channel in
// case we need to stop it
func (w Worker) Start() {
    go func() {
        for {
            // register the current worker into the worker queue.
            w.WorkerPool <- w.JobChannel

            select {
            case job := <-w.JobChannel:
                // we have received a work request.
                if err := job.Payload.UploadToS3(); err != nil {
                    log.Errorf("Error uploading to S3: %s", err.Error())
                }

            case <-w.quit:
                // we have received a signal to stop
                return
            }
        }
    }()
}

我是想实现能实时支持的方式,还把w http.ResponseWriter也套进了 Job 里面,想等真正有结果在w.Write。 结果发现接受的不到 Response.Body。应该是返回比处理要早。 所以不知道是不是这种模式 ,只能实现异步。

1110 次点击
所在节点    问与答
2 条回复
gfreezy
2018-09-29 19:25:37 +08:00
原文跟 go 没啥关系啊。收到请求入队列,然后开一大波 worker 处理。只要队列扛得住,不停加 worker 就可以了。跟 go 没啥关系,而且都是 io bound 的,ruby 完全也可以达到这个效果
yph007595
2018-09-30 11:31:34 +08:00
@gfreezy 你没明白楼主的意思,楼主意思是在 worker 里面处理完消息后,还要把消息结果通过 http 返回给调用方。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/493942

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX