GoProxy
ReverseProxy / LoadBalancer, You can easily extend the load balancing algorithm
How to use?
package main
import (
"./server"
"./server/pxy"
"net/http"
_ "./server/algorithm"
"./server/state"
"./server/setting"
)
func main() {
//Setting Load Balancing Algorithms
//The algorithm is registered in folder ./server/algorithm
setting.Set("random")
//New Node parameter: url.Scheme, url.Host, url.Port
server.NewInstance("http", "47.107.120.140", "1999")
server.NewInstance("http", "localhost", "3000")
http.HandleFunc("/", pxy.NewMultipleHostsReverseProxy)
http.HandleFunc("/state", state.State)
http.ListenAndServe(":9090", nil)
}
extend the load balancing algorithm
Create a new file under the folder Algorithm, and Declare a structure that implements the Balance interface (there is only one Balancing method).
Thats all
Example
package algorithm
import (
"errors"
"math/rand"
"../../server"
)
type RandomBalance struct {}
func init() {server.RegisterBalance("random", &RandomBalance{})}
func (rb *RandomBalance) Balancing() (*server.Instance, int, error) {
if len(server.Ins) == 0 {
return nil, -1, errors.New("No server is running")
}
index := rand.Int() % len(server.Ins)
ins := server.Ins[index]
return ins, index, nil
}
limitations
For now, this load balancer only supports HTTP. If you implement some load balancing algorithms,feel free to submit a PR, I'll be happy to merge it.