I am attempting to write a simple program that sends a given number of concurrent GET requests per second, so let's say I want to send 10 requests at a frequency of 5 per second. In this case it means we want to send 5 concurrent requests twice for a total of 10 requests.
I have implemented this using channels, but I am not sure if this is the right approach:
Edited
package main
import (
"fmt"
"time"
"net/http"
)
func main() {
transactions := 10
frequency := 5
prepareRequests(frequency, transactions)
}
func prepareRequests(frequency int, transactions int) {
transactionsPerSecond := transactions / frequency
c := make(chan string)
link := "http://google.com"
for i := 0; i < transactionsPerSecond; i++ {
fmt.Println("batch: ", i+1)
for j := 0; j < frequency; j++ {
go sendRequests(link, c)
}
time.Sleep(time.Duration(1) * time.Second)
}
for k :=0; k < frequency; k++ {
fmt.Println(<-c)
}
}
func sendRequests(l string, c chan string) {
_, err := http.Get(l)
if err != nil {
fmt.Println(l, "might be down!")
c <- l
return
}
c <- l
}
Updated go playground
Is this correct? Seems to be working, but I am not sure I am actually sending concurrent GET request through the sub routines.