package module
Version: v0.0.0-...-7d3d334 Latest Latest

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

Go to latest
Published: Apr 23, 2015 License: Apache-2.0 Imports: 10 Imported by: 0



A fast and robust http/websocket reverse proxy library written in Go


  • Simple:
    • Implements http.Handler interface
    • Easy to write Balancers for custom routing logic
    • Built-in balancing patterns (Roundrobin, Random, etc ...)
  • Robust: Retry requests on failure
  • Flexible:
    • Custom error handling (so you can draw custom error pages etc ...) (use ErrorHandler)
    • Custom request rewriting (use Rewriter)
    • Your Balancer lookups can use information from the request, hard coded rules or they can query databases such as redis, memcache, etcd, mysql, ...
  • Fast & "scalable":
    • Written in go, so concurrent by default and fast
    • Easy to deploy (proxies can be compiled as single binaries)


package main

import (

func main() {
    p, _ := proxy.New(proxy.ProxyOptions{
        Balancer: func(req *http.Request) (string, error) {
            return "", nil

    http.ListenAndServe(":8080", p)





This section is empty.


This section is empty.


func DefaultRewriter

func DefaultRewriter(req *http.Request, url *url.URL)

DefaultRewriter adjust the *http.Request object it's attributes match those of the destionation url (scheme, host, path, ...)

func Random

func Random(hosts ...string) func(*http.Request) (string, error)

Random generates a balancer than randomly picks one of provided hosts (this does not guarantee equal traffic distribution) but with many requests should get close to it

func Roundrobin

func Roundrobin(hosts ...string) func(*http.Request) (string, error)

Roundrobin generates a balancer that cycles through the list of hosts in a round robin fashion (equally distributing traffic)


type Proxy

type Proxy struct {
	// contains filtered or unexported fields

func New

func New(opts ProxyOptions) (*Proxy, error)

New returns a new Proxy instance based on the provided ProxyOptions either 'Backend' (static) or 'Balancer' must be provided

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP allows us to comply to the http.Handler interface

type ProxyOptions

type ProxyOptions struct {
	// Number of times a request should be tried
	Retries int

	// Period to wait between retries
	Period time.Duration

	// Returns a url that we should proxy to for a given request
	Balancer func(req *http.Request) (string, error)

	// A static backend to route to
	Backend string

	// Rewriter is a function modifying the request before it's proxied
	// usually it should change the hostname of the request object, etc ...
	Rewriter func(req *http.Request, destUrl *url.URL)

	// Allows to customize how errors are written to responses
	ErrorHandler func(rw http.ResponseWriter, req *http.Request, err error)


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL