Skip to content
/ mylb Public

⭐ Implementing a Load Balancer in Golang(just a toy) ⭐

License

Notifications You must be signed in to change notification settings

seipan/mylb

Repository files navigation

Last commit Repository Stars Issues Open Issues go testserver-e2e

eyecatch

mylb

⭐ Implementing a Load Balancer in Golang(just a toy) ⭐



Usage

testserver

You can launch servers for load balancer testing. There are a total of four servers.

cd testserver
make up

If you want to restart the server, use the following command.

cd testserver
make re

The four servers to be launched are as follows.

http://localhost:8081
http://localhost:8082
http://localhost:8083
http://localhost:8085
http://localhost:8086
http://localhost:8087
http://localhost:8088
http://localhost:8089

The responses from these APIs are as follows:

{
	"message": "ok",
}

Among these, 8081 and 8082 are set to wait 4 seconds before responding. This represents servers with slow responses.

router.GET("/", func(c *gin.Context) {
		time.Sleep(4 * time.Second)
		c.JSON(200, gin.H{
			"message": "ok",
		})
	})

loadbalancer

There are two types of load balancers in this repository, representing different algorithms. Specifically, there are two types: lc (Least Connections) and lr (Least Response Time).The two types can be changed by modifying the config.json.

{
    "type": "lr"
}

To launch the load balancer, enter the following command. (Don't forget to start the test server beforehand.)

make run

Results

Let's take a look at the actual execution results from here. First, let's take a look at the results for Least Connections.

make run
{"level":"info","msg":"access to endpoint","url":"http://localhost:8081/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8081/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8082/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8083/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8083/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8081/","connections":0}

I think you can see that the server is actually being changed so that the connections become 0.

Next, let's take a look at the results for Least Response Time.

make run
{"level":"info","msg":"access to endpoint","url":"http://localhost:8085/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8085/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8085/","connections":0}
{"level":"info","msg":"access to endpoint","url":"http://localhost:8085/","connections":0}

Now, as explained earlier, you can see that there is no access to 8081 and 8082, which have slower responses.

Reference

Golangでロードバランサーを実装する

Creating a Load Balancer in GO

https://github.com/kasvith/simplelb

https://github.com/leonardo5621/golang-load-balancer

License

Code licensed under the MIT License.