【每日笔记】【Go学习笔记】2019-01-16 go网络编程

作者:施洪宝
一. 介绍

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/pkgdoc

2.官方文档中已经说明了如何使用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.go
2.结构体定义

type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
hosts bool // whether any patterns contain hostnames
}
var DefaultServeMux = &defaultServeMux
var 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) error

4.基本流程

通过http.Handle或者http.HandleFunc, 向DefaultServeMux中注册一些url相关的处理函数
通过http.ListenAndServe启动web服务
客户端发送http请求后, 服务端接受请求, 创建协程处理请求(找到最匹配的url并调用之前设置的处理函数)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理