Coder Social home page Coder Social logo

Keep-Alive in HTTP 1.0 about fasthttp HOT 4 CLOSED

valyala avatar valyala commented on May 22, 2024
Keep-Alive in HTTP 1.0

from fasthttp.

Comments (4)

dimchansky avatar dimchansky commented on May 22, 2024 1

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.

valyala avatar valyala commented on May 22, 2024

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.

valyala avatar valyala commented on May 22, 2024

Added support for 'Connection: keep-alive' header for non-http/1.1 protocols.
@dimchansky, could you verify that it works as expected?

from fasthttp.

dimchansky avatar dimchansky commented on May 22, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.