-1

I'm trying to speed up api requests in go. They go slow using the standard "net/http". Does anyone know a method to speed up api requests in go? I'm trying to reach 10k requests per second for a different program.

Example code:

func main() {
    counter := 0
    for {
        resp, err := http.Get("http://google.com")

        if err != nil {
            log.Fatal(err)
        }
        if resp.StatusCode == 200 {
            counter++
            fmt.Print(counter, "\n")
        }
    }
}

Output (with one sec in between):

1
2
3
4 
..
gxzs1337
  • 196
  • 10
  • 2
    In a few minutes.... *"Why am I getting a HTTP 429 Too Many Requests response?"* :) – Cory Kramer Dec 02 '21 at 20:27
  • @CoryKramer Haha no :D – gxzs1337 Dec 02 '21 at 20:29
  • 3
    The more serious response to your question is to make GET requests from multiple threads concurrently [like this](https://stackoverflow.com/questions/40680642/multiple-threads-go-for-http-get) or similar – Cory Kramer Dec 02 '21 at 20:30
  • 4
    Per the docs, you must read and close the response body in order to ensure the connections can be re-used. – JimB Dec 02 '21 at 20:34
  • You are going to need channels and GoRoutines like https://stackoverflow.com/questions/40680642/multiple-threads-go-for-http-get – Gustavo Dec 06 '21 at 03:17

1 Answers1

0

Use fasthttp and goroutine

for {
    go func(){
        resp, err := http.Get("http://google.com")

        if err != nil {
            log.Fatal(err)
        }
        if resp.StatusCode == 200 {
            counter++
            fmt.Print(counter, "\n")
        }
    }
}