proxy

package
v0.0.0-...-28fadcf Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Copyright 2017 The margin Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file. Read more about consistent hashing on wikipedia: http://en.wikipedia.org/wiki/Consistent_hashing Modified from https://github.com/stathat/consistent

Package proxy provides an tcp proxy applied with consistent routers to backends

Index

Constants

View Source
const (
	MonitorInterval = 1e9
)

Variables

View Source
var ErrEmptyCircle = errors.New("empty circle")

ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.

Functions

func UnderlyingConn

func UnderlyingConn(c net.Conn) net.Conn

UnderlyingConn returns c.Conn if c of type *Conn, otherwise it returns c.

Types

type Conn

type Conn struct {
	// Peeked are the bytes that have been read from Conn for the
	// purposes of route matching, but have not yet been consumed
	// by Read calls. It set to nil by Read when fully consumed.
	Peeked []byte

	// Conn is the underlying connection.
	net.Conn
}

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

type Consistent

type Consistent struct {
	NumberOfReplicas int

	sync.RWMutex
	// contains filtered or unexported fields
}

Consistent holds the information about the members of the consistent hash circle.

func New

func New() *Consistent

New creates a new Consistent object with a default setting of 20 replicas for each entry.

To change the number of replicas, set NumberOfReplicas before adding entries.

func (*Consistent) Add

func (c *Consistent) Add(elt string, t Target)

Add inserts a string element in the consistent hash.

func (*Consistent) Get

func (c *Consistent) Get(name string) (string, error)

Get returns an element close to where name hashes to in the circle.

func (*Consistent) GetN

func (c *Consistent) GetN(name string, n int) ([]string, error)

GetN returns the N closest distinct elements to the name input in the circle.

func (*Consistent) GetTarget

func (c *Consistent) GetTarget(name string) (Target, error)

func (*Consistent) GetTwo

func (c *Consistent) GetTwo(name string) (string, string, error)

GetTwo returns the two closest distinct elements to the name input in the circle.

func (*Consistent) Members

func (c *Consistent) Members() []string

func (*Consistent) Remove

func (c *Consistent) Remove(elt string)

Remove removes an element from the hash.

func (*Consistent) Set

func (c *Consistent) Set(elts []string)

Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.

type DialProxy

type DialProxy struct {
	// Addr is the TCP address to proxy to.
	Addr string

	KeepAlivePeriod time.Duration

	// If zero, a default is used.
	// If negative, the timeout is disabled.
	DialTimeout time.Duration

	// DialContext optionally specifies an alternate dial function
	// for TCP targets. If nil, the standard
	// net.Dialer.DialContext method is used.
	DialContext func(ctx context.Context, network, address string) (net.Conn, error)

	// OnDialError optionally specifies an alternate way to handle errors dialing Addr.
	OnDialError func(src net.Conn, dstDialErr error)

	ProxyProtocolVersion int
}

DialProxy implements Target by dialing a new connection to Addr and then proxying data back and forth.

func To

func To(addr string) *DialProxy

func (*DialProxy) HandleConn

func (dp *DialProxy) HandleConn(src net.Conn)

HandleConn implements the Target interface.

type Matcher

type Matcher func(ctx context.Context, hostname string) bool

Matcher reports whether hostname matches the Matcher's criteria.

type Proxy

type Proxy struct {
	LocalHost string

	// ListenFunc optionally specifies an alternate listen function.
	ListenFunc func(net, laddr string) (net.Listener, error)
	// contains filtered or unexported fields
}

Proxy is a proxy.

func (*Proxy) AddHTTPHostMatchRoute

func (p *Proxy) AddHTTPHostMatchRoute(match Matcher, dest Target)

func (*Proxy) AddHTTPHostRoute

func (p *Proxy) AddHTTPHostRoute(httpHost string, dest Target)

func (*Proxy) AddRoute

func (p *Proxy) AddRoute(dest string)

AddRoute is generally used as either the only rule (for simple TCP proxies based on consistent hash)

func (*Proxy) Close

func (p *Proxy) Close() error

Close closes all the proxy's self-opened listeners.

func (*Proxy) Run

func (p *Proxy) Run() error

Run is calls Start, and then Wait.

func (*Proxy) Start

func (p *Proxy) Start() error

Start creates a TCP listener and starts the proxy.

func (*Proxy) Wait

func (p *Proxy) Wait() error

Wait waits for the Proxy to finish running.

type Target

type Target interface {
	// HandleConn is called when an incoming connection is
	// matched.
	HandleConn(net.Conn)
}

Target is what an incoming matched connection is sent to.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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