plugin

package
v0.0.0-...-dbb750d Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const BalanceByOrder uint8 = 0 // 负载均衡:轮训
View Source
const BalanceByWeight uint8 = 1 // 负载均衡:权重
View Source
const RESPONSE_CODE_ERROR = 5000
View Source
const ServiceStatusLoading = 1
View Source
const ServiceStatusReady = 0

Variables

View Source
var LocalPath = map[string]func(*Router, http.ResponseWriter, *http.Request){
	"/gateway/getServices":     LocalPathGetServices,
	"/gateway/refreshServices": LocalPathRefreshServices,
}
View Source
var Rander = rand.New(src)

Functions

func ClientIp

func ClientIp(req *http.Request) string

考虑代理 考虑ipv6

func DefaultGetServiceName

func DefaultGetServiceName(path string) (serviceName string)

func DefaultTargetPathFunc

func DefaultTargetPathFunc(service Service, path string) (*url.URL, error)

func JsonResponseError

func JsonResponseError(w http.ResponseWriter, msg string, code ...int)

func JsonResponseSuccess

func JsonResponseSuccess(w http.ResponseWriter, data any)

func LocalPathGetServices

func LocalPathGetServices(r *Router, w http.ResponseWriter, req *http.Request)

func LocalPathRefreshServices

func LocalPathRefreshServices(r *Router, w http.ResponseWriter, req *http.Request)

func RandInt

func RandInt(min int, max int) int

随机数生成 @Param min int 最小值 @Param max int 最大值 @return int [min, max]

func WriteContentType

func WriteContentType(w http.ResponseWriter)

Types

type Gateway

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

func NewGateway

func NewGateway() *Gateway

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)

type GatewayResponse

type GatewayResponse struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

type Limiter

type Limiter struct {
	WhiteList                *Set[string]                // 白名单
	BlackList                *Set[string]                // 黑名单
	RejectList               *RejectList                 // 封禁名单
	AddToRejectList          bool                        // 超过阈值的IP加入到限制列表
	RejectTTL                int64                       // 限制访问的时间
	BlockSeconds             int64                       // 封禁时长,秒,0-不封禁
	RequestNumPerSecond      int                         // 限制请求数
	RequestNumPerSecondPerIp int                         // 限制请求数
	WindowsNum               int                         // 样本窗口个数
	Counter                  *WindowLeapArray            // 滑动时间窗口计数
	CounterMu                sync.Mutex                  // 全局锁
	IpCounter                map[string]*WindowLeapArray // 滑动时间窗口计数,基于IP
	IpCounterMu              []*sync.Mutex
}

func NewLimiter

func NewLimiter(requestNumPerSecond int, requestNumPerSecondPerIp int) *Limiter

NewLimiter 实例化

requestNumPerSecond 每秒允许的请求数

func (*Limiter) AddBlackList

func (lm *Limiter) AddBlackList(bl []string)

func (*Limiter) AddWhiteList

func (lm *Limiter) AddWhiteList(wl []string)

func (*Limiter) Limit

func (lm *Limiter) Limit(w http.ResponseWriter, req *http.Request) error

func (*Limiter) RemoveBlackList

func (lm *Limiter) RemoveBlackList(bl []string)

func (*Limiter) RemoveWhiteList

func (lm *Limiter) RemoveWhiteList(wl []string)

type RejectList

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

func NewRejectList

func NewRejectList() *RejectList

func (*RejectList) Add

func (rl *RejectList) Add(key string, val int64)

func (*RejectList) Get

func (rl *RejectList) Get(key string) (int64, bool)

func (*RejectList) GetAll

func (rl *RejectList) GetAll(key string) map[string]int64

func (*RejectList) Remove

func (rl *RejectList) Remove(key string)

type Router

type Router struct {
	LastUpdate            time.Time
	Expire                time.Duration
	ServiceStatus         uint8
	ServiceMap            ServiceMapType
	ServiceMapCache       ServiceMapType
	GetServiceNameFun     func(string) string
	TargetPathFunc        func(Service, string) (*url.URL, error)
	BalanceType           uint8
	BalanceTypeData       map[string]int // 负载均衡相关数据
	EnableServiceDiscover bool           // 启动服务发现
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) RefreshServices

func (r *Router) RefreshServices() error

RefreshServices Refresh cache goroutine

func (*Router) ReverseProxy

func (r *Router) ReverseProxy(w http.ResponseWriter, req *http.Request)

func (*Router) ServiceBalance

func (r *Router) ServiceBalance(serviceName string) (Service, error)

func (*Router) SetServices

func (r *Router) SetServices() error

type Service

type Service struct {
	Host   string `json:"host"`
	Port   string `json:"port"`
	Weight int    `json:"weight"`
}

type ServiceMapType

type ServiceMapType map[string][]Service

func RetrieveServices

func RetrieveServices() (ServiceMapType, error)

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T comparable]() *Set[T]

func NewSetWithData

func NewSetWithData[T comparable](origin []T) *Set[T]

func (*Set[T]) Add

func (s *Set[T]) Add(element T)

func (*Set[T]) AddMore

func (s *Set[T]) AddMore(elements []T)

func (*Set[T]) All

func (s *Set[T]) All() []T

func (*Set[T]) Exists

func (s *Set[T]) Exists(element T) bool

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

func (*Set[T]) Len

func (s *Set[T]) Len() int

func (*Set[T]) Remove

func (s *Set[T]) Remove(element T)

func (*Set[T]) RemoveMore

func (s *Set[T]) RemoveMore(elements []T)

type WindowLeapArray

type WindowLeapArray struct {
	Arr          []int // 窗口数据
	Front        int   // 游标
	FrontTime    int64 // 游标最新时间
	WindowStatus bool  // 窗口状态,true 为 拒绝访问
}

滑动窗口

func NewWindowLeapArray

func NewWindowLeapArray(windowNum int) *WindowLeapArray

func (*WindowLeapArray) Check

func (w *WindowLeapArray) Check(threshold int) bool

Check 限流计算

Jump to

Keyboard shortcuts

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