• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H15-May-2019-493370

expvarhandler/H15-May-2019-131101

fasthttpadaptor/H15-May-2019-274210

fasthttpproxy/H15-May-2019-2615

fasthttputil/H15-May-2019-1,081876

pprofhandler/H15-May-2019-4537

reuseport/H15-May-2019-224166

stackless/H15-May-2019-475381

testdata/H03-May-2022-

.gitignoreH A D15-May-201927 43

.travis.ymlH A D15-May-2019653 3830

LICENSEH A D15-May-20191.2 KiB2620

README.mdH A D15-May-201928.4 KiB586470

TODOH A D15-May-2019197 54

allocation_test.goH A D15-May-20191.2 KiB7151

args.goH A D15-May-201913.1 KiB589419

args_test.goH A D15-May-201912.5 KiB523427

args_timing_test.goH A D15-May-2019550 3127

bytesconv.goH A D15-May-20199.5 KiB438363

bytesconv_32.goH A D15-May-201980 84

bytesconv_32_test.goH A D15-May-2019871 4031

bytesconv_64.goH A D15-May-201978 84

bytesconv_64_test.goH A D15-May-20191.1 KiB4334

bytesconv_test.goH A D15-May-20197.9 KiB304262

bytesconv_timing_test.goH A D15-May-20193.6 KiB178162

client.goH A D15-May-201958.8 KiB2,2601,303

client_example_test.goH A D15-May-20191 KiB4028

client_test.goH A D15-May-201938 KiB1,7381,489

client_timing_test.goH A D15-May-201915.7 KiB643561

coarseTime.goH A D15-May-2019314 147

coarseTime_test.goH A D15-May-2019638 3834

compress.goH A D15-May-201910.9 KiB439323

compress_test.goH A D15-May-20195.2 KiB217193

cookie.goH A D15-May-201912.5 KiB535386

cookie_test.goH A D15-May-20199.7 KiB359312

cookie_timing_test.goH A D15-May-2019821 3631

doc.goH A D15-May-20191.4 KiB381

fs.goH A D15-May-201932.4 KiB1,272912

fs_example_test.goH A D15-May-2019577 2916

fs_handler_example_test.goH A D15-May-20191.1 KiB4829

fs_test.goH A D15-May-201918 KiB707588

go.modH A D15-May-2019321 108

go.sumH A D15-May-20191 KiB1110

header.goH A D15-May-201955.7 KiB2,2011,578

header_regression_test.goH A D15-May-20192.5 KiB8869

header_test.goH A D15-May-201969.8 KiB2,3161,917

header_timing_test.goH A D15-May-20193.4 KiB149132

http.goH A D15-May-201943.8 KiB1,7921,212

http_test.goH A D15-May-201953.6 KiB1,9241,564

lbclient.goH A D15-May-20194.4 KiB184116

lbclient_example_test.goH A D15-May-2019914 4333

methods.goH A D15-May-2019478 1512

nocopy.goH A D15-May-2019378 124

peripconn.goH A D15-May-20191.7 KiB10187

peripconn_test.goH A D15-May-20191.1 KiB6048

requestctx_setbodystreamwriter_example_test.goH A D15-May-2019742 3324

server.goH A D15-May-201968.8 KiB2,4921,408

server_example_test.goH A D15-May-20195.4 KiB178108

server_test.goH A D15-May-201970.8 KiB2,8972,487

server_timing_test.goH A D15-May-201911.2 KiB462404

ssl-cert-snakeoil.keyH A D15-May-20191.7 KiB2928

ssl-cert-snakeoil.pemH A D15-May-20191,005 1817

status.goH A D15-May-20197.3 KiB177157

stream.goH A D15-May-20191.1 KiB5529

stream_test.goH A D15-May-20192.1 KiB10389

stream_timing_test.goH A D15-May-20191.3 KiB7162

strings.goH A D15-May-20192.9 KiB8173

tcpdialer.goH A D15-May-201912.5 KiB449215

timer.goH A D15-May-20191.2 KiB5536

uri.goH A D15-May-201912.8 KiB526344

uri_test.goH A D15-May-201911.2 KiB343243

uri_timing_test.goH A D15-May-20191.1 KiB5040

uri_unix.goH A D15-May-2019202 137

uri_windows.goH A D15-May-2019213 137

uri_windows_test.goH A D15-May-2019237 136

userdata.goH A D15-May-20191.1 KiB7261

userdata_test.goH A D15-May-20191.4 KiB7557

userdata_timing_test.goH A D15-May-2019950 4944

workerpool.goH A D15-May-20194.7 KiB238183

workerpool_test.goH A D15-May-20193.5 KiB170151

