| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A flexible HTTP reverse proxy library for Go with pluggable load balancing strategies. Inspired by go-kratos.
go get -u github.com/omalloc/proxyHere is a simple example of how to use the proxy client with a specific selector and initial nodes.
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"github.com/omalloc/proxy"
"github.com/omalloc/proxy/selector"
"github.com/omalloc/proxy/selector/random"
)
func main() {
// Define backend nodes
node1 := selector.NewNode("http", "127.0.0.1:8081", nil)
node2 := selector.NewNode("http", "127.0.0.1:8082", nil)
// Initialize proxy with Random selector
proxyClient := proxy.New(
proxy.WithSelector(random.NewBuilder()),
proxy.WithInitialNodes([]selector.Node{node1, node2}),
)
// Create a request
targetURL := "http://example.com/api/v1/resource"
req, _ := http.NewRequest(http.MethodGet, targetURL, nil)
// Execute request via proxy
resp, err := proxyClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Dump response
info, _ := httputil.DumpResponse(resp, false)
log.Printf("Response Info: \n%v", string(info))
}The library supports multiple load balancing algorithms located in the selector package and its subdirectories:
To use a different selector, pass it to proxy.WithSelector():
import "github.com/omalloc/proxy/selector/wrr"
// ...
proxyClient := proxy.New(
proxy.WithSelector(wrr.NewBuilder()),
// ...
)MIT
| Back | FazBrowse Home | New Git URL |