Documentation ¶
Overview ¶
Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved. Use of this source code is governed by the MIT license. See LICENSE file for details.
Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved. Use of this source code is governed by the MIT license. See LICENSE file for details.
Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved. Use of this source code is governed by the MIT license. See LICENSE file for details.
Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved. Use of this source code is governed by the MIT license. See LICENSE file for details.
Package balancers provides implementations of HTTP load-balancers.
It has two key interfaces: A Balancer is the implementation of a load-balancer that chooses from a set of Connections.
You can e.g. use the balancer from the roundrobin package to rewrite HTTP requests and use URLs from a given set of HTTP connections.
Suppose you have a cluster of two servers (on two different URLs) and you want to load-balance between the two in a round-robin fashion, you can use code like this:
balancer, err := roundrobin.NewBalancerFromURL("https://server1.com", "https://server2.com") ... // Get a HTTP client for the roundrobin balancer. client := balancer.Client() ... client.Get("http://example.com/path1?foo=bar") // will rewrite URL to https://server1.com/path1?foo=bar client.Get("http://example.com/path1?foo=bar") // will rewrite URL to https://server2.com/path1?foo=bar client.Get("http://example.com/path1?foo=bar") // will rewrite URL to https://server1.com/path1?foo=bar client.Get("/path1?foo=bar") // will rewrite URL to https://server2.com/path1?foo=bar
Copyright (c) 2014-2015 Oliver Eilhard. All rights reserved. Use of this source code is governed by the MIT license. See LICENSE file for details.
Most of the code here is taken from the Google OAuth2 client library at https://github.com/golang/oauth2, especially https://github.com/golang/oauth2/blob/master/transport.go.
Index ¶
Constants ¶
const (
// Version is the current version of this package.
Version = "1.0.0"
)
Variables ¶
var ( // UserAgent is sent with all heartbeat requests. UserAgent = "balancers/" + Version + " (" + runtime.GOOS + "-" + runtime.GOARCH + ")" // DefaultHeartbeatDuration is the default time between heartbeat messages. DefaultHeartbeatDuration = 30 * time.Second )
var ErrNoConn = errors.New("no connection")
ErrNoConn must be returned when a Balancer does not find a (non-broken) connection.
Functions ¶
Types ¶
type Balancer ¶
type Balancer interface { // Get returns a connection that can be used for the next request. Get() (Connection, error) // Connections is the list of available connections. Connections() []Connection }
Balancer holds a list of connections to hosts.
type Connection ¶
type Connection interface { // URL to the host. URL() *url.URL // IsBroken must return true if the connection to URL is currently not available. IsBroken() bool }
Connection is a single connection to a host. It is defined by a URL. It also maintains state in the form that a connection can be broken. TODO(oe) Not sure if this abstraction is necessary.
type HttpConnection ¶
HttpConnection is a HTTP connection to a host. It implements the Connection interface and can be used by balancer implementations.
func NewHttpConnection ¶
func NewHttpConnection(url *url.URL) *HttpConnection
NewHttpConnection creates a new HTTP connection to the given URL.
func (*HttpConnection) HeartbeatDuration ¶
func (c *HttpConnection) HeartbeatDuration(d time.Duration) *HttpConnection
HeartbeatDuration sets the duration in which the connection is checked.
func (*HttpConnection) IsBroken ¶
func (c *HttpConnection) IsBroken() bool
IsBroken returns true if the HTTP connection is currently broken.
func (*HttpConnection) URL ¶
func (c *HttpConnection) URL() *url.URL
URL returns the URL of the HTTP connection.
type Transport ¶
type Transport struct { Base http.RoundTripper // contains filtered or unexported fields }
Transport implements a http Transport for a HTTP load balancer.
func (*Transport) CancelRequest ¶
CancelRequest cancels the given request (if canceling is available).