feat: add routes loadblancing

This commit is contained in:
2024-11-09 10:59:17 +01:00
parent 1279f82bf9
commit 0f39ead97c
6 changed files with 35 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ import (
"net/url"
"slices"
"strings"
"sync/atomic"
)
// ProxyHandler proxies requests to the backend
@@ -76,8 +77,13 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
r.Header.Set("X-Real-IP", getRealIP(r))
r.Host = targetURL.Host
}
backendURL, _ := url.Parse(proxyRoute.destination)
if len(proxyRoute.backends) > 0 {
// Select the next backend URL
backendURL = getNextBackend(proxyRoute.backends)
}
// Create proxy
proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy := httputil.NewSingleHostReverseProxy(backendURL)
// Rewrite
if proxyRoute.path != "" && proxyRoute.rewrite != "" {
// Rewrite the path
@@ -92,3 +98,10 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
proxy.ServeHTTP(w, r)
}
}
// getNextBackend selects the next backend in a round-robin fashion
func getNextBackend(backendURLs []string) *url.URL {
idx := atomic.AddUint32(&counter, 1) % uint32(len(backendURLs))
backendURL, _ := url.Parse(backendURLs[idx])
return backendURL
}