Go, tcp too many open files debug

前端 未结 6 884
有刺的猬
有刺的猬 2020-12-08 20:32

Here\'s a straightforward Go http (tcp) connection test script

func main() {
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *htt         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 20:37

    If you want to run millions of go routines that open/read/close a socket, well you better up your ulimit, or open/read/close the socket and pass the value read in to the go-routine, but I would use a buffered channel to control how many file descriptors you want to be able to open.

    const (
        // this is where you can specify how many maxFileDescriptors
        // you want to allow open
        maxFileDescriptors = 100
    )
    
    func main() {
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintln(w, "Hello, client")
        }))
        defer ts.Close()
        var wg sync.WaitGroup
        maxChan := make(chan bool, maxFileDescriptors)
        for i := 0; i < 1000; i++ {
            maxChan <- true
            wg.Add(1)
            go func(url string, i int, maxChan chan bool, wg *sync.WaitGroup) {
                defer wg.Done()
                defer func(maxChan chan bool) { <-maxChan }(maxChan)
                resp, err := http.Get(url)
                if err != nil {
                    panic(err)
                }
                greeting, err := ioutil.ReadAll(resp.Body)
                if err != nil {
                    panic(err)
                }
                err = resp.Body.Close()
                if err != nil {
                    panic(err)
                }
                fmt.Printf("%d: %s", i, string(greeting))
            }(ts.URL, i, maxChan, &wg)
        }
        wg.Wait()
    }
    

提交回复
热议问题