dsy19890123 2011-12-27 14:40
浏览 1153
已采纳

Go服务器中并发(同时)HTTP连接的理论最大数量是多少?

What is the upper limit on the number of concurrent HTTP connections that a very simple server implemented in Go can handle?

  • 写回答

1条回答 默认 最新

  • dongwen2794 2011-12-27 15:08
    关注

    The number of concurrent HTTP connections is limited by available memory and by operating system limits.

    In Linux, the soft operating system limits - such as the maximum number of open files - can be printed out and changed by using ulimit.

    In terms of memory, each HTTP connection in a minimal Go HTTP server running on 32-bit Linux consumes 21 KiB of memory (the source code of this server, compilable with Go version 2013-03-23, is below). On 64-bit Linux, the memory consumption can be expected to be higher.

    On a 32-bit system with 1GB of memory available to the server, 21 KiB means that about 50,000 simultaneous connections are possible. This does not include memory consumed by the Linux kernel.

    package main
    
    import (
        "flag"
        "fmt"
        "net/http"
        "os"
        "runtime"
        "sync"
    )
    
    var isClient = flag.Bool("client", false, "Whether to start the HTTP server or the HTTP client")
    var N = flag.Int("n", 1000, "Number of concurrent HTTP requests")
    
    var wait = make(chan byte)
    var counter = 0
    var reachedN = make(chan byte)
    
    func handler(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "some text")
        counter++
        if counter == *N {
            reachedN <- 0
        }
        <-wait // Block this goroutine
    }
    
    func main() {
        flag.Parse()
        if *N <= 0 {
            fmt.Fprintf(os.Stderr, "invalid number of goroutines")
            os.Exit(1)
        }
    
        if *isClient {
            // Initiate N http connections
            var wg sync.WaitGroup
            for i := 0; i < *N; i++ {
                wg.Add(1)
                go func(ii int) {
                    _, err := http.Get("http://127.0.0.1:12345")
                    if err != nil {
                        fmt.Fprintf(os.Stderr, "client %d: %s
    ", ii, err)
                        os.Exit(1)
                    }
                    wg.Done()
                }(i)
            }
            wg.Wait()
        } else {
            runtime.GOMAXPROCS(1) // No concurrency
    
            // Read MemStats
            var m0 runtime.MemStats
            runtime.ReadMemStats(&m0)
    
            go func() {
                <-reachedN // Wait until there are *N concurrent requests
    
                // Read MemStats
                var m1 runtime.MemStats
                runtime.ReadMemStats(&m1)
    
                fmt.Printf("Number of HTTP connections:        %d
    ", *N)
                fmt.Printf("Memory consumption per connection: %.2f bytes
    ", float64(m1.Sys-m0.Sys)/float64(*N))
                os.Exit(1)
            }()
    
            http.HandleFunc("/", handler)
            err := http.ListenAndServe(":12345", nil)
            if err != nil {
            fmt.Fprintf(os.Stderr, "server: %s
    ", err)
                os.Exit(1)
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)