Golang http之server源码详解

       http 是典型的 C/S 架构,客户端向服务端发送请求(request),服务端做出应答(response)。HTTP server–简而言之就是一个支持http协议的服务,http是一个相对简单的请求响应的协议,通常是运行在TCP连接之上, 通过客户端发送请求到服务端,获取服务端的响应。

我们先看一个简单的http  server例子

    package main

	import (
		"io"
		"net/http"
		"log"
	)

	// hello world, the web server
	func HelloServer(w http.ResponseWriter, req *http.Request) {
		io.WriteString(w, "hello, world!\n")
	}

	func main() {
        //路由注册
		http.HandleFunc("/hello", HelloServer)
        //开启一个http服务
		log.Fatal(http.ListenAndServe(":8086", nil))
	}

当服务器启动后,浏览器访问:http://127.0.0.1:8086/hello

网页上会显示:

以上就是http 服务的一个简单demo,下面我们主要从源码来分析http server

 

源码分析

 一、  常用概念

  1.Handler接口

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

   作用:Handler封装了处理客户端的请求逻辑及创建返回响应的逻辑,一个http server需要实现Handler接口。

 

2.Server结构体

//Server定义一个HTTP 服务
type Server struct {
   //监听的TCP地址
   Addr    string  // TCP address to listen on, ":http" if empty
   //要调用的处理函数,如果为nil,则默认用http.DefaultServeMux
   Handler Handler // handler to invoke, http.DefaultServeMux if nil

   // TLSConfig optionally provides a TLS configuration for use
   // by ServeTLS and ListenAndServeTLS. Note that this value is
   // cloned by ServeTLS and ListenAndServeTLS, so it's not
   // possible to modify the configuration with methods like
   // tls.Config.SetSessionTicketKeys. To use
   // SetSessionTicketKeys, use Server.Serve with a TLS Listener
   // instead.
   /*
   TLSConfig可以选择提供TLS  配置 用于使用ServeTLS 和 ListenAndServeTLS
   */
   TLSConfig *tls.Config

   // ReadTimeout is the maximum duration for reading the entire
   // request, including the body.
   //
   // Because ReadTimeout does not let Handlers make per-request
   // decisions on each request body's acceptable deadline or
   // upload rate, most users will prefer to use
   // ReadHeaderTimeout. It is valid to use them both.
   //ReadTimeout读取请求的最长时间,包括正文
   ReadTimeout time.Duration

   // ReadHeaderTimeout is the amount of time allowed to read
   // request headers. The connection's read deadline is reset
   // after reading the headers and the Handler can decide what
   // is considered too slow for the body.
   //ReadHeaderTimeout是允许读取请求头的最长时间
   //读完请求头后会重置超时间,并且Handler可以判断太慢的body
   ReadHeaderTimeout time.Duration

   // WriteTimeout is the maximum duration before timing out
   // writes of the response. It is reset whenever a new
   // request's header is read. Like ReadTimeout, it does not
   // let Handlers make decisions on a per-request basis.
   //WriteTimeout是写入响应之前的最大持续时间。 只要读取新请求的标头,它就会重置。 与ReadTimeout一样,它不允许处理程序根据请求做出决策。
   WriteTimeout time.Duration

   // IdleTimeout is the maximum amount of time to wait for the
   // next request when keep-alives are enabled. If IdleTimeout
   // is zero, the value of ReadTimeout is used. If both are
   // zero, ReadHeaderTimeout is used.
   //当开启keep-alives时,IdleTimeout是等待下个请求的最长时间,如果IdleTimeout是0,则使用ReadTimeout,如果ReadTimeout也是0,则使用ReadHeaderTimeout
   IdleTimeout time.Duration

   // MaxHeaderBytes controls the maximum number of bytes the
   // server will read parsing the request header's keys and
   // values, inc
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值