向日葵武士

深入理解 Golang HTTP Timeout

2018-02-19  本文已影响0人  liudankinglongl

深入理解 Golang HTTP Timeout

背景

前段时间,线上服务器因为部分微服务提供的 HTTP API 响应慢,而我们没有用正确的姿势来处理 HTTP 超时(当然,还有少量 RPC 超时), 同时我们也没有服务降级策略和容灾机制,导致服务相继挂掉😂。服务降级和容灾需要一段时间的架构改造,但是以正确的姿势使用 HTTP 超时确是马上可以习得的。

超时的本质

所有的 Timeout 都构建于 Golang 提供的 Set[Read|Write]Deadline 原语之上。

服务器超时

server timeout

此外,http.ListenAndServe, http.ListenAndServeTLS and http.Serve 等方法都没有设置超时,且无法设置超时。因此不适合直接用来提供公网服务。正确的姿势是:


package main

import (

"net/http"

"time"

)

func main() {

server := &http.Server{

Addr:        ":8081",

ReadTimeout:  3 * time.Second,

WriteTimeout: 5 * time.Second,

}

http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {

w.Write([]byte("hi"))

})

server.ListenAndServe()

}

客户端超时

client timeout

取消一个 http request 有两种方式:

  1. Request.Cancel

  2. Context (Golang >= 1.7.0)

后一种因为可以传递 parent context, 因此可以做级联 cancel, 效果更佳。代码示例:


ctx, cancel := context.WithCancel(context.TODO())  // or parant context

timer := time.AfterFunc(5*time.Second, func() { 

    cancel()

})

req, err := http.NewRequest("GET", "http://httpbin.org/range/2048?duration=8&chunk_size=256", nil) 

if err != nil { 

    log.Fatal(err)

}

req = req.WithContext(ctx) 

Credits

  1. The complete guide to Go net/http timeouts

  2. Go middleware for net.Conn tracking (Prometheus/trace)

同步自我的博客深入理解 Golang HTTP Timeout

上一篇 下一篇

猜你喜欢

热点阅读