http package

[TOC]

概述

go 的http包是作为其他web framework的基础,web框架(echo, gin, chi, martini)都基于http拓展而来。但是目前官方网站也没给出很好的例子解释。目前找到比较全面介绍的参考如下: https://cryptic.io/go-http/

http server

四个重要概念

  • http.Handler: 所有设计都基于该接口,路由对应的处理函数,包含*Request 和 RresponseWriter, 处理请求,返回response。
    • 接口: ServeHTTP(ResponseWriter, *Request)
  • http.HandlerFunc:将普通的函数和匿名函数转化成http.Handler接口,快速创建Handler避免每次需要struct实现
  • http.ServeMux httpr的默认路由管理器,匹配请求的endpoint,调用对应的handler,该类型也实现了http.Handler。前面的两个是基础实现,针对不同endpoint做不同的操作,就是ServeMux的功能。 http的路由器可以扩展实现,第三方web framework基于Handler接口,实现与ServeMux相同的功能。ServerMux有如下两个方法,做的事情一样,接受不同的参数:
    • ServerMux. HandleFunc(endpoint, HandlerFunc)
    • ServerMux.Handle(endpoint, Handler)
  • Middleware chains: 基于http.Handle组成一条链。第三方的web framework一般也实现了middleware chains的管理如[Alice]https://github.com/justinas/alice)。中间键的实现的两种方式
    • HandlerFunc: 用一个普通函数接受Handler参数,返回一个HandlerFunc
    • Handler: 用一个struct保存Handler实例,实现Handler接口

timeout

参考: https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/

http package_第1张图片
image.png

  • ReadTimeout: accept到read body或accept 到read header
  • WriteTimeout: read body 到完成write response
  • IdleTimeout: keepalive 保活时间

http client

client

http client 应用层设置。it is safe for concurrent use.

功能

  • headers
  • redirect policy
  • cookies
  • timeout: 请求的超时间,包括:连接,重定向,读取response body

transport

  • 传输与通信层设置
  • http 底层操作
  • it is safe for concurrent use

功能

  • RoundTripper 接口: 执行一系列操作(HTTP transaction),获取response。 该接口必须支持concurrent use.
    • 可以用于mock,返回特定的reponse,用于测试
  • proxies
  • TLS configuration
    • TLSHandshakeTimeout: TLS handshake timeouts
  • keep-alives
    • DisableKeepAlives
    • MaxIdleConnsPerHost: 每个主机最大的keepalive idle 连接. 为0时采用DefaultMaxIdleConnsPerHost(为2)
    • IdleConnTimeout: keepalive 连接保持的时间
  • compression
  • basic auth

Dialer

包含一些有关连接的设置

  • Timeout
  • Deadline
  • KeepAlive:
    • 0:禁止使用KeepAlive
    • >0 : 保持keepalive的时间

client timeout

http package_第2张图片
image.png
  • 注意事项: 没有可以设置发送整个request 的时间限制,server也没有设置发送整个response的时间限制,可以通过Context实现:https://play.golang.org/p/kvqwVd39oai
  • 部分timeout已经随着新版本消失

client middleware

/*
the middleware example of http client
 */
package main

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

// TransportFunc implements the RountTripper interface
type TransportFunc func(*http.Request) (*http.Response, error)

// RoundTrip just calls the original function
func (tf TransportFunc) RoundTrip(r *http.Request) (*http.Response, error) {
    return tf(r)
}

// RoundTripperDecorator is a convenience function to represent our
// middleware inner function
type RoundTripperDecorator func(http.RoundTripper) http.RoundTripper

// Decorate is a helper to wrap all the middleware
// eg: Decorate( &http.Transport, mw1, mw2, mw3)
// mw1 --> mw2 --> ... --> http.RoundTripper
func Decorate(t http.RoundTripper, rts ...RoundTripperDecorator) http.RoundTripper {
    if len(rts) == 0 {
        return t
    }

    lastPos := len(rts) - 1
    decorated := rts[lastPos](t)
    rts[lastPos] = nil
    for i := lastPos - 1; i >= 0; i-- {
        decorated = rts[i](decorated)
    }
    return decorated
}


// Logger is one of our 'middleware' decorators
func Logger(l *log.Logger) RoundTripperDecorator {
    return func(c http.RoundTripper) http.RoundTripper {
        return TransportFunc(func(r *http.Request) (*http.Response, error) {
            fmt.Println("this is logger")
            // start := time.Now()
            // l.Printf("started request to %s at %s", r.URL, start.Format("2006-01-02 15:04:05"))
            resp, err := c.RoundTrip(r)
            // l.Printf("completed request to %s in %s", r.URL, time.Since(start))
            return resp, err
        })
    }
}

// BasicAuth is another of our 'middleware' decorators
func BasicAuth(username, password string) RoundTripperDecorator {
    return func(c http.RoundTripper) http.RoundTripper {
        return TransportFunc(func(r *http.Request) (*http.Response, error) {
            fmt.Println("this is Basic Auth")
            r.SetBasicAuth(username, password)
            resp, err := c.RoundTrip(r)
            return resp, err
        })
    }
}

func main() {

    c := http.Client{}
    t := Decorate(
        &http.Transport{},
        Logger(log.New(os.Stdout, "", 0)),
        BasicAuth("username", "password"),
    )
    c.Transport = t
    resp, err := c.Get("https://www.google.com")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Response code:", resp.StatusCode)
}

best tips for http client

  • 合理设置Client.Timeout(请求超时时间): time.Second * N
  • 合理设置Transport.MaxIdleConnsPerHost

参考:

  • https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
  • https://medium.com/@valyala/net-http-client-has-the-following-additional-limitations-318ac870ce9d

你可能感兴趣的:(http package)