ego008
ego008
12232 9 0

fasthttp:一个比官方net/http 快10 倍的http server

Fast HTTP implementation for Go.

Currently fasthttp is successfully used in a production serving up to 1M concurrent keep-alive connections doing 50K qps from a single server.

In short, fasthttp is up to 10 times faster than net/http.

项目地址 https://github.com/valyala/fasthttp

跟标准库比,在下面四个方面得到改进:

net/http 的实现是一个连接新建一个 goroutine;fasthttp 是利用一个 worker 复用 goroutine,减轻 runtime 调度 goroutine 的压力
net/http 解析的请求数据很多放在 map[string]string(http.Header) 或 map[string][]string(http.Request.Form),有不必要的 []byte 到 string 的转换,是可以规避的
net/http 解析 HTTP 请求每次生成新的 *http.Request 和 http.ResponseWriter; fasthttp 解析 HTTP 数据到 *fasthttp.RequestCtx,然后使用 sync.Pool 复用结构实例,减少对象的数量
fasthttp 会延迟解析 HTTP 请求中的数据,尤其是 Body 部分。这样节省了很多不直接操作 Body 的情况的消耗

这家伙写了好多'fast' 系列:

fasthttp: Fast HTTP package for Go
ybc: Fast in-process BLOB cache with persistence support
gorpc: Simple, fast and scalable golang rpc library for high load
goloris: Slowloris for nginx DoS. Written in go
gheap: Fast generalized heap tree algorithms in C++ and C. Provides simultaneous support for D-heap and B-heap.
0

See Also

Nearby


Discussion (9)

youbbs
youbbs 2015-12-04 08:08

这个库最近发展的很捉急,一下收了1171 颗星。

作者在后面给出了go 程序优化的建议。

有人问道:为什么不去优化net/http?
他说:本来想去优化net/http 的,但它的设计思想只能让我重写。

0
小马
小马 2015-12-08 01:53

这个发图片怎么样?

0
小马
小马 2015-12-08 01:55

这个很轻量啊!!!!!!!!!!!!!!!!!!

0
ego008
ego008 2016-02-25 03:34

加上路由的例子

package main

import (
    "fmt"
    "log"

    "github.com/buaazp/fasthttprouter"
    "github.com/valyala/fasthttp"
)

func Index(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
    fmt.Fprint(ctx, "Welcome!n")
}

func Hello(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
    fmt.Fprintf(ctx, "hello, %s!n", ps.ByName("name"))
}

func main() {
    router := fasthttprouter.New()
    router.GET("/", Index)
    router.GET("/hello/:name", Hello)

    log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
}
0
闻讯
闻讯 2016-03-03 02:48

地地道道的

0
ego008
ego008 2016-03-19 15:56

fasthttp-postgresql 在最新一轮跑分中脱颖而出

https://www.techempower.com/benchmarks/
https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Go/fasthttp-postgresql

新出的一个模版引擎也很牛(20x faster than html/template)
https://github.com/valyala/quicktemplate
上面的库会编译为一个go 文件,不能动态载入,如果要动态载入可以用
https://github.com/valyala/fasttemplate 也是这个牛人写的

0
最acg
最acg 2016-03-21 14:18

test

0
d2fan
d2fan 2016-03-23 06:04

test1

0
ego008
ego008 2021-04-27 19:26

本站已经使用 fasthttp 驱动: 《记一下youBBS第三次程序改写》 https://youbbs.org/t/3277

0
Login Topics