作者:施洪宝一. 介绍1.codis使用了go中martini这个web框架martinie github地址: https://github.com/go-martini...2.martini主要是利用go标准库中的net包以及net/http包二. net包1.net包文档官方文档: https://golang.org/pkg/net/中文文档: https://studygolang.com/pkgdoc2.官方文档中已经说明了如何使用net包实现tcp服务端监听ln, err := net.Listen(“tcp”, “:8080”)if err != nil {// handle error}for {conn, err := ln.Accept()if err != nil { // handle error}go handleConnection(conn)}从使用库的一方来看, 接受连接的操作是阻塞式的, 使用方只需按照阻塞式网络编程进行处理即可由于go有自己的协程调度, 在接受网络连接时, 实际使用的是IO多路复用, 具体则是通过sysmon线程进行操作由于go具有垃圾自动回收以及协程, 故而针对每个网络请求, 开启单独协程进行处理三. net/http包对于http服务, go提供了net/http包可以直接使用官方文档: https://golang.org/pkg/net/ht…中文文档: https://studygolang.com/pkgdoc使用示例(net/http包中还有其他方式构建http服务, 具体情况参考官方文档)http.Handle("/foo", fooHandler)http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, “Hello, %q”, html.EscapeString(r.URL.Path))})log.Fatal(http.ListenAndServe(":8080", nil))http.Handle, http.HandleFunc用于向DefaultServeMux注册处理函数http.ListenAndServe用于启动http服务, 当第二个参数为nil时, 表示使用默认的DefaultServeMux处理http请求3.1 源码简介1.go采用1.11.4, 不同的版本实现可能不同, 源码参见src/net/http/server.go2.结构体定义type ServeMux struct {mu sync.RWMutexm map[string]muxEntryhosts bool // whether any patterns contain hostnames}var DefaultServeMux = &defaultServeMuxvar defaultServeMux ServeMux//http请求处理接口type Handler interface {ServeHTTP(ResponseWriter, *Request)}3.函数定义func Handle(pattern string, handler Handler)func HandleFunc(pattern string, handler func(ResponseWriter, *Request))func ListenAndServe(addr string, handler Handler) error4.基本流程通过http.Handle或者http.HandleFunc, 向DefaultServeMux中注册一些url相关的处理函数通过http.ListenAndServe启动web服务客户端发送http请求后, 服务端接受请求, 创建协程处理请求(找到最匹配的url并调用之前设置的处理函数)