小编Tho*_*ort的帖子

请求取消不会传播到云运行容器

当发送到云运行服务的 HTTP 请求被发起服务取消时,取消/关闭的连接不会传播到云运行容器内正在服务的请求。

示例代码:

package main

import (
    "log"
    "net/http"
    "os"
    "time"
)

func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()
        select {
        case <-ctx.Done():
            log.Printf("context cancelled")
        case <-time.After(time.Minute):
            w.Write([]byte("OK"))
            log.Printf("request completed. path=%s", r.URL.Path)
        }
    })
    log.Fatal(http.ListenAndServe(":"+port, nil))
}
Run Code Online (Sandbox Code Playgroud)

在本地运行此代码,发送到http://localhost:8080然后使用 ctrl-c 取消的curl 请求将在日志中显示为“上下文已取消”。与在云运行中部署并取消的服务相同的请求将在 1 分钟后在日志中显示为成功请求。

google-cloud-run

5
推荐指数
1
解决办法
504
查看次数

标签 统计

google-cloud-run ×1