Documentation ¶
Overview ¶
Package loadbalance defines an http.RoundTripper that load balances requests across a group of hosts.
// select from two servers var client http.Client selector := roundrobin.SelectFrom("localhost:8000", "localhost:8001") client.Transport = loadbalance.With(http.DefaultTransport, selector) // create a new request (to any host) req, _ := http.NewRequest("GET", "http://replaced/foo/bar", nil) // make the request, which will be routed to one of the two servers resp, _ := client.Do(req)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // NoHosts is returned RoundTrip when no hosts have been set. NoHosts = errors.New("No hosts to choose from") )
Functions ¶
func With ¶
func With(another http.RoundTripper, s Selector) http.RoundTripper
With returns an http.RoundTripper that will change the request's host based on the result from the provided Selector and call another RoundTripper.
Example ¶
Send requests using the round-robin algorithm.
package main import ( "fmt" "io" "net/http" "net/http/httptest" "net/url" "os" "github.com/go-goo/http/loadbalance" "github.com/go-goo/http/loadbalance/roundrobin" ) func main() { // example only: create 2 test servers that will return their identifier // and the requested path serverA := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "A: %s", r.URL) })) serverB := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "B: %s", r.URL) })) defer serverA.Close() defer serverB.Close() // example only: extract the host:port from the test server URLs hosts := []string{serverA.URL, serverB.URL} for i, host := range hosts { url, _ := url.Parse(host) hosts[i] = url.Host } // create a new client that will load balance across hosts using the // RoundRobin algorithm. var client http.Client selector := roundrobin.SelectFrom(hosts...) client.Transport = loadbalance.With(http.DefaultTransport, selector) // make multiple requests, which are distributed across A/B hosts keys := "abc" for _, key := range keys { req, _ := http.NewRequest("GET", "http://replace.me/foo/bar?key="+string(key), nil) resp, _ := client.Do(req) io.Copy(os.Stdout, resp.Body) fmt.Println() resp.Body.Close() } }
Output: A: /foo/bar?key=a B: /foo/bar?key=b A: /foo/bar?key=c
Types ¶
type Selector ¶
type Selector interface { // Select the host to send the request to using properties of an http.Request. Select(*http.Request) (host string) // Complete is called once the response is received from the underlying // http.RoundTripper. The host value is the same as what was returned by // Select. This is used in algorithms that require tracking the number // of pending requests. Complete(host string) }
Selector defines an interface capable of choosing a host. Implementations must be safe for concurrent access.
type Transport ¶
type Transport struct { http.RoundTripper Selector }
Transport implements http.Transport that will change the request's host based on the result from a Selector and call the embedded Transport.
Directories ¶
Path | Synopsis |
---|---|
Package hashring implements a loadbalance.Selector that selects hosts using groupcache's consistenthash package.
|
Package hashring implements a loadbalance.Selector that selects hosts using groupcache's consistenthash package. |
Package least implements a loadbalance.Selector that selects hosts using a weighted least connection algorithm.
|
Package least implements a loadbalance.Selector that selects hosts using a weighted least connection algorithm. |
Package random implements a loadbalance.Selector that selects hosts randomly using math/rand.
|
Package random implements a loadbalance.Selector that selects hosts randomly using math/rand. |
Package roundrobin implements a loadbalance.Selector that selects hosts using a round-robin algorithm.
|
Package roundrobin implements a loadbalance.Selector that selects hosts using a round-robin algorithm. |
Package single implements a loadbalance.Selector that uses a single host.
|
Package single implements a loadbalance.Selector that uses a single host. |
Click to show internal directories.
Click to hide internal directories.