GoProxy

command module
v0.0.0-...-362e55c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2019 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pxy

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL