原文链接:http://www.codeceo.com/article/go-http-server-code.html
Go语言中HTTP Server:
HTTP server,顾名思义,支持http协议的服务器,HTTP是一个简单的请求-响应协议,通常运行在TCP之上。通过客户端发送请求给服务器得到对应的响应。
HTTP服务简单实现
package mainimport ( "fmt" "net/http")//③处理请求,返回结果func Hello(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "hello world") }func main() { //①路由注册 http.HandleFunc("/", Hello) //②服务监听 http.ListenAndServe(":8080", nil) }
你以为这样就结束了吗,不才刚刚开始。
源码分析
①路由注册
http中的HandleFunc方法,主要用来注册路由
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { DefaultServeMux.HandleFunc(pattern, handler) }
DefaultServeMux是什么?
DefaultServeMux是ServeMux的一个实例。
ServeMux又是什么?
// DefaultServeMux is the default ServeMux used by Serve.var DefaultServeMux = &defaultServeMuxvar defaultServeMux ServeMux type ServeMux struct { mu sync.RWMutex m map[string]muxEntry hosts bool } type muxEntry struct { explicit bool h Handler pattern string}
ServeMux主要通过map[string]muxEntry,来存储了具体的url模式和handler(此handler是实现Handler接口的类型)。通过实现Handler的ServeHTTP方法,来匹配路由(这一点下面源码会讲到)
很多地方都涉及到了Handler,那么Handler是什么?
type Handler interface { ServeHTTP(ResponseWriter, *Request) }
此接口可以算是HTTP Server一个枢纽
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { mux.Handle(pattern, HandlerFunc(handler)) } type HandlerFunc func(ResponseWriter, *Request)func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) }
从代码中可以看出HandlerFunc是一个函数类型,并实现了Handler接口。当通过调用HandleFunc(),把Hello强转为HandlerFunc类型时,就意味着 Hello函数也实现ServeHTTP方法。
ServeMux的Handle方法:
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 mux.m[pattern].explicit { panic("http: multiple registrations for " + pattern) } if mux.m == nil { mux.m = make(map[string]muxEntry) } //把handler和pattern模式绑定到 //map[string]muxEntry的map上 mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern} if pattern[0] != '/' { mux.hosts = true } //这里是绑定静态目录,不作为本片重点。 n := len(pattern) if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit { path := pattern if pattern[0] != '/' { path = pattern[strings.Index(pattern, "/"):] } url := &url.URL{Path: path} mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(url.String(), StatusMovedPermanently), pattern: pattern} } }
上面的流程就完成了路由注册。
②服务监听
type Server struct { Addr string Handler Handler ReadTimeout time.Duration WriteTimeout time.Duration TLSConfig *tls.Config MaxHeaderBytes int TLSNextProto map[string]func(*Server, *tls.Conn, Handler) ConnState func(net.Conn, ConnState) ErrorLog *log.Logger disableKeepAlives int32 nextProtoOnce sync.Once nextProtoErr error }func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler} return server.ListenAndServe() }//初始化监听地址Addr,同时调用Listen方法设置监听。//最后将监听的TCP对象传入Serve方法:func (srv *Server) ListenAndServe() error { addr := srv.Addr if addr == "" { addr = ":http" } ln, err := net.Listen("tcp", addr) if err != nil { return err } return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}) }
Serve(l net.Listener)为每个请求开启goroutine的设计,保证了go的高并发。
func (srv *Server) Serve(l net.Listener) error { defer l.Close() if fn := testHookServerServe; fn != nil { fn(srv, l) } var tempDelay time.Duration // how long to sleep on accept failure if err := srv.setupHTTP2_Serve(); err != nil { return err } srv.trackListener(l, true) defer srv.trackListener(l, false) baseCtx := context.Background() // base is always background, per Issue 16220 ctx := context.WithValue(baseCtx, ServerContextKey, srv) ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr()) //开启循环进行监听 for { //通过Listener的Accept方法用来获取连接数据 rw, e := l.Accept() if e != nil { select { case <-srv.getDoneChan(): return ErrServerClosed default: } if ne, ok := e.(net.Error); ok && ne.Temporary() { 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 } tempDelay = 0 //通过获得的连接数据,创建newConn连接对象 c := srv.newConn(rw) c.setState(c.rwc, StateNew) // before Serve can return //开启goroutine发送连接请求 go c.serve(ctx) } }
serve()为核心,读取对应的连接数据进行分配
func (c *conn) serve(ctx context.Context) { c.remoteAddr = c.rwc.RemoteAddr().String() //连接关闭相关的处理 defer func() { if err := recover(); err != nil && err != ErrAbortHandler { const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf) } if !c.hijacked() { c.close() c.setState(c.rwc, StateClosed) } }() ..... ctx, cancelCtx := context.WithCancel(ctx) c.cancelCtx = cancelCtx defer cancelCtx() c.r = &connReader{conn: c} c.bufr = newBufioReader(c.r) c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10) for { //读取客户端的请求 w, err := c.readRequest(ctx) if c.r.remain != c.server.initialReadLimitSize() { // If we read any bytes off the wire, we're active. c.setState(c.rwc, StateActive) } ................. //处理网络数据的状态 // Expect 100 Continue support req := w.req if req.expectsContinue() { if req.ProtoAtLeast(1, 1) && req.ContentLength != 0 { // Wrap the Body reader with one that replies on the connection req.Body = &expectContinueReader{readCloser: req.Body, resp: w} } } else if req.Header.get("Expect") != "" { w.sendExpectationFailed() return } c.curReq.Store(w) if requestBodyRemains(req.Body) { registerOnHitEOF(req.Body, w.conn.r.startBackgroundRead) } else { if w.conn.bufr.Buffered() > 0 { w.conn.r.closeNotifyFromPipelinedRequest() } w.conn.r.startBackgroundRead() } //调用serverHandler{c.server}.ServeHTTP(w, w.req) //方法处理请求 serverHandler{c.server}.ServeHTTP(w, w.req) w.cancelCtx() if c.hijacked() { return } w.finishRequest() if !w.shouldReuseConnection() { if w.requestBodyLimitHit || w.closedRequestBodyEarly() { c.closeWriteAndWait() } return } c.setState(c.rwc, StateIdle) c.curReq.Store((*response)(nil)) if !w.conn.server.doKeepAlives() { return } if d := c.server.idleTimeout(); d != 0 { c.rwc.SetReadDeadline(time.Now().Add(d)) if _, err := c.bufr.Peek(4); err != nil { return } } c.rwc.SetReadDeadline(time.Time{}) } }
③处理请求,返回结果
serverHandler 主要初始化路由多路复用器。如果server对象没有指定Handler,则使用默认的DefaultServeMux作为路由多路复用器。并调用初始化Handler的ServeHTTP方法。
type serverHandler struct { srv *Server}func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) { handler := sh.srv.Handler if handler == nil { handler = DefaultServeMux } if req.RequestURI == "*" && req.Method == "OPTIONS" { handler = globalOptionsHandler{} } handler.ServeHTTP(rw, req) }
这里就是之前提到的匹配路由的具体代码
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 } //匹配注册到路由上的handler函数 h, _ := mux.Handler(r) //调用handler函数的ServeHTTP方法 //即Hello函数,然后把数据写到http.ResponseWriter //对象中返回给客户端。 h.ServeHTTP(w, r) }func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) { if r.Method != "CONNECT" { if p := cleanPath(r.URL.Path); p != r.URL.Path { _, pattern = mux.handler(r.Host, p) url := *r.URL url.Path = p return RedirectHandler(url.String(), StatusMovedPermanently), pattern } } return mux.handler(r.Host, r.URL.Path) }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 { //如 127.0.0.1/hello h, pattern = mux.match(host + path) } if h == nil { // 如 /hello h, pattern = mux.match(path) } if h == nil { h, pattern = NotFoundHandler(), "" } return}func (mux *ServeMux) match(path string) (h Handler, pattern string) { var n = 0 for k, v := range mux.m { if !pathMatch(k, path) { continue } //通过迭代m寻找出注册路由的patten模式 //与实际url匹配的handler函数并返回。 if h == nil || len(k) > n { n = len(k) h = v.h pattern = v.pattern } } return}func pathMatch(pattern, path string) bool { if len(pattern) == 0 { // should not happen return false } n := len(pattern) //如果注册模式与请求uri一样返回true,否则false if pattern[n-1] != '/' { return pattern == path } //静态文件匹配 return len(path) >= n && path[0:n] == pattern }
将数据写给客户端
//主要代码,通过层层封装才走到这一步func (w checkConnErrorWriter) Write(p []byte) (n int, err error) { n, err = w.c.rwc.Write(p) if err != nil && w.c.werr == nil { w.c.werr = err w.c.cancelCtx() } return}
serverHandler{c.server}.ServeHTTP(w, w.req)当请求结束后,就开始执行连接断开的相关逻辑。
总结
Go语言通过一个ServeMux实现了的路由多路复用器来管理路由。同时提供一个Handler接口提供ServeHTTP方法,实现handler接口的函数,可以处理实际request并返回response。
ServeMux和handler函数的连接桥梁就是Handler接口。ServeMux的ServeHTTP方法实现了寻找注册路由的handler的函数,并调用该handler的ServeHTTP方法。
所以说Handler接口是一个重要枢纽。
简单梳理下整个请求响应过程,如下图
共同学习,写下你的评论
评论加载中...
作者其他优质文章