先來(lái)看一段最基本的http的server例子:
func HandleApp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}
func main() {
s := http.Server{ //拿到一個(gè)server結(jié)構(gòu)體,包含了http的超時(shí)等相關(guān)配置
Addr: ":8081",
ReadHeaderTimeout: 600 * time.Second,
Handler: nil,
}
http.HandleFunc("/app", HandleApp) // 注冊(cè)路由
s.ListenAndServe()
}
問(wèn)題:
(1)為什么執(zhí)行了http.HandleFunc之后,s.ListenAndServe就能拿到相關(guān)注冊(cè)路由?
(2)路由注冊(cè)之后是怎么在s.ListenAndServe()中進(jìn)行處理的?
一、HandleFunc源碼
1、DefaultServeMux全局變量
// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
es []muxEntry // slice of entries sorted from longest to shortest.
hosts bool // whether any patterns contain hostnames
}
type muxEntry struct {
h Handler
pattern string
}
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
可以看到server.go中定義了一個(gè)全局變量DefaultServeMux,HandleFunc里的方法最終由DefaultServeMux接手包裝成muxEntry里的muxEntry結(jié)構(gòu)體
2、DefaultServeMux.HandleFunc包裝
// HandleFunc registers the handler function for the given pattern.
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if handler == nil {
panic("http: nil handler")
}
mux.Handle(pattern, HandlerFunc(handler))
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
由HandlerFunc這個(gè)adapter結(jié)構(gòu)體來(lái)實(shí)現(xiàn),把func(ResponseWriter, *Request)包裝成了HandlerFunc類型,而HandlerFunc實(shí)現(xiàn)了ServerHttp方法,可以充當(dāng)為muxEntry里的handler。
最后由ServerHttp方法來(lái)調(diào)用實(shí)現(xiàn)最開始傳入的HandleApp方法(Handler that calls f)。
問(wèn)題:
(1)mux.Handle方法里面對(duì)HandlerFunc這個(gè)handler具體是怎么包裝到DefaultServeMux里的呢?
下面看mux.Handle方法:
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock() // 加鎖,防止線程沖突
defer mux.mu.Unlock()
if pattern == "" {
panic("http: invalid pattern") // pattern不能為空
}
if handler == nil {
panic("http: nil handler")
}
if _, exist := mux.m[pattern]; exist { // 重復(fù)pattern,則panic
panic("http: multiple registrations for " + pattern)
}
if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
e := muxEntry{h: handler, pattern: pattern} // 包裝muxEntry
mux.m[pattern] = e // 放入map
if pattern[len(pattern)-1] == '/' { // 若pattern以"/"結(jié)尾,放到es中進(jìn)行統(tǒng)一處理
mux.es = appendSorted(mux.es, e) // 對(duì)pattern的長(zhǎng)度進(jìn)行排序,按長(zhǎng)度從大到小
}
if pattern[0] != '/' {
mux.hosts = true // 標(biāo)記pattern是否以'/'開頭
}
}
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
n := len(es)
i := sort.Search(n, func(i int) bool { // sort.Search用到了二分法進(jìn)行排序
return len(es[i].pattern) < len(e.pattern) // 拿到第一個(gè)<len(e.pattern)的位置記為I,然后進(jìn)行插入
})
if i == n {
return append(es, e)
}
// we now know that i points at where we want to insert
es = append(es, muxEntry{}) // try to grow the slice in place, any entry works.
copy(es[i+1:], es[i:]) // Move shorter entries down
es[i] = e
return es
}
func Search(n int, f func(int) bool) int { // sort.Search二分法*****
// Define f(-1) == false and f(n) == true.
// Invariant: f(i-1) == false, f(j) == true.
i, j := 0, n
for i < j {
h := int(uint(i+j) >> 1) // avoid overflow when computing h
// i ≤ h < j
if !f(h) {
i = h + 1 // preserves f(i-1) == false
} else {
j = h // preserves f(j) == true
}
}
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
return i
}
看注釋,可以知道整體的一個(gè)注冊(cè)路由到DefaultServeMux的邏輯
二、ListenAndServe方法實(shí)現(xiàn)
現(xiàn)在可以知道路由的傳遞是由DefaultServeMux這個(gè)全局變量來(lái)傳遞的。
問(wèn)題:
(1)最終的ListenAndServe怎么對(duì)pattern進(jìn)行處理的以及拿到DefaultServeMux這個(gè)全局變量怎么處理handler的?
func (srv *Server) ListenAndServe() error {
if srv.shuttingDown() {
return ErrServerClosed
}
addr := srv.Addr
if addr == "" {
addr = ":http"
}
ln, err := net.Listen("tcp", addr) // 啟動(dòng)對(duì)端口的監(jiān)聽,最終調(diào)用到了socket底層方法并進(jìn)行了內(nèi)核系統(tǒng)調(diào)用,具體查看相關(guān)源碼分析
if err != nil {
return err
}
return srv.Serve(ln) // 處理路由入口
}
再來(lái)看srv.Serve(ln):
// Serve accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines read requests and
// then call srv.Handler to reply to them.
func (srv *Server) Serve(l net.Listener) error {
if fn := testHookServerServe; fn != nil {
fn(srv, l) // call hook with unwrapped listener
}
origListener := l
l = &onceCloseListener{Listener: l}
defer l.Close()
if err := srv.setupHTTP2_Serve(); err != nil {
return err
}
if !srv.trackListener(&l, true) {
return ErrServerClosed
}
defer srv.trackListener(&l, false)
var tempDelay time.Duration // how long to sleep on accept failure
baseCtx := context.Background()
if srv.BaseContext != nil {
baseCtx = srv.BaseContext(origListener)
if baseCtx == nil {
panic("BaseContext returned a nil context")
}
}
ctx := context.WithValue(baseCtx, ServerContextKey, srv)
for { // 啟動(dòng)循環(huán)接收請(qǐng)求
rw, e := l.Accept() // 監(jiān)聽
if e != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok := e.(net.Error); ok && ne.Temporary() { //accept失敗,重試機(jī)制
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
time.Sleep(tempDelay)
continue
}
return e
}
connCtx := ctx
if cc := srv.ConnContext; cc != nil {
connCtx = cc(connCtx, rw)
if connCtx == nil {
panic("ConnContext returned nil")
}
}
tempDelay = 0
c := srv.newConn(rw)
c.setState(c.rwc, StateNew)
go c.serve(connCtx) // go c.serve(connCtx)為每個(gè)進(jìn)來(lái)的請(qǐng)求建一個(gè)groutine進(jìn)行處理
}
}
下面來(lái)看每個(gè)groutine對(duì)進(jìn)來(lái)的請(qǐng)求的處理
看到下面的代碼:
func (c *conn) serve(ctx context.Context) {
...
serverHandler{c.server}.ServeHTTP(w, w.req)
....
}
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
handler := sh.srv.Handler
if handler == nil { // server中handler不為空的話,DefaultServeMux就沒用了
handler = DefaultServeMux
}
if req.RequestURI == "*" && req.Method == "OPTIONS" { // 處理跨域的請(qǐng)求
handler = globalOptionsHandler{}
}
handler.ServeHTTP(rw, req)
}
// 關(guān)于globalOptionsHandler中的ServeHTTP請(qǐng)求處理
func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
w.Header().Set("Content-Length", "0") // 設(shè)置Content-Length為0
if r.ContentLength != 0 { // 請(qǐng)求體的內(nèi)容不為空
// Read up to 4KB of OPTIONS body (as mentioned in the
// spec as being reserved for future use), but anything
// over that is considered a waste of server resources
// (or an attack) and we abort and close the connection,
// courtesy of MaxBytesReader's EOF behavior.
mb := MaxBytesReader(w, r.Body, 4<<10)
// 生成一個(gè)讀取最多4kB的io.ReadCloser,返回maxBytesReader結(jié)構(gòu)體,
//實(shí)現(xiàn)了io.ReadCloser的Read方法,實(shí)現(xiàn)的Read方法里最多只能讀取4kb的內(nèi)容,防止攻擊和浪費(fèi)
io.Copy(ioutil.Discard, mb) // 將內(nèi)容輸出到ioutil.Discard丟棄掉
}
}
// 關(guān)于DefaultServeMux對(duì)應(yīng)的ServeMux的ServerHTTP方法實(shí)現(xiàn)
// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
if r.RequestURI == "*" {
if r.ProtoAtLeast(1, 1) {
w.Header().Set("Connection", "close")
}
w.WriteHeader(StatusBadRequest)
return
}
h, _ := mux.Handler(r) // handler的路由處理
h.ServeHTTP(w, r) // ServeHTTP的實(shí)現(xiàn),對(duì)應(yīng)到用戶的業(yè)務(wù)邏輯處理
}
// 看到mux.Handler(r):
// Handler returns the handler to use for the given request,
// consulting r.Method, r.Host, and r.URL.Path. It always returns
// a non-nil handler. If the path is not in its canonical form, the
// handler will be an internally-generated handler that redirects
// to the canonical path. If the host contains a port, it is ignored
// when matching handlers.
//
// The path and host are used unchanged for CONNECT requests.
//
// Handler also returns the registered pattern that matches the
// request or, in the case of internally-generated redirects,
// the pattern that will match after following the redirect.
//
// If there is no registered handler that applies to the request,
// Handler returns a ``page not found'' handler and an empty pattern.
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
// CONNECT requests are not canonicalized.
if r.Method == "CONNECT" { // 如果請(qǐng)求方法是connect,作為代理請(qǐng)求進(jìn)行重定向
// If r.URL.Path is /tree and its handler is not registered,
// the /tree -> /tree/ redirect applies to CONNECT requests
// but the path canonicalization does not.
if u, ok := mux.redirectToPathSlash(r.URL.Host, r.URL.Path, r.URL); ok {
return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
}
return mux.handler(r.Host, r.URL.Path)
}
// All other requests have any port stripped and path cleaned
// before passing to mux.handler.
host := stripHostPort(r.Host)
path := cleanPath(r.URL.Path)
// If the given path is /tree and its handler is not registered,
// redirect for /tree/.
if u, ok := mux.redirectToPathSlash(host, path, r.URL); ok {
return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
}
if path != r.URL.Path {
_, pattern = mux.handler(host, path)
url := *r.URL
url.Path = path
return RedirectHandler(url.String(), StatusMovedPermanently), pattern
}
return mux.handler(host, r.URL.Path) // 最終的實(shí)現(xiàn)
}
// 具體的handler處理實(shí)現(xiàn)
// handler is the main implementation of Handler.
// The path is known to be in canonical form, except for CONNECT methods.
func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
mux.mu.RLock()
defer mux.mu.RUnlock()
// Host-specific pattern takes precedence over generic ones
if mux.hosts {
h, pattern = mux.match(host + path)
}
if h == nil {
h, pattern = mux.match(path)
}
if h == nil {
h, pattern = NotFoundHandler(), ""
}
return
}
// 來(lái)看看最終的match路由匹配
// Find a handler on a handler map given a path string.
// Most-specific (longest) pattern wins.
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
// Check for exact match first.
v, ok := mux.m[path] // mux.m含有所有的路徑,先匹配字典中是否含有路徑,有的話直接返回
if ok {
return v.h, v.pattern
}
// Check for longest valid match. mux.es contains all patterns
// that end in / sorted from longest to shortest.
for _, e := range mux.es { // 從長(zhǎng)到短排序后綴帶有'/'
if strings.HasPrefix(path, e.pattern) { // 再匹配輸入的前綴是否匹配mux.es中的某一個(gè),長(zhǎng)的pattern先匹配。eg: 兩個(gè)路由/app/和/app/bef/,對(duì)于輸入的 /app/bef/hewhj 優(yōu)先會(huì)匹配到 /app/bef/這個(gè)路由上
return e.h, e.pattern
}
}
return nil, ""
}
至此,對(duì)于一個(gè)基本的http的server代碼梳理完畢,包含3部分
(1)路由注冊(cè)(HandleFunc)
(2)服務(wù)器啟動(dòng)(net.Listen監(jiān)聽,Accept無(wú)限循環(huán)接收請(qǐng)求)
(3)路由規(guī)則處理(ListenAndServe)