upstream

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: Apache-2.0 Imports: 9 Imported by: 2

README

upstream

Build Status

It's easy to get upstream from upstream list. It support http request check or tcp port check.

uh := upstream.HTTP{
  // use http request check
  Ping: "/ping",
}
uh.OnStatus(func(status int32, upstream *upstream.HTTPUpstream) {
})
uh.Add("http://127.0.0.1:7001")
uh.AddBackup("http://127.0.0.1:7002")
// do health check
uh.DoHealthCheck()
// do health check loop(default interval: 5s )
go uh.StartHealthCheck()
// get valid upstream by round robin
upstream := uh.PolicyRoundRobin()

Documentation

Index

Constants

View Source
const (
	// UpstreamUnknown upstream unknown
	UpstreamUnknown int32 = iota
	// UpstreamSick upstream sick
	UpstreamSick
	// UpstreamHealthy upstream healthy
	UpstreamHealthy
	// UpstreamIgnored upstream ignored
	UpstreamIgnored
)
View Source
const (
	// PolicyFirst get the first avaliable upstream
	PolicyFirst = "first"
	// PolicyRandom get random avaliable upstream
	PolicyRandom = "random"
	// PolicyRoundRobin get avaliable upstream round robin
	PolicyRoundRobin = "roundRobin"
	// PolicyLeastconn get least connection upstream
	PolicyLeastconn = "leastconn"
)
View Source
const (
	// UserAgent user agent for http check
	UserAgent = "upstream/go"
)

Variables

This section is empty.

Functions

func ConvertStatusToString

func ConvertStatusToString(status int32) string

ConvertStatusToString convert status to string

Types

type Done added in v0.0.4

type Done func()

Done done function for upstream it should be call when upstream is used done

type HTTP

type HTTP struct {
	// Host the http host
	Host string
	// Ping http ping url
	Ping string
	// Timeout timeout for health check
	Timeout time.Duration
	// Interval health check interval
	Interval time.Duration
	// HealthCheckCount health check count
	HealthCheckCount int
	// MaxFailCount max fail count, if fail count >=, it will be set to sick
	MaxFailCount int
	// Policy upstream choose policy
	Policy string
	// contains filtered or unexported fields
}

HTTP http upstream

func (*HTTP) Add

func (h *HTTP) Add(upstream string) error

Add add upstream

func (*HTTP) AddBackup

func (h *HTTP) AddBackup(upstream string) error

AddBackup add backup stream

func (*HTTP) DoHealthCheck

func (h *HTTP) DoHealthCheck()

DoHealthCheck do health check

func (*HTTP) GetAvailableUpstream

func (h *HTTP) GetAvailableUpstream(index uint32) (upstream *HTTPUpstream)

GetAvailableUpstream get available upstream

func (*HTTP) GetAvailableUpstreamList

func (h *HTTP) GetAvailableUpstreamList() []*HTTPUpstream

GetAvailableUpstreamList get available upstream list

func (*HTTP) GetUpstreamList

func (h *HTTP) GetUpstreamList() []*HTTPUpstream

GetUpstreamList get all upstream

func (*HTTP) Next added in v0.0.4

func (h *HTTP) Next(index ...uint32) (*HTTPUpstream, Done)

Next get the next available upstream by policy

func (*HTTP) OnStatus

func (h *HTTP) OnStatus(listener StatusListener)

OnStatus add listener for status event

func (*HTTP) PolicyFirst

func (h *HTTP) PolicyFirst() *HTTPUpstream

PolicyFirst get the first backend

func (*HTTP) PolicyLeastconn

func (h *HTTP) PolicyLeastconn() *HTTPUpstream

PolicyLeastconn get least connection backend

func (*HTTP) PolicyRandom

func (h *HTTP) PolicyRandom() *HTTPUpstream

PolicyRandom get random backend

func (*HTTP) PolicyRoundRobin

func (h *HTTP) PolicyRoundRobin() *HTTPUpstream

PolicyRoundRobin get backend round robin

func (*HTTP) StartHealthCheck

func (h *HTTP) StartHealthCheck()

StartHealthCheck start health check

func (*HTTP) StopHealthCheck

func (h *HTTP) StopHealthCheck()

StopHealthCheck 停止health check

type HTTPUpstream

type HTTPUpstream struct {
	// URL upstream url
	URL *url.URL
	// Backup backup upstream
	Backup bool
	// contains filtered or unexported fields
}

HTTPUpstream http upstream

func (*HTTPUpstream) Dec

func (hu *HTTPUpstream) Dec() uint32

Dec decrease value o upstream

func (*HTTPUpstream) Healthy

func (hu *HTTPUpstream) Healthy()

Healthy set the http upstream to be healthy

func (*HTTPUpstream) Ignored

func (hu *HTTPUpstream) Ignored()

Ignored set the http upstream to be ignored

func (*HTTPUpstream) Inc

func (hu *HTTPUpstream) Inc() uint32

Inc increase value of upstream

func (*HTTPUpstream) Sick

func (hu *HTTPUpstream) Sick()

Sick set the http upstream to be sick

func (*HTTPUpstream) Status

func (hu *HTTPUpstream) Status() int32

Status get upstream status

func (*HTTPUpstream) StatusDesc added in v0.0.2

func (hu *HTTPUpstream) StatusDesc() string

StatusDesc get upstream status description

type StatusListener

type StatusListener func(status int32, upstream *HTTPUpstream)

StatusListener upstream status listener

Jump to

Keyboard shortcuts

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