balancer

package module
v0.0.0-...-cbe03e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2016 License: MIT Imports: 6 Imported by: 0

README

balancer Build Status GoDoc Coverage Status Go Report Card API

Simple, generic, domain-agnostic balancing algorithms for vinxi.

Currently provides random and roundrobin distribution algorithms. New algorithms may be added in the future.

Installation

go get -u gopkg.in/vinxi/balancer.v0

API

See godoc reference.

Example

Use as vinxi middleware
package main

import (
  "fmt"
  "gopkg.in/vinxi/balancer.v0"
  "gopkg.in/vinxi/forward.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  servers := []string{
    "http://www.nytimes.com",
    "http://www.repubblica.it",
    "http://elpais.com",
  }

  // Create the balancer
  lb := balancer.New(servers...)

  // Use custom handler in case of error
  lb.OnError(func(err error, w http.ResponseWriter, r *http.Request, h http.Handler) {
    fmt.Printf("Error: %s\n", err)
    w.WriteHeader(http.StatusBadGateway)
    w.Write([]byte("Error: " + err.Error()))
  })

  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: 3100})

  vs.Use(lb)

  fw, _ := forward.New(forward.PassHostHeader(true))
  vs.UseFinalHandler(fw)

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}
Round-robin balancing
package main

import (
  "fmt"
  "gopkg.in/vinxi/balancer.v0"
)

func main() {
  servers := []string{
    "http://1.server.com",
    "http://2.server.com",
    "http://3.server.com",
  }

  lb := balancer.NewRoundRobin()

  for i := 0; i < 9; i++ {
    fmt.Println("Next balance round...")
    server, err := lb.Balance(servers)
    if err != nil {
      fmt.Printf("Error: %s\n", err)
      return
    }
    fmt.Printf("Next target server: %s\n", server)
  }
}

License

MIT

Documentation

Overview

Package balancer provides simple well-known distribution algorithms for easy traffic balancing.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBalancer = NewRoundRobin()

DefaultBalancer is the default balancer used by the middleware.

View Source
var DefaultErrorHandler = func(err error, w http.ResponseWriter, r *http.Request, h http.Handler) {
	h.ServeHTTP(w, r)
}

DefaultErrorHandler is used as default error handler, which is a no-op handler.

View Source
var ErrNoServers = errors.New("balancer: server list is empty")

ErrNoServers is returned when no servers are passed to the balancer.

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	// Balance balances the given servers and returns
	// the next server or an error.
	Balance([]string) (string, error)
}

Balancer represents the interface implemented by balancer providers.

type ErrorHandler

type ErrorHandler func(error, http.ResponseWriter, *http.Request, http.Handler)

ErrorHandler represents the required interface implemented by balancer error handlers.

type ProxyBalancer

type ProxyBalancer struct {
	// Servers stores the list of server URLs to balance.
	Servers []string

	// Balancer stores the balancer to be used.
	Balancer Balancer

	// ErrorHandler optionally stores the error handler to handle the balancer error and reply accordingly.
	ErrorHandler ErrorHandler
}

ProxyBalancer balances an incoming HTTP request across a pool of server based on the selected balancing algorithm.

func New

func New(servers ...string) *ProxyBalancer

New creates a new balancer which will balance traffic across the specific URLs.

func (*ProxyBalancer) BalanceHTTP

func (b *ProxyBalancer) BalanceHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)

BalanceHTTP handles an incoming HTTP request and defines the target server to forward the request.

func (*ProxyBalancer) OnError

func (b *ProxyBalancer) OnError(handler ErrorHandler)

OnError defines the error handler to be used to handle the balancer error.

func (*ProxyBalancer) Register

func (b *ProxyBalancer) Register(mw layer.Middleware)

Register registers the middleware.

type Random

type Random struct {
	// contains filtered or unexported fields
}

Random is a simple load balance using a random seed for balancing.

func NewRandom

func NewRandom() *Random

NewRandom creates a new random algorithm to balance servers.

func (*Random) Balance

func (b *Random) Balance(pool []string) (string, error)

Balance returns the next server to be used. Balance implements the Balance interface.

type RoundRobin

type RoundRobin struct {
	// contains filtered or unexported fields
}

RoundRobin is a simple load balancer that returns each of the published endpoints in sequence.

func NewRoundRobin

func NewRoundRobin() *RoundRobin

NewRoundRobin returns a new RoundRobin load balancer.

func (*RoundRobin) Balance

func (b *RoundRobin) Balance(pool []string) (string, error)

Balance returns the next server to be used based on the given server list. Balance implements the Balance interface.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL