先来看一段最基本的http的server例子:
func HandleApp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}
func main() {
s := http.Server{ //拿到一个server结构体,包含了http的超时等相关配置
Addr: ":8081",
ReadHeaderTimeout: 600 * time.Second,
Handler: nil,
}
http.HandleFunc("/app", HandleApp) // 注册路由
s.ListenAndServe()
}
问题:
(1)为什么执行了http.HandleFunc之后,s.ListenAndServe就能拿到相关注册路由?
(2)路由注册之后是怎么在s.ListenAndServe()中进行处理的?
一、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中定义了一个全局变量DefaultServeMux,HandleFunc里的方法最终由DefaultServeMux接手包装成muxEntry里的muxEntry结构体
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这个adapter结构体来实现,把func(ResponseWriter, *Request)包装成了HandlerFunc类型,而HandlerFunc实现了ServerHttp方法,可以充当为muxEntry里的handler。
最后由ServerHttp方法来调用实现最开始传入的HandleApp方法(Handler that calls f)。
问题:
(1)mux.Handle方法里面对HandlerFunc这个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 { // 重复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以"/"结尾,放到es中进行统一处理
mux.es = appendSorted(mux.es, e) // 对pattern的长度进行排序,按长度从大到小
}
if pattern[0] != '/' {
mux.hosts = true // 标记pattern是否以'/'开头
}
}
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
n := len(es)
i := sort.Search(n, func(i int) bool { // sort.Search用到了二分法进行排序
return len(es[i].pattern) < len(e.pattern) // 拿到第一个> 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
}
看注释,可以知道整体的一个注册路由到DefaultServeMux的逻辑
二、ListenAndServe方法实现
现在可以知道路由的传递是由DefaultServeMux这个全局变量来传递的。
问题:
(1)最终的ListenAndServe怎么对pattern进行处理的以及拿到DefaultServeMux这个全局变量怎么处理handler的?
func (srv *Server) ListenAndServe() error {
if srv.shuttingDown() {
return ErrServerClosed
}
addr := srv.Addr
if addr == "" {
addr = ":http"
}
ln, err := net.Listen("tcp", addr) // 启动对端口的监听,最终调用到了socket底层方法并进行了内核系统调用,具体查看相关源码分析
if err != nil {
return err
}
return srv.Serve(ln) // 处理路由入口
}
再来看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 { // 启动循环接收请求
rw, e := l.Accept() // 监听
if e != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok := e.(net.Error); ok && ne.Temporary() { //accept失败,重试机制
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)为每个进来的请求建一个groutine进行处理
}
}
下面来看每个groutine对进来的请求的处理
看到下面的代码:
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" { // 处理跨域的请求
handler = globalOptionsHandler{}
}
handler.ServeHTTP(rw, req)
}
// 关于globalOptionsHandler中的ServeHTTP请求处理
func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
w.Header().Set("Content-Length", "0") // 设置Content-Length为0
if r.ContentLength != 0 { // 请求体的内容不为空
// 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)
// 生成一个读取最多4kB的io.ReadCloser,返回maxBytesReader结构体,
//实现了io.ReadCloser的Read方法,实现的Read方法里最多只能读取4kb的内容,防止攻击和浪费
io.Copy(ioutil.Discard, mb) // 将内容输出到ioutil.Discard丢弃掉
}
}
// 关于DefaultServeMux对应的ServeMux的ServerHTTP方法实现
// 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的实现,对应到用户的业务逻辑处理
}
// 看到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" { // 如果请求方法是connect,作为代理请求进行重定向
// 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) // 最终的实现
}
// 具体的handler处理实现
// 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
}
// 来看看最终的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 { // 从长到短排序后缀带有'/'
if strings.HasPrefix(path, e.pattern) { // 再匹配输入的前缀是否匹配mux.es中的某一个,长的pattern先匹配。eg: 两个路由/app/和/app/bef/,对于输入的 /app/bef/hewhj 优先会匹配到 /app/bef/这个路由上
return e.h, e.pattern
}
}
return nil, ""
}
至此,对于一个基本的http的server代码梳理完毕,包含3部分
(1)路由注册(HandleFunc)
(2)服务器启动(net.Listen监听,Accept无限循环接收请求)
(3)路由规则处理(ListenAndServe)