溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Golang?HTTP編程源碼分析

發(fā)布時間:2023-02-24 15:32:00 來源:億速云 閱讀:125 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Golang HTTP編程源碼分析”,在日常操作中,相信很多人在Golang HTTP編程源碼分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Golang HTTP編程源碼分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1、網(wǎng)絡(luò)基礎(chǔ)

基本TCP客戶-服務(wù)器程序Socket編程流程如如下圖所示。

TCP服務(wù)器綁定到特定端口并阻塞監(jiān)聽客戶端端連接,

TCP客戶端則通過IP+端口向服務(wù)器發(fā)起請求,客戶-服務(wù)器建立連接之后就能開始進行數(shù)據(jù)傳輸。

Golang?HTTP編程源碼分析

Golang的TCP編程也是基于上述流程的。

2、Golang HTTP編程

2.1 代碼示例

func timeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%v", time.Now().Format(time.RFC3339))
}
 
func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%v", "hello world.")
}
 
func main() {
    // 1. 新建路由解碼器
    h := http.NewServeMux()
    // 2. 路由注冊
    h.HandleFunc("/hello", helloHandler)
    h.HandleFunc("/time", timeHandler)
    // 3. 服務(wù)啟動 阻塞監(jiān)聽
    http.ListenAndServe(":8000", h)
}

運行上述程序,在瀏覽器地址欄分別輸入 http://localhost:8000/hello http://localhost:8000/time 結(jié)果分別如下圖所示。

Golang?HTTP編程源碼分析

Golang?HTTP編程源碼分析

2.2 源碼分析

分析從路由注冊到響應(yīng)用戶請求的流程。

2.2.1 新建解碼器 h := http.NewServeMux()

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
}
// NewServeMux allocates and returns a new ServeMux.
func NewServeMux() *ServeMux { return new(ServeMux) }

Handler是interface,定義如下

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

ServeMux實現(xiàn)了Handler接口。

2.2.2 路由注冊 h.HandleFunc("/hello", helloHandler)

// HandleFunc registers the handler function for the given pattern.
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    ...
    mux.Handle(pattern, HandlerFunc(handler))
}
 
func (mux *ServeMux) Handle(pattern string, handler Handler) {
    ...
    e := muxEntry{h: handler, pattern: pattern}
    mux.m[pattern] = e
    if pattern[len(pattern)-1] == '/' {
        mux.es = appendSorted(mux.es, e)
    }
    ...
}

timeHandler和helloHandler函數(shù)被強制轉(zhuǎn)換為type HandlerFunc func(ResponseWriter, *Request)類型,且實現(xiàn)了Handler接口。

func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

mux.m建立了路由到處理函數(shù)timeHandler和helloHandler的映射。

2.2.3 服務(wù)啟動阻塞監(jiān)聽 http.ListenAndServe(":8000", h)

包裝Server結(jié)構(gòu)體,HTTP使用TCP協(xié)議。

func ListenAndServe(addr string, handler Handler) error {
    server := &Server{Addr: addr, Handler: handler}
    return server.ListenAndServe()
}
func (srv *Server) ListenAndServe() error {
    ...
    ln, err := net.Listen("tcp", addr)
    if err != nil {
        return err
    }
    return srv.Serve(ln)
}

net.Listen封裝了Socket編程的socket,bind,listen的調(diào)用,極大的方便了使用者。

阻塞監(jiān)聽請求,新建goroutine處理每個新請求。

func (srv *Server) Serve(l net.Listener) error {
    ...
    for {
        rw, err := l.Accept()
        ...
        c := srv.newConn(rw)
        c.setState(c.rwc, StateNew, runHooks) // before Serve can return
        go c.serve(connCtx)
    }
}
// Serve a new connection.
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
    ...
    handler.ServeHTTP(rw, req)
}

通過前面的流程推導(dǎo)可知,handler是http.ListenAndServe的第二個參數(shù)ServeMux

// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
    ...
    h, _ := mux.Handler(r) // 通過路由獲取處理函數(shù)
    h.ServeHTTP(w, r)
}

mux.Handler使用mux.m這個map通過請求URL找到對應(yīng)處理函數(shù)的。

h的實際類型為HandlerFunc,根據(jù)2.2.2會調(diào)用到具體函數(shù)timeHandler或者h(yuǎn)elloHandler。

到此,關(guān)于“Golang HTTP編程源碼分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI