reverseproxy

package
v0.0.0-...-be629c2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XRealIP      string = "X-Real-IP"
	XProxy       string = "X-Proxy"
	ReverseProxy string = "Balancer-Reverse-Proxy"
)

Variables

View Source
var HealthCheckTimeout = 5 * time.Second

Functions

This section is empty.

Types

type DefaultRouter

type DefaultRouter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

使用哈希记录路由前缀

func (*DefaultRouter) Add

func (r *DefaultRouter) Add(p string, proxy *HTTPProxy)

func (*DefaultRouter) HasPrefix

func (r *DefaultRouter) HasPrefix(p string) bool

func (*DefaultRouter) Remove

func (r *DefaultRouter) Remove(p string)

func (*DefaultRouter) Route

具体路由选择的算法

func (*DefaultRouter) ServeHTTP

func (r *DefaultRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

执行方法的顺序: 1. 判断 ip 是否在黑名单内 (acl) 2. 执行一遍 FilterChain 的方法 3. 根据 path 找到反向代理 4. 限流 5. 根据反向代理中的主机路径,进行负载均衡 6. 找出一个转发的主机,转发请求

type HTTPProxy

type HTTPProxy struct {
	HostMap       map[string]*httputil.ReverseProxy
	Pattern       string
	Lb            balancer.Balancer
	Alive         map[string]bool
	Methods       map[string]ratelimit.RateLimiter
	HostsShutDown map[string]chan bool
	ShutDown      chan bool
	ProxyMap      *ProxyMap
	sync.RWMutex
}

* * hostMap: 主机对反向代理的映射,其中的键值表示我们需要反向代理的主机 * lb: 负载均衡器 * alive: 反向代理的主机是否处于健康状态

func NewHTTPProxy

func NewHTTPProxy(pattern string, targetHosts []string, algo balancer.Algorithm) (*HTTPProxy, error)

对每一个 URL 创建反向代理并且记录到 URL 树中

func (*HTTPProxy) ChangeLb

func (httpProxy *HTTPProxy) ChangeLb(mode string) error

func (*HTTPProxy) HealthCheck

func (h *HTTPProxy) HealthCheck()

func (*HTTPProxy) ReadAlive

func (h *HTTPProxy) ReadAlive(url string) bool

func (*HTTPProxy) ServeHTTP

func (h *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*HTTPProxy) SetAlive

func (h *HTTPProxy) SetAlive(url string, alive bool)

func (*HTTPProxy) SetRateLimiter

func (httpProxy *HTTPProxy) SetRateLimiter(info LimiterInfo) error

type Info

type Info struct {
	RouterType string
}

type LimiterInfo

type LimiterInfo struct {
	Prefix      string `json:"prefix"`
	PathName    string `json:"pathName"`
	LimiterType string `json:"limiterType"`
	Volumn      int    `json:"volumn"`    // 容量
	Speed       int64  `json:"speed"`     // 速率
	MaxThread   int    `json:"maxThread"` // 最大并发数量
	Duration    int    `json:"duration"`  // 超时时间
}

type ProxyMap

type ProxyMap struct {
	sync.RWMutex
	Relations map[string]*HTTPProxy `json:"-"`
	Locations map[string]config.Location
	Limiters  map[string][]LimiterInfo
	Infos     Info
}

func NewProxyMap

func NewProxyMap() *ProxyMap

func (*ProxyMap) AddProxy

func (proxyMap *ProxyMap) AddProxy(pattern string, host string) error

func (*ProxyMap) AddProxyWithLocation

func (proxyMap *ProxyMap) AddProxyWithLocation(l config.Location) error

func (*ProxyMap) AddRelations

func (proxyMap *ProxyMap) AddRelations(pattern string, proxy *HTTPProxy, location config.Location)

func (*ProxyMap) Marshal

func (proxyMap *ProxyMap) Marshal() ([]byte, error)

func (*ProxyMap) RemoveHost

func (proxyMap *ProxyMap) RemoveHost(pattern string, host string) error

func (*ProxyMap) RemoveProxy

func (proxyMap *ProxyMap) RemoveProxy(pattern string) error

func (*ProxyMap) UnMarshal

func (proxyMap *ProxyMap) UnMarshal(serialized io.ReadCloser) error

type Router

type Router interface {
	Add(string, *HTTPProxy)
	Remove(string)
	HasPrefix(string) bool
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	Route(w http.ResponseWriter, r *http.Request) (*HTTPProxy, string)
}

路由转发器

var (
	RouterSingleton Router
)

func GetRouterInstance

func GetRouterInstance(name string) Router

Jump to

Keyboard shortcuts

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