Documentation ¶
Index ¶
- Constants
- Variables
- func ClientIp(req *http.Request) string
- func DefaultGetServiceName(path string) (serviceName string)
- func DefaultTargetPathFunc(service Service, path string) (*url.URL, error)
- func JsonResponseError(w http.ResponseWriter, msg string, code ...int)
- func JsonResponseSuccess(w http.ResponseWriter, data any)
- func LocalPathGetServices(r *Router, w http.ResponseWriter, req *http.Request)
- func LocalPathRefreshServices(r *Router, w http.ResponseWriter, req *http.Request)
- func RandInt(min int, max int) int
- func WriteContentType(w http.ResponseWriter)
- type Gateway
- type GatewayResponse
- type Limiter
- type RejectList
- type Router
- type Service
- type ServiceMapType
- type Set
- type WindowLeapArray
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 DefaultGetServiceName ¶
func DefaultTargetPathFunc ¶
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 WriteContentType ¶
func WriteContentType(w http.ResponseWriter)
Types ¶
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
func NewGateway ¶
func NewGateway() *Gateway
type GatewayResponse ¶
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 (*Limiter) AddBlackList ¶
func (*Limiter) AddWhiteList ¶
func (*Limiter) RemoveBlackList ¶
func (*Limiter) RemoveWhiteList ¶
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) 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 (*Router) RefreshServices ¶
RefreshServices Refresh cache goroutine
func (*Router) ReverseProxy ¶
func (r *Router) ReverseProxy(w http.ResponseWriter, req *http.Request)
func (*Router) ServiceBalance ¶
func (*Router) SetServices ¶
type ServiceMapType ¶
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]) 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
Click to show internal directories.
Click to hide internal directories.