Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1642

Go http request, "read: connection reset by peer"

$
0
0

I read the answer in Go http.Get, concurrency, and "Connection reset by peer". I set the MaxConnsPerHost to equal the number of concurrent requests. But the same request comes again, read tcp 127.0.0.1:52517->127.0.0.1:8080: read: connection reset by peer.

Here is my client code:

package mainimport ("fmt""io""net/http""sync""time")func main() {    var wg sync.WaitGroup    for i := 0; i < 400; i++ {        wg.Add(1)        go func(id int) {            defer wg.Done()            url := "http://127.0.0.1:8080/sleep"            rt := http.Transport{                MaxIdleConns:        400,                MaxIdleConnsPerHost: 400,                MaxConnsPerHost:     400,                IdleConnTimeout:     20 * time.Second,                DisableKeepAlives:   false,            }            client := http.Client{                Transport: &rt,            }            resp, err := client.Get(url)            if err != nil {                fmt.Printf("%d Request to [%s] failed, error: %v\n", i, url, err)                return            }            defer resp.Body.Close()            body, err := io.ReadAll(resp.Body)            if err != nil {                fmt.Printf("%d Failed to read response from [%s], error: %v\n", i, url, err)                return            }            fmt.Printf("%d Response from [%s] result: %s\n", i, url, string(body))        }(i)    }    wg.Wait()}

And Here is my server code:

package mainimport ("github.com/gin-gonic/gin""time")func main() {    r := gin.Default()    r.GET("/sleep", func(c *gin.Context) {        time.Sleep(10)        c.JSON(200, gin.H{"message": "Slept for 10 seconds",        })    })    r.Run() // listen and serve on 0.0.0.0:8080}

Can you tell me how to fix it?


Viewing all articles
Browse latest Browse all 1642

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>