I am trying to find a way to load balance incoming requests on my Go servers. I have tried nginx, haproxy and some Go repositories on GitHub like:
https://github.com/darkhelmet/balance
that allows you to do layer 4 load balancing through TCP.
I have tried TCP load balancing through haproxy and through the darkhelmet/balance library. It seems like when I do load balancing through TCP, my connections are re used so every request from my PC gets sent to the same server. So it becomes hard for me to do benchmarks with for example the "wrk" benchmarking tool. And also other tests when I need to see if websocket connections on different go apps can communicate through a redis server via pub sub methods and so on.
When I refresh the web browser I can see that all requests are sent to the same server, even tho I have 3 Go servers in the backend.
Someone said this to me: "Disable HTTP connection re-use on your web servers." But I do not know how to do this.
The code I use for my go servers to benchmark is this:
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
func fib(n int) int {
if n == 0 {
return 0
} else if n == 1 {
return 1
} else {
return fib(n-1) + fib(n-2)
}
}
func main() {
router := httprouter.New()
router.GET("/", toHandle(root))
router.GET("/fibonacci/:n", toHandle(fibonacci))
log.Fatal(http.ListenAndServe(":80", router))
}
type errHandle func(http.ResponseWriter, *http.Request, httprouter.Params) error
func toHandle(eh errHandle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if err := eh(w, r, ps); err != nil {
log.Println(err)
}
}
}
func root(w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
w.Write([]byte("To the moon! goapp1"))
return nil
}
func fibonacci(w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
n, err := strconv.Atoi(ps.ByName("n"))
if err != nil {
return err
}
result := fib(n)
w.Write([]byte(fmt.Sprintf("result: %d", result)))
return nil
}
How can I achieve this? To be able to do roundrobin from the same client/ip/machine through tcp load balancing?