I am attempting to make a master program that redirects to servers in a round robin fashion. The master listens at port 9090 in this case and then redirects to either port 9001 or 9000. The idea is to have ports 9000 and 9001 held in a channel that is pulled from and added to by the redirect function. However, in practice it always redirects to 9000 rather than 9001. Is the channel not being updated correctly by the redirect function? or is there something else I am missing. The channel definitely has the values stored as I am successfully redirected from 9090 to 9000 every time.
Redirect function
func redirect(ports chan int) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
channel := <-ports
address := fmt.Sprintf("http://localhost:%d", channel)
ports <- channel
http.Redirect(w, r, address, 301)
}
}
Master Main Function
func main() {
currentChannel := make(chan int, 2)
go startServer("C:/Users", "9000")
go startServer("C:/Users", "9001")
currentChannel <- 9000
currentChannel <- 9001
http.HandleFunc("/", redirect(currentChannel))
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
go startServer just starts a generic file server. I have tested each of them individually and I am able to access both 9000 and 9001 without the redirect with no problems.