Go语言 在编写http处理程序时,我们是否必须侦听请求上下文取消?

icomxhvb  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(155)

假设我正在编写一个http处理程序,在返回响应之前执行其他操作,我是否必须设置一个侦听器来检查http请求上下文是否已被取消?以便它可以立即返回,或者是否有其他方法在请求上下文取消时退出处理程序?

func handleSomething(w http.ResponseWriter, r *http.Request) {
    done := make(chan error)

    go func() {
        if err := doSomething(r.Context()); err != nil {
            done <- err
                        return
        }

        done <- nil
    }()

    select {
    case <-r.Context().Done():
        http.Error(w, r.Context().Err().Error(), http.StatusInternalServerError)
        return
    case err := <-done:
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        w.WriteHeader(http.StatusOK)
        w.Write([]byte("ok"))
    }
}

func doSomething(ctx context.Context) error {
    // simulate doing something for 1 second.
    time.Sleep(time.Second)
    return nil
}

我试着对它进行测试,但是在上下文被取消后,doSomething函数没有停止,仍然在后台运行。

func TestHandler(t *testing.T) {
    mux := http.NewServeMux()
    mux.HandleFunc("/something", handleSomething)

    srv := http.Server{
        Addr:    ":8989",
        Handler: mux,
    }

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        if err := srv.ListenAndServe(); err != nil {
            log.Println(err)
        }
    }()

    time.Sleep(time.Second)

    req, err := http.NewRequest(http.MethodGet, "http://localhost:8989/something", nil)
    if err != nil {
        t.Fatal(err)
    }

    cl := http.Client{
        Timeout: 3 * time.Second,
    }

    res, err := cl.Do(req)
    if err != nil {
        t.Logf("error: %s", err.Error())
    } else {
        t.Logf("request is done with status code %d", res.StatusCode)
    }

    go func() {
        <-time.After(10 * time.Second)
        shutdown, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()

        srv.Shutdown(shutdown)
    }()

    wg.Wait()
}

func handleSomething(w http.ResponseWriter, r *http.Request) {
    done := make(chan error)

    go func() {
        if err := doSomething(r.Context()); err != nil {
            log.Println(err)
            done <- err
        }

        done <- nil
    }()

    select {
    case <-r.Context().Done():
        log.Println("context is done!")
        return
    case err := <-done:
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        w.WriteHeader(http.StatusOK)
        w.Write([]byte("ok"))
    }
}

func doSomething(ctx context.Context) error {
    return runInContext(ctx, func() {
        log.Println("doing something")
        defer log.Println("done doing something")

        time.Sleep(10 * time.Second)
    })
}

func runInContext(ctx context.Context, fn func()) error {
    ch := make(chan struct{})
    go func() {
        defer close(ch)
        fn()
    }()

    select {
    case <-ctx.Done():
        return ctx.Err()
    case <-ch:
        return nil
    }
}
b1payxdu

b1payxdu1#

我刚刚对提供的解决方案进行了一点重构,现在它应该可以工作了。

doSomething函数
func doSomething(ctx context.Context) error {
    fmt.Printf("%v - doSomething: start\n", time.Now())
    select {
    case <-ctx.Done():
        fmt.Printf("%v - doSomething: cancelled\n", time.Now())
        return ctx.Err()
    case <-time.After(3 * time.Second):
        fmt.Printf("%v - doSomething: processed\n", time.Now())
        return nil
    }
}

它等待取消输入,或者在延迟3秒后返回调用方。它接受要侦听的上下文。

handleSomething函数
func handleSomething(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()

    fmt.Printf("%v - handleRequestCtx: start\n", time.Now())

    done := make(chan error)
    go func() {
        if err := doSomething(ctx); err != nil {
            fmt.Printf("%v - handleRequestCtx: error %v\n", time.Now(), err)
            done <- err
        }

        done <- nil
    }()

    select {
    case <-ctx.Done():
        fmt.Printf("%v - handleRequestCtx: cancelled\n", time.Now())
        return
    case err := <-done:
        if err != nil {
            fmt.Printf("%v - handleRequestCtx: error: %v\n", time.Now(), err)
            w.WriteHeader(http.StatusInternalServerError)
            return
        }
        fmt.Printf("%v - handleRequestCtx: processed\n", time.Now())
    }
}

这里的逻辑与您的非常相似。在select中,我们检查接收到的错误是否为nil,并基于此向调用者返回正确的HTTP状态代码。如果我们接收到取消输入,则取消所有上下文链。

TestHandler函数
func TestHandler(t *testing.T) {
    r := mux.NewRouter()
    r.HandleFunc("/demo", handleSomething)

    srv := http.Server{
        Addr:    ":8000",
        Handler: r,
    }

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        if err := srv.ListenAndServe(); err != nil {
            fmt.Println(err.Error())
        }
    }()

    ctx := context.Background()
    ctx, cancel := context.WithTimeout(ctx, 1*time.Second) // request canceled
    // ctx, cancel := context.WithTimeout(ctx, 5*time.Second) // request processed
    defer cancel()

    req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8000/demo", nil)

    client := http.Client{}
    res, err := client.Do(req)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Printf("res status code: %d\n", res.StatusCode)
    }
    srv.Shutdown(ctx)

    wg.Wait()
}

这里,我们启动一个HTTP服务器,并通过http.Client向它发出一个HTTP请求。您可以看到有两个语句用于设置上下文超时。如果您使用带有注解// request canceled的语句,则所有操作都将被取消,否则,如果您使用另一个语句,则请求将被处理。
我希望这澄清了你的问题!

相关问题