rrdialer

package module
v0.0.0-...-521a42d Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2017 License: MIT Imports: 11 Imported by: 0

README

rrdialer

GoDoc Build Status

A round robin net dialer.

rrdialer has features below.

  • Dials to one of specified hosts by round robin.
  • Health checker by Go's function
    • MySQL
    • TCP
    • HTTP
    • or Your custom function

Usage

Without healthchecker

A simple usage.

addrs := []string{"host1:8888", "host2:8888"},
da := rrdialer.NewDialer(ctx, addrs, nil)
conn, err := da.Dial("tcp") // connect to host1:8888 or host2:8888
Example for MySQL

go-sql-driver/mysql has an interface to customize dialer.

// upstream addresses
addrs := []string{"slave1:3306", "slave2:3307"},

// health checker
opt := rrdialer.NewOption()
// health checker uses "tcp" as usual
opt.CheckFunc = rrdialer.NewMySQLCheckFunc("test:test@tcp(dummy)/test")

da := rrdialer.NewDialer(ctx, addrs, opt)

// register a custom network "roundrobin" for rrdialer into mysql
dsn := "test:test@roundrobin(dummy)/test"
cfg, _ := mysql.ParseDSN(dsn)
mysql.RegisterDial("roundrobin",
	func(_ string) (net.Conn, error) {
		return da.DialTimeout("tcp", cfg.Timeout)
	},
)

// connect via rrdialer
db, _ := sql.Open("mysql", dsn)
Example for TCP
addrs := []string{"host1:8888", "host2:8888"},

opt := rrdialer.NewOption()
opt.CheckFunc = rrdialer.NewTCPCheckFunc()

da := rrdialer.NewDialer(ctx, addrs, opt)
conn, err := da.Dial("tcp")
Example for HTTP health check
addrs := []string{"backend1:80", "backend2:80"}

checkReq, _ := http.NewRequest("GET", "http://example.com/ping", nil)
opt := rrdialer.NewOption()
opt.CheckFunc = rrdialer.NewHTTPCheckFunc(checkReq)

da := rrdialer.NewDialer(ctx, addrs, opt)

client := &http.Client{
	Transport: &http.Transport{
		DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
			return da.DialContext(ctx, network)
		},
		Dial: func(network, _ string) (net.Conn, error) {
			return da.Dial(network)
		},
	},
}

// client connects to a server via rrdialer
res, err := client.Get("http://example.com/foo/bar")

LICENSE

The MIT License (MIT)

Copyright (c) 2017 FUJIWARA Shunichiro / (c) 2017 KAYAC Inc.

Documentation

Index

Constants

View Source
const (
	DefaultEjectThreshold   = 2
	DefaultRecoverThreshold = 2
	DefaultCheckInterval    = 5 * time.Second
	DefaultCheckTimeout     = 5 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckFunc

type CheckFunc func(ctx context.Context, addr string) error

func NewHTTPCheckFunc

func NewHTTPCheckFunc(req *http.Request) CheckFunc

NewHTTPCheckFunc makes CheckFunc for HTTP server. This func send request to server and check HTTP status code. A status code over 400 makes failure.

func NewMySQLCheckFunc

func NewMySQLCheckFunc(dsn string) CheckFunc

NewMySQLCheckFunc makes CheckFunc for MySQL server

func NewTCPCheckFunc

func NewTCPCheckFunc() CheckFunc

NewMySQLCheckFunc makes CheckFunc for general TCP server This func tries connect and close simply.

type Dialer

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

func NewDialer

func NewDialer(ctx context.Context, address []string, opt *Option) *Dialer

NewDialer makes a Dialer.

func (*Dialer) Dial

func (d *Dialer) Dial(network string) (net.Conn, error)

Dial dials to an available address in Dialer via network.

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network string) (net.Conn, error)

DialContext dials to an available address in Dialer via network with context.

func (*Dialer) DialTimeout

func (d *Dialer) DialTimeout(network string, timeout time.Duration) (net.Conn, error)

DialContext dials to an available address in Dialer via network with timeout.

func (*Dialer) Get

func (d *Dialer) Get() (string, error)

Get gets an available address in Dialer.

type Logger

type Logger interface {
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

Logger is an interface for rrdialer

type Option

type Option struct {
	EjectThreshold   int           // When health check failed count reached EjectThreshold, upstream is marked as down until pass health check
	RecoverThreshold int           // When health check succeeded count reached RecoverThreshold, upstream is marked as alive
	CheckInterval    time.Duration // health check interval
	CheckTimeout     time.Duration // health check timeout
	Logger           Logger
	CheckFunc        CheckFunc
	NextUpstream     bool // Try next upstream when dial failed
}

func NewOption

func NewOption() *Option

NewOption makes Option with default values.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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