-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42206b2
commit 943f913
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module golang-loadbalancer | ||
|
||
go 1.20 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type Server interface { | ||
Address() string | ||
IsAlive() bool | ||
Serve(rw http.ResponseWriter, req *http.Request) | ||
} | ||
|
||
type simpleServer struct { | ||
addr string | ||
proxy *httputil.ReverseProxy | ||
} | ||
|
||
func (s *simpleServer) Address() string { return s.addr } | ||
|
||
func (s *simpleServer) IsAlive() bool { return true } | ||
|
||
func (s *simpleServer) Serve(rw http.ResponseWriter, req *http.Request) { | ||
s.proxy.ServeHTTP(rw, req) | ||
} | ||
|
||
func newSimpleServer(addr string) *simpleServer { | ||
serverUrl, err := url.Parse(addr) | ||
handleErr(err) | ||
return &simpleServer{ | ||
addr: addr, | ||
proxy: httputil.NewSingleHostReverseProxy(serverUrl), | ||
} | ||
} | ||
|
||
type LoadBalancer struct { | ||
port string | ||
roundRobinCount int | ||
servers []Server | ||
} | ||
|
||
func NewLoadBalancer(port string, servers []Server) *LoadBalancer { | ||
return &LoadBalancer{ | ||
port: port, | ||
roundRobinCount: 0, | ||
servers: servers, | ||
} | ||
} | ||
|
||
func handleErr(err error) { | ||
if err != nil { | ||
fmt.Printf("error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func (lb *LoadBalancer) getNextAvailableServer() Server { | ||
server := lb.servers[lb.roundRobinCount%len(lb.servers)] | ||
for !server.IsAlive() { | ||
lb.roundRobinCount++ | ||
server = lb.servers[lb.roundRobinCount%len(lb.servers)] | ||
} | ||
lb.roundRobinCount++ | ||
return server | ||
} | ||
|
||
func (lb *LoadBalancer) serveProxy(rw http.ResponseWriter, req *http.Request) { | ||
targetServer := lb.getNextAvailableServer() | ||
fmt.Printf("forwarding request to address %q\n", targetServer.Address()) | ||
targetServer.Serve(rw, req) | ||
} | ||
|
||
func main() { | ||
servers := []Server{ | ||
newSimpleServer("http://localhost:3001"), | ||
newSimpleServer("http://localhost:3002"), | ||
newSimpleServer("http://localhost:3003"), | ||
} | ||
lb := NewLoadBalancer("3000", servers) | ||
handleRedirect := func(rw http.ResponseWriter, req *http.Request) { | ||
lb.serveProxy(rw, req) | ||
} | ||
http.HandleFunc("/", handleRedirect) | ||
fmt.Printf("serving requests at 'localhost:%s'\n", lb.port) | ||
http.ListenAndServe(":"+lb.port, nil) | ||
} |