Comments (4)
Why I asked is because in other libraries (Haskell Warp, Go net/http, C# HttpListener) it was easy to make such workaround for HTTP 1.0.
I understand your position perfectly and I can't insist on anything.
Maybe I'll do it on my own.
I did quick fix of your library. Maybe you'll be interested in benchmark results on my Windows machine.
2 programs were tested with the command:
ab.exe -k -n 1000000 -c 51 http://127.0.0.1:8080/
Standard net/http version:
package main
import (
"net/http"
)
const helloWorldString = "Hello, World!"
var helloWorldBytes = []byte(helloWorldString)
func plaintextHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write(helloWorldBytes)
}
func main() {
http.HandleFunc("/", plaintextHandler)
http.ListenAndServe(":8080", nil)
}
Benchmark result:
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /
Document Length: 13 bytes
Concurrency Level: 51
Time taken for tests: 9.662 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 1000000
Total transferred: 139000000 bytes
HTML transferred: 13000000 bytes
Requests per second: 103493.25 [#/sec] (mean)
Time per request: 0.493 [ms] (mean)
Time per request: 0.010 [ms] (mean, across all concurrent requests)
Transfer rate: 14048.40 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.8 0 13
Waiting: 0 0 0.8 0 13
Total: 0 0 0.8 0 13
Percentage of the requests served within a certain time (ms)
50% 0
66% 1
75% 1
80% 1
90% 1
95% 1
98% 3
99% 4
100% 13 (longest request)
fasthttp version:
package main
import (
"github.com/valyala/fasthttp"
)
const helloWorldString = "Hello, World!"
var helloWorldBytes = []byte(helloWorldString)
func plaintextHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set("Connection", "keep-alive")
ctx.Success("text/plain", helloWorldBytes)
}
func main() {
fasthttp.ListenAndServe(":8080", plaintextHandler)
}
Benchmark result:
Server Software: fasthttp
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /
Document Length: 13 bytes
Concurrency Level: 51
Time taken for tests: 6.162 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 1000000
Total transferred: 157000000 bytes
HTML transferred: 13000000 bytes
Requests per second: 162274.44 [#/sec] (mean)
Time per request: 0.314 [ms] (mean)
Time per request: 0.006 [ms] (mean, across all concurrent requests)
Transfer rate: 24879.97 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.4 0 4
Waiting: 0 0 0.4 0 4
Total: 0 0 0.4 0 5
Percentage of the requests served within a certain time (ms)
50% 0
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 1
100% 5 (longest request)
from fasthttp.
The server deliberately closes connections on non-HTTP/1.1 requests. I have no desire complicating the code by adding support for Connection: keep-alive
hack in HTTP/1.0 requests.
Apache Benchmark (ab) is old and broken. Use something newer like wrk instead.
As for the code, it may be improved in the following way:
package main
import (
"github.com/valyala/fasthttp"
)
const helloWorldString = "Hello, World!"
var helloWorldBytes = []byte(helloWorldString)
func plaintextHandler(ctx *fasthttp.RequestCtx) {
ctx.Success("text/plain", helloWorldBytes)
}
func main() {
fasthttp.ListenAndServe(":8080", plaintextHandler)
}
ctx.Success automatically sets StatusOK status code, sets the given Content-Type
header and automatically calculates and sets Content-Length
so there is no need in ctx.SetStatusCode(fasthttp.StatusOK)
, ctx.Response.Header.SetContentType("text/plain")
and ctx.Response.Header.SetContentLength(helloWorldBytesLen)
.
As for ctx.Response.Header.Set("Connection", "keep-alive")
, this header has no sense for HTTP/1.1, since HTTP/1.1-compliant client and servers must treat all the connections as keep-alive unless explicit Connection: close
header is present.
I'm closing the bug with "won't fix" status.
from fasthttp.
Added support for 'Connection: keep-alive' header for non-http/1.1 protocols.
@dimchansky, could you verify that it works as expected?
from fasthttp.
@valyala Everything works now with Apache Benchmark that uses HTTP 1.0 with Connection: Keep-Alive
header. Good work, thanks!
from fasthttp.
Related Issues (20)
- Question: Add client.MaxResponseBody*Read*Size? HOT 2
- Bug: fasthttp client connect to Vite dev server on localhost, tcp4 127.0.0.1:5173: connect: connection refused HOT 2
- Support for Prometheus HOT 1
- CloseOnShutdown dont work? HOT 3
- how can i use fasthttp bare minimum "net" library only without routing through the http overhead? just pure tcp rpc stuff will do HOT 1
- What is a correct way to create a proper fasthttp.RequestCtx in the unit tests? HOT 1
- Timeout stream response connection does not clear buffer data for re-use HOT 2
- Propagate request error to RetryIfFunc HOT 1
- Expose `timeout` field in Request HOT 1
- NewFastHTTPHandler does not set response status code. HOT 1
- NewFastHTTPHandler does not set response status code HOT 7
- Error when serving connection "x.x.x.x:443"<->"172.56.198.126:18271": EOF HOT 3
- http parsing of the request line - where does that happen? HOT 1
- gnet is going to have tls working on it very soon. was wondering if anyone has tried to port fasthttp to use gnet instead. HOT 1
- Feature request : update and show all examples for doing zero allocation when using fasthttp. 100% zero alloc for each req / resp. HOT 3
- It is not safe to read all stream body to memory without a max size limit. HOT 1
- serving a compress enabled public folder without +w permissions results in a 404 HOT 1
- no free connections available to host HOT 3
- when setting MaxConnsPerIP to value greater than zero, the TLSConnectionState( ) is null on a TLS connection returned from the worker pool. HOT 11
- invalid memory address or nil pointer dereference for firstByteReader'c HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fasthttp.