README.md

1[![Build Status](https://travis-ci.org/valyala/fasthttp.svg)](https://travis-ci.org/valyala/fasthttp)
2[![GoDoc](https://godoc.org/github.com/valyala/fasthttp?status.svg)](http://godoc.org/github.com/valyala/fasthttp)
3[![Go Report](https://goreportcard.com/badge/github.com/valyala/fasthttp)](https://goreportcard.com/report/github.com/valyala/fasthttp)
4
5# fasthttp
6Fast HTTP implementation for Go.
7
8Currently fasthttp is successfully used by [VertaMedia](https://vertamedia.com/)
9in a production serving up to 200K rps from more than 1.5M concurrent keep-alive
10connections per physical server.
11
12[TechEmpower Benchmark round 12 results](https://www.techempower.com/benchmarks/#section=data-r12&hw=peak&test=plaintext)
13
14[Server Benchmarks](#http-server-performance-comparison-with-nethttp)
15
16[Client Benchmarks](#http-client-comparison-with-nethttp)
17
18[Install](#install)
19
20[Documentation](https://godoc.org/github.com/valyala/fasthttp)
21
22[Examples from docs](https://godoc.org/github.com/valyala/fasthttp#pkg-examples)
23
24[Code examples](examples)
25
26[Awesome fasthttp tools](https://github.com/fasthttp)
27
28[Switching from net/http to fasthttp](#switching-from-nethttp-to-fasthttp)
29
30[Fasthttp best practices](#fasthttp-best-practices)
31
32[Tricks with byte buffers](#tricks-with-byte-buffers)
33
34[Related projects](#related-projects)
35
36[FAQ](#faq)
37
38# HTTP server performance comparison with [net/http](https://golang.org/pkg/net/http/)
39
40In short, fasthttp server is up to 10 times faster than net/http.
41Below are benchmark results.
42
43*GOMAXPROCS=1*
44
45net/http server:
46```
47$ GOMAXPROCS=1 go test -bench=NetHTTPServerGet -benchmem -benchtime=10s
48BenchmarkNetHTTPServerGet1ReqPerConn                	 1000000	     12052 ns/op	    2297 B/op	      29 allocs/op
49BenchmarkNetHTTPServerGet2ReqPerConn                	 1000000	     12278 ns/op	    2327 B/op	      24 allocs/op
50BenchmarkNetHTTPServerGet10ReqPerConn               	 2000000	      8903 ns/op	    2112 B/op	      19 allocs/op
51BenchmarkNetHTTPServerGet10KReqPerConn              	 2000000	      8451 ns/op	    2058 B/op	      18 allocs/op
52BenchmarkNetHTTPServerGet1ReqPerConn10KClients      	  500000	     26733 ns/op	    3229 B/op	      29 allocs/op
53BenchmarkNetHTTPServerGet2ReqPerConn10KClients      	 1000000	     23351 ns/op	    3211 B/op	      24 allocs/op
54BenchmarkNetHTTPServerGet10ReqPerConn10KClients     	 1000000	     13390 ns/op	    2483 B/op	      19 allocs/op
55BenchmarkNetHTTPServerGet100ReqPerConn10KClients    	 1000000	     13484 ns/op	    2171 B/op	      18 allocs/op
56```
57
58fasthttp server:
59```
60$ GOMAXPROCS=1 go test -bench=kServerGet -benchmem -benchtime=10s
61BenchmarkServerGet1ReqPerConn                       	10000000	      1559 ns/op	       0 B/op	       0 allocs/op
62BenchmarkServerGet2ReqPerConn                       	10000000	      1248 ns/op	       0 B/op	       0 allocs/op
63BenchmarkServerGet10ReqPerConn                      	20000000	       797 ns/op	       0 B/op	       0 allocs/op
64BenchmarkServerGet10KReqPerConn                     	20000000	       716 ns/op	       0 B/op	       0 allocs/op
65BenchmarkServerGet1ReqPerConn10KClients             	10000000	      1974 ns/op	       0 B/op	       0 allocs/op
66BenchmarkServerGet2ReqPerConn10KClients             	10000000	      1352 ns/op	       0 B/op	       0 allocs/op
67BenchmarkServerGet10ReqPerConn10KClients            	20000000	       789 ns/op	       2 B/op	       0 allocs/op
68BenchmarkServerGet100ReqPerConn10KClients           	20000000	       604 ns/op	       0 B/op	       0 allocs/op
69```
70
71*GOMAXPROCS=4*
72
73net/http server:
74```
75$ GOMAXPROCS=4 go test -bench=NetHTTPServerGet -benchmem -benchtime=10s
76BenchmarkNetHTTPServerGet1ReqPerConn-4                  	 3000000	      4529 ns/op	    2389 B/op	      29 allocs/op
77BenchmarkNetHTTPServerGet2ReqPerConn-4                  	 5000000	      3896 ns/op	    2418 B/op	      24 allocs/op
78BenchmarkNetHTTPServerGet10ReqPerConn-4                 	 5000000	      3145 ns/op	    2160 B/op	      19 allocs/op
79BenchmarkNetHTTPServerGet10KReqPerConn-4                	 5000000	      3054 ns/op	    2065 B/op	      18 allocs/op
80BenchmarkNetHTTPServerGet1ReqPerConn10KClients-4        	 1000000	     10321 ns/op	    3710 B/op	      30 allocs/op
81BenchmarkNetHTTPServerGet2ReqPerConn10KClients-4        	 2000000	      7556 ns/op	    3296 B/op	      24 allocs/op
82BenchmarkNetHTTPServerGet10ReqPerConn10KClients-4       	 5000000	      3905 ns/op	    2349 B/op	      19 allocs/op
83BenchmarkNetHTTPServerGet100ReqPerConn10KClients-4      	 5000000	      3435 ns/op	    2130 B/op	      18 allocs/op
84```
85
86fasthttp server:
87```
88$ GOMAXPROCS=4 go test -bench=kServerGet -benchmem -benchtime=10s
89BenchmarkServerGet1ReqPerConn-4                         	10000000	      1141 ns/op	       0 B/op	       0 allocs/op
90BenchmarkServerGet2ReqPerConn-4                         	20000000	       707 ns/op	       0 B/op	       0 allocs/op
91BenchmarkServerGet10ReqPerConn-4                        	30000000	       341 ns/op	       0 B/op	       0 allocs/op
92BenchmarkServerGet10KReqPerConn-4                       	50000000	       310 ns/op	       0 B/op	       0 allocs/op
93BenchmarkServerGet1ReqPerConn10KClients-4               	10000000	      1119 ns/op	       0 B/op	       0 allocs/op
94BenchmarkServerGet2ReqPerConn10KClients-4               	20000000	       644 ns/op	       0 B/op	       0 allocs/op
95BenchmarkServerGet10ReqPerConn10KClients-4              	30000000	       346 ns/op	       0 B/op	       0 allocs/op
96BenchmarkServerGet100ReqPerConn10KClients-4             	50000000	       282 ns/op	       0 B/op	       0 allocs/op
97```
98
99# HTTP client comparison with net/http
100
101In short, fasthttp client is up to 10 times faster than net/http.
102Below are benchmark results.
103
104*GOMAXPROCS=1*
105
106net/http client:
107```
108$ GOMAXPROCS=1 go test -bench='HTTPClient(Do|GetEndToEnd)' -benchmem -benchtime=10s
109BenchmarkNetHTTPClientDoFastServer                  	 1000000	     12567 ns/op	    2616 B/op	      35 allocs/op
110BenchmarkNetHTTPClientGetEndToEnd1TCP               	  200000	     67030 ns/op	    5028 B/op	      56 allocs/op
111BenchmarkNetHTTPClientGetEndToEnd10TCP              	  300000	     51098 ns/op	    5031 B/op	      56 allocs/op
112BenchmarkNetHTTPClientGetEndToEnd100TCP             	  300000	     45096 ns/op	    5026 B/op	      55 allocs/op
113BenchmarkNetHTTPClientGetEndToEnd1Inmemory          	  500000	     24779 ns/op	    5035 B/op	      57 allocs/op
114BenchmarkNetHTTPClientGetEndToEnd10Inmemory         	 1000000	     26425 ns/op	    5035 B/op	      57 allocs/op
115BenchmarkNetHTTPClientGetEndToEnd100Inmemory        	  500000	     28515 ns/op	    5045 B/op	      57 allocs/op
116BenchmarkNetHTTPClientGetEndToEnd1000Inmemory       	  500000	     39511 ns/op	    5096 B/op	      56 allocs/op
117```
118
119fasthttp client:
120```
121$ GOMAXPROCS=1 go test -bench='kClient(Do|GetEndToEnd)' -benchmem -benchtime=10s
122BenchmarkClientDoFastServer                         	20000000	       865 ns/op	       0 B/op	       0 allocs/op
123BenchmarkClientGetEndToEnd1TCP                      	 1000000	     18711 ns/op	       0 B/op	       0 allocs/op
124BenchmarkClientGetEndToEnd10TCP                     	 1000000	     14664 ns/op	       0 B/op	       0 allocs/op
125BenchmarkClientGetEndToEnd100TCP                    	 1000000	     14043 ns/op	       1 B/op	       0 allocs/op
126BenchmarkClientGetEndToEnd1Inmemory                 	 5000000	      3965 ns/op	       0 B/op	       0 allocs/op
127BenchmarkClientGetEndToEnd10Inmemory                	 3000000	      4060 ns/op	       0 B/op	       0 allocs/op
128BenchmarkClientGetEndToEnd100Inmemory               	 5000000	      3396 ns/op	       0 B/op	       0 allocs/op
129BenchmarkClientGetEndToEnd1000Inmemory              	 5000000	      3306 ns/op	       2 B/op	       0 allocs/op
130```
131
132*GOMAXPROCS=4*
133
134net/http client:
135```
136$ GOMAXPROCS=4 go test -bench='HTTPClient(Do|GetEndToEnd)' -benchmem -benchtime=10s
137BenchmarkNetHTTPClientDoFastServer-4                    	 2000000	      8774 ns/op	    2619 B/op	      35 allocs/op
138BenchmarkNetHTTPClientGetEndToEnd1TCP-4                 	  500000	     22951 ns/op	    5047 B/op	      56 allocs/op
139BenchmarkNetHTTPClientGetEndToEnd10TCP-4                	 1000000	     19182 ns/op	    5037 B/op	      55 allocs/op
140BenchmarkNetHTTPClientGetEndToEnd100TCP-4               	 1000000	     16535 ns/op	    5031 B/op	      55 allocs/op
141BenchmarkNetHTTPClientGetEndToEnd1Inmemory-4            	 1000000	     14495 ns/op	    5038 B/op	      56 allocs/op
142BenchmarkNetHTTPClientGetEndToEnd10Inmemory-4           	 1000000	     10237 ns/op	    5034 B/op	      56 allocs/op
143BenchmarkNetHTTPClientGetEndToEnd100Inmemory-4          	 1000000	     10125 ns/op	    5045 B/op	      56 allocs/op
144BenchmarkNetHTTPClientGetEndToEnd1000Inmemory-4         	 1000000	     11132 ns/op	    5136 B/op	      56 allocs/op
145```
146
147fasthttp client:
148```
149$ GOMAXPROCS=4 go test -bench='kClient(Do|GetEndToEnd)' -benchmem -benchtime=10s
150BenchmarkClientDoFastServer-4                           	50000000	       397 ns/op	       0 B/op	       0 allocs/op
151BenchmarkClientGetEndToEnd1TCP-4                        	 2000000	      7388 ns/op	       0 B/op	       0 allocs/op
152BenchmarkClientGetEndToEnd10TCP-4                       	 2000000	      6689 ns/op	       0 B/op	       0 allocs/op
153BenchmarkClientGetEndToEnd100TCP-4                      	 3000000	      4927 ns/op	       1 B/op	       0 allocs/op
154BenchmarkClientGetEndToEnd1Inmemory-4                   	10000000	      1604 ns/op	       0 B/op	       0 allocs/op
155BenchmarkClientGetEndToEnd10Inmemory-4                  	10000000	      1458 ns/op	       0 B/op	       0 allocs/op
156BenchmarkClientGetEndToEnd100Inmemory-4                 	10000000	      1329 ns/op	       0 B/op	       0 allocs/op
157BenchmarkClientGetEndToEnd1000Inmemory-4                	10000000	      1316 ns/op	       5 B/op	       0 allocs/op
158```
159
160
161# Install
162
163```
164go get -u github.com/valyala/fasthttp
165```
166
167
168# Switching from net/http to fasthttp
169
170Unfortunately, fasthttp doesn't provide API identical to net/http.
171See the [FAQ](#faq) for details.
172There is [net/http -> fasthttp handler converter](https://godoc.org/github.com/valyala/fasthttp/fasthttpadaptor),
173but it is better to write fasthttp request handlers by hand in order to use
174all of the fasthttp advantages (especially high performance :) ).
175
176Important points:
177
178* Fasthttp works with [RequestHandler functions](https://godoc.org/github.com/valyala/fasthttp#RequestHandler)
179instead of objects implementing [Handler interface](https://golang.org/pkg/net/http/#Handler).
180Fortunately, it is easy to pass bound struct methods to fasthttp:
181
182  ```go
183  type MyHandler struct {
184  	foobar string
185  }
186
187  // request handler in net/http style, i.e. method bound to MyHandler struct.
188  func (h *MyHandler) HandleFastHTTP(ctx *fasthttp.RequestCtx) {
189  	// notice that we may access MyHandler properties here - see h.foobar.
190  	fmt.Fprintf(ctx, "Hello, world! Requested path is %q. Foobar is %q",
191  		ctx.Path(), h.foobar)
192  }
193
194  // request handler in fasthttp style, i.e. just plain function.
195  func fastHTTPHandler(ctx *fasthttp.RequestCtx) {
196  	fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
197  }
198
199  // pass bound struct method to fasthttp
200  myHandler := &MyHandler{
201  	foobar: "foobar",
202  }
203  fasthttp.ListenAndServe(":8080", myHandler.HandleFastHTTP)
204
205  // pass plain function to fasthttp
206  fasthttp.ListenAndServe(":8081", fastHTTPHandler)
207  ```
208
209* The [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler)
210accepts only one argument - [RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx).
211It contains all the functionality required for http request processing
212and response writing. Below is an example of a simple request handler conversion
213from net/http to fasthttp.
214
215  ```go
216  // net/http request handler
217  requestHandler := func(w http.ResponseWriter, r *http.Request) {
218  	switch r.URL.Path {
219  	case "/foo":
220  		fooHandler(w, r)
221  	case "/bar":
222  		barHandler(w, r)
223  	default:
224  		http.Error(w, "Unsupported path", http.StatusNotFound)
225  	}
226  }
227  ```
228
229  ```go
230  // the corresponding fasthttp request handler
231  requestHandler := func(ctx *fasthttp.RequestCtx) {
232  	switch string(ctx.Path()) {
233  	case "/foo":
234  		fooHandler(ctx)
235  	case "/bar":
236  		barHandler(ctx)
237  	default:
238  		ctx.Error("Unsupported path", fasthttp.StatusNotFound)
239  	}
240  }
241  ```
242
243* Fasthttp allows setting response headers and writing response body
244in an arbitrary order. There is no 'headers first, then body' restriction
245like in net/http. The following code is valid for fasthttp:
246
247  ```go
248  requestHandler := func(ctx *fasthttp.RequestCtx) {
249  	// set some headers and status code first
250  	ctx.SetContentType("foo/bar")
251  	ctx.SetStatusCode(fasthttp.StatusOK)
252
253  	// then write the first part of body
254  	fmt.Fprintf(ctx, "this is the first part of body\n")
255
256  	// then set more headers
257  	ctx.Response.Header.Set("Foo-Bar", "baz")
258
259  	// then write more body
260  	fmt.Fprintf(ctx, "this is the second part of body\n")
261
262  	// then override already written body
263  	ctx.SetBody([]byte("this is completely new body contents"))
264
265  	// then update status code
266  	ctx.SetStatusCode(fasthttp.StatusNotFound)
267
268  	// basically, anything may be updated many times before
269  	// returning from RequestHandler.
270  	//
271  	// Unlike net/http fasthttp doesn't put response to the wire until
272  	// returning from RequestHandler.
273  }
274  ```
275
276* Fasthttp doesn't provide [ServeMux](https://golang.org/pkg/net/http/#ServeMux),
277but there are more powerful third-party routers and web frameworks
278with fasthttp support:
279
280  * [fasthttp-routing](https://github.com/qiangxue/fasthttp-routing)
281  * [fasthttprouter](https://github.com/buaazp/fasthttprouter)
282  * [lu](https://github.com/vincentLiuxiang/lu)
283  * [atreugo](https://github.com/savsgio/atreugo)
284
285  Net/http code with simple ServeMux is trivially converted to fasthttp code:
286
287  ```go
288  // net/http code
289
290  m := &http.ServeMux{}
291  m.HandleFunc("/foo", fooHandlerFunc)
292  m.HandleFunc("/bar", barHandlerFunc)
293  m.Handle("/baz", bazHandler)
294
295  http.ListenAndServe(":80", m)
296  ```
297
298  ```go
299  // the corresponding fasthttp code
300  m := func(ctx *fasthttp.RequestCtx) {
301  	switch string(ctx.Path()) {
302  	case "/foo":
303  		fooHandlerFunc(ctx)
304  	case "/bar":
305  		barHandlerFunc(ctx)
306  	case "/baz":
307  		bazHandler.HandlerFunc(ctx)
308  	default:
309  		ctx.Error("not found", fasthttp.StatusNotFound)
310  	}
311  }
312
313  fasthttp.ListenAndServe(":80", m)
314  ```
315
316* net/http -> fasthttp conversion table:
317
318  * All the pseudocode below assumes w, r and ctx have these types:
319  ```go
320	var (
321		w http.ResponseWriter
322		r *http.Request
323		ctx *fasthttp.RequestCtx
324	)
325  ```
326  * r.Body -> [ctx.PostBody()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.PostBody)
327  * r.URL.Path -> [ctx.Path()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Path)
328  * r.URL -> [ctx.URI()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.URI)
329  * r.Method -> [ctx.Method()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Method)
330  * r.Header -> [ctx.Request.Header](https://godoc.org/github.com/valyala/fasthttp#RequestHeader)
331  * r.Header.Get() -> [ctx.Request.Header.Peek()](https://godoc.org/github.com/valyala/fasthttp#RequestHeader.Peek)
332  * r.Host -> [ctx.Host()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Host)
333  * r.Form -> [ctx.QueryArgs()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.QueryArgs) +
334  [ctx.PostArgs()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.PostArgs)
335  * r.PostForm -> [ctx.PostArgs()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.PostArgs)
336  * r.FormValue() -> [ctx.FormValue()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.FormValue)
337  * r.FormFile() -> [ctx.FormFile()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.FormFile)
338  * r.MultipartForm -> [ctx.MultipartForm()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.MultipartForm)
339  * r.RemoteAddr -> [ctx.RemoteAddr()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.RemoteAddr)
340  * r.RequestURI -> [ctx.RequestURI()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.RequestURI)
341  * r.TLS -> [ctx.IsTLS()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.IsTLS)
342  * r.Cookie() -> [ctx.Request.Header.Cookie()](https://godoc.org/github.com/valyala/fasthttp#RequestHeader.Cookie)
343  * r.Referer() -> [ctx.Referer()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Referer)
344  * r.UserAgent() -> [ctx.UserAgent()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.UserAgent)
345  * w.Header() -> [ctx.Response.Header](https://godoc.org/github.com/valyala/fasthttp#ResponseHeader)
346  * w.Header().Set() -> [ctx.Response.Header.Set()](https://godoc.org/github.com/valyala/fasthttp#ResponseHeader.Set)
347  * w.Header().Set("Content-Type") -> [ctx.SetContentType()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.SetContentType)
348  * w.Header().Set("Set-Cookie") -> [ctx.Response.Header.SetCookie()](https://godoc.org/github.com/valyala/fasthttp#ResponseHeader.SetCookie)
349  * w.Write() -> [ctx.Write()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Write),
350  [ctx.SetBody()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.SetBody),
351  [ctx.SetBodyStream()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.SetBodyStream),
352  [ctx.SetBodyStreamWriter()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.SetBodyStreamWriter)
353  * w.WriteHeader() -> [ctx.SetStatusCode()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.SetStatusCode)
354  * w.(http.Hijacker).Hijack() -> [ctx.Hijack()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Hijack)
355  * http.Error() -> [ctx.Error()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Error)
356  * http.FileServer() -> [fasthttp.FSHandler()](https://godoc.org/github.com/valyala/fasthttp#FSHandler),
357  [fasthttp.FS](https://godoc.org/github.com/valyala/fasthttp#FS)
358  * http.ServeFile() -> [fasthttp.ServeFile()](https://godoc.org/github.com/valyala/fasthttp#ServeFile)
359  * http.Redirect() -> [ctx.Redirect()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Redirect)
360  * http.NotFound() -> [ctx.NotFound()](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.NotFound)
361  * http.StripPrefix() -> [fasthttp.PathRewriteFunc](https://godoc.org/github.com/valyala/fasthttp#PathRewriteFunc)
362
363* *VERY IMPORTANT!* Fasthttp disallows holding references
364to [RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx) or to its'
365members after returning from [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler).
366Otherwise [data races](http://blog.golang.org/race-detector) are inevitable.
367Carefully inspect all the net/http request handlers converted to fasthttp whether
368they retain references to RequestCtx or to its' members after returning.
369RequestCtx provides the following _band aids_ for this case:
370
371  * Wrap RequestHandler into [TimeoutHandler](https://godoc.org/github.com/valyala/fasthttp#TimeoutHandler).
372  * Call [TimeoutError](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.TimeoutError)
373  before returning from RequestHandler if there are references to RequestCtx or to its' members.
374  See [the example](https://godoc.org/github.com/valyala/fasthttp#example-RequestCtx-TimeoutError)
375  for more details.
376
377Use this brilliant tool - [race detector](http://blog.golang.org/race-detector) -
378for detecting and eliminating data races in your program. If you detected
379data race related to fasthttp in your program, then there is high probability
380you forgot calling [TimeoutError](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.TimeoutError)
381before returning from [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler).
382
383* Blind switching from net/http to fasthttp won't give you performance boost.
384While fasthttp is optimized for speed, its' performance may be easily saturated
385by slow [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler).
386So [profile](http://blog.golang.org/profiling-go-programs) and optimize your
387code after switching to fasthttp. For instance, use [quicktemplate](https://github.com/valyala/quicktemplate)
388instead of [html/template](https://golang.org/pkg/html/template/).
389
390* See also [fasthttputil](https://godoc.org/github.com/valyala/fasthttp/fasthttputil),
391[fasthttpadaptor](https://godoc.org/github.com/valyala/fasthttp/fasthttpadaptor) and
392[expvarhandler](https://godoc.org/github.com/valyala/fasthttp/expvarhandler).
393
394
395# Performance optimization tips for multi-core systems
396
397* Use [reuseport](https://godoc.org/github.com/valyala/fasthttp/reuseport) listener.
398* Run a separate server instance per CPU core with GOMAXPROCS=1.
399* Pin each server instance to a separate CPU core using [taskset](http://linux.die.net/man/1/taskset).
400* Ensure the interrupts of multiqueue network card are evenly distributed between CPU cores.
401  See [this article](https://blog.cloudflare.com/how-to-achieve-low-latency/) for details.
402* Use Go 1.6 as it provides some considerable performance improvements.
403
404
405# Fasthttp best practices
406
407* Do not allocate objects and `[]byte` buffers - just reuse them as much
408  as possible. Fasthttp API design encourages this.
409* [sync.Pool](https://golang.org/pkg/sync/#Pool) is your best friend.
410* [Profile your program](http://blog.golang.org/profiling-go-programs)
411  in production.
412  `go tool pprof --alloc_objects your-program mem.pprof` usually gives better
413  insights for optimization opportunities than `go tool pprof your-program cpu.pprof`.
414* Write [tests and benchmarks](https://golang.org/pkg/testing/) for hot paths.
415* Avoid conversion between `[]byte` and `string`, since this may result in memory
416  allocation+copy. Fasthttp API provides functions for both `[]byte` and `string` -
417  use these functions instead of converting manually between `[]byte` and `string`.
418  There are some exceptions - see [this wiki page](https://github.com/golang/go/wiki/CompilerOptimizations#string-and-byte)
419  for more details.
420* Verify your tests and production code under
421  [race detector](https://golang.org/doc/articles/race_detector.html) on a regular basis.
422* Prefer [quicktemplate](https://github.com/valyala/quicktemplate) instead of
423  [html/template](https://golang.org/pkg/html/template/) in your webserver.
424
425
426# Tricks with `[]byte` buffers
427
428The following tricks are used by fasthttp. Use them in your code too.
429
430* Standard Go functions accept nil buffers
431```go
432var (
433	// both buffers are uninitialized
434	dst []byte
435	src []byte
436)
437dst = append(dst, src...)  // is legal if dst is nil and/or src is nil
438copy(dst, src)  // is legal if dst is nil and/or src is nil
439(string(src) == "")  // is true if src is nil
440(len(src) == 0)  // is true if src is nil
441src = src[:0]  // works like a charm with nil src
442
443// this for loop doesn't panic if src is nil
444for i, ch := range src {
445	doSomething(i, ch)
446}
447```
448
449So throw away nil checks for `[]byte` buffers from you code. For example,
450```go
451srcLen := 0
452if src != nil {
453	srcLen = len(src)
454}
455```
456
457becomes
458
459```go
460srcLen := len(src)
461```
462
463* String may be appended to `[]byte` buffer with `append`
464```go
465dst = append(dst, "foobar"...)
466```
467
468* `[]byte` buffer may be extended to its' capacity.
469```go
470buf := make([]byte, 100)
471a := buf[:10]  // len(a) == 10, cap(a) == 100.
472b := a[:100]  // is valid, since cap(a) == 100.
473```
474
475* All fasthttp functions accept nil `[]byte` buffer
476```go
477statusCode, body, err := fasthttp.Get(nil, "http://google.com/")
478uintBuf := fasthttp.AppendUint(nil, 1234)
479```
480
481# Related projects
482
483  * [fasthttp](https://github.com/fasthttp) - various useful
484    helpers for projects based on fasthttp.
485  * [fasthttp-routing](https://github.com/qiangxue/fasthttp-routing) - fast and
486    powerful routing package for fasthttp servers.
487  * [fasthttprouter](https://github.com/buaazp/fasthttprouter) - a high
488    performance fasthttp request router that scales well.
489  * [gramework](https://github.com/gramework/gramework) - a web framework made by one of fasthttp maintainers
490  * [lu](https://github.com/vincentLiuxiang/lu) - a high performance
491    go middleware web framework which is based on fasthttp.
492  * [websocket](https://github.com/fasthttp/websocket) - Gorilla-based
493    websocket implementation for fasthttp.
494  * [fasthttpsession](https://github.com/phachon/fasthttpsession) - a fast and powerful session package for fasthttp servers.
495  * [atreugo](https://github.com/savsgio/atreugo) - Micro-framework to make simple the use of routing and middlewares.
496  * [kratgo](https://github.com/savsgio/kratgo) - Simple, lightweight and ultra-fast HTTP Cache to speed up your websites.
497
498
499# FAQ
500
501* *Why creating yet another http package instead of optimizing net/http?*
502
503  Because net/http API limits many optimization opportunities.
504  For example:
505  * net/http Request object lifetime isn't limited by request handler execution
506    time. So the server must create a new request object per each request instead
507    of reusing existing objects like fasthttp does.
508  * net/http headers are stored in a `map[string][]string`. So the server
509    must parse all the headers, convert them from `[]byte` to `string` and put
510    them into the map before calling user-provided request handler.
511    This all requires unnecessary memory allocations avoided by fasthttp.
512  * net/http client API requires creating a new response object per each request.
513
514* *Why fasthttp API is incompatible with net/http?*
515
516  Because net/http API limits many optimization opportunities. See the answer
517  above for more details. Also certain net/http API parts are suboptimal
518  for use:
519  * Compare [net/http connection hijacking](https://golang.org/pkg/net/http/#Hijacker)
520    to [fasthttp connection hijacking](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Hijack).
521  * Compare [net/http Request.Body reading](https://golang.org/pkg/net/http/#Request)
522    to [fasthttp request body reading](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.PostBody).
523
524* *Why fasthttp doesn't support HTTP/2.0 and WebSockets?*
525
526  [HTTP/2.0 support](https://github.com/fasthttp/http2) is in progress. [WebSockets](https://github.com/fasthttp/websockets) has been done already.
527  Third parties also may use [RequestCtx.Hijack](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.Hijack)
528  for implementing these goodies.
529
530* *Are there known net/http advantages comparing to fasthttp?*
531
532  Yes:
533  * net/http supports [HTTP/2.0 starting from go1.6](https://http2.golang.org/).
534  * net/http API is stable, while fasthttp API constantly evolves.
535  * net/http handles more HTTP corner cases.
536  * net/http should contain less bugs, since it is used and tested by much
537    wider audience.
538  * net/http works on Go older than 1.5.
539
540* *Why fasthttp API prefers returning `[]byte` instead of `string`?*
541
542  Because `[]byte` to `string` conversion isn't free - it requires memory
543  allocation and copy. Feel free wrapping returned `[]byte` result into
544  `string()` if you prefer working with strings instead of byte slices.
545  But be aware that this has non-zero overhead.
546
547* *Which GO versions are supported by fasthttp?*
548
549  Go1.5+. Older versions won't be supported, since their standard package
550  [miss useful functions](https://github.com/valyala/fasthttp/issues/5).
551
552  **NOTE**: Go 1.9.7 is the oldest tested version. We recommend you to update as soon as you can. As of 1.11.3 we will drop 1.9.x support.
553
554* *Please provide real benchmark data and server information*
555
556  See [this issue](https://github.com/valyala/fasthttp/issues/4).
557
558* *Are there plans to add request routing to fasthttp?*
559
560  There are no plans to add request routing into fasthttp.
561  Use third-party routers and web frameworks with fasthttp support:
562
563    * [fasthttp-routing](https://github.com/qiangxue/fasthttp-routing)
564    * [fasthttprouter](https://github.com/buaazp/fasthttprouter)
565    * [gramework](https://github.com/gramework/gramework)
566    * [lu](https://github.com/vincentLiuxiang/lu)
567    * [atreugo](https://github.com/savsgio/atreugo)
568
569  See also [this issue](https://github.com/valyala/fasthttp/issues/9) for more info.
570
571* *I detected data race in fasthttp!*
572
573  Cool! [File a bug](https://github.com/valyala/fasthttp/issues/new). But before
574  doing this check the following in your code:
575
576  * Make sure there are no references to [RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx)
577  or to its' members after returning from [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler).
578  * Make sure you call [TimeoutError](https://godoc.org/github.com/valyala/fasthttp#RequestCtx.TimeoutError)
579  before returning from [RequestHandler](https://godoc.org/github.com/valyala/fasthttp#RequestHandler)
580  if there are references to [RequestCtx](https://godoc.org/github.com/valyala/fasthttp#RequestCtx)
581  or to its' members, which may be accessed by other goroutines.
582
583* *I didn't find an answer for my question here*
584
585  Try exploring [these questions](https://github.com/valyala/fasthttp/issues?q=label%3Aquestion).
586