connlimit

package
v1.18.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 12 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewListener

func NewListener(l net.Listener, protocol string, config *Config) (net.Listener, error)

NewListener returns a new listener @param l 网络连接 @param protocol 当前listener的七层协议,比如http,grpc等

func RemoveLimitListener

func RemoveLimitListener(protocol string)

RemoveLimitListener 清理对应协议的链接计数

func SetLimitListener

func SetLimitListener(lis *Listener) error

SetLimitListener 设置当前的listener 注意:Listener.protocol不能重复

Types

type Config

type Config struct {
	// 开启连接限制
	OpenConnLimit bool `mapstructure:"openConnLimit"`

	// 单个host最大的连接数,必须 > 1
	MaxConnPerHost int `mapstructure:"maxConnPerHost"`

	// 当前协议监听端口的最大连接数
	// 兼容老版本,> 1,则开启listen的全局限制;< 1则不开启listen的全局限制
	MaxConnLimit int `mapstructure:"maxConnLimit"`

	// 白名单,不进行host连接数限制
	WhiteList string `mapstructure:"whiteList"`

	// 读超时
	ReadTimeout time.Duration `mapstructure:"readTimeout"`

	// 回收连接统计数据的周期
	PurgeCounterInterval time.Duration `mapstructure:"purgeCounterInterval"`

	// 回收连接的最大超时时间
	PurgeCounterExpire time.Duration `mapstructure:"purgeCounterExpire"`
}

Config 连接限制配置

func ParseConnLimitConfig

func ParseConnLimitConfig(raw map[interface{}]interface{}) (*Config, error)

ParseConnLimitConfig 解析配置

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn 包装net.Conn 目的:拦截Close操作,用于listener计数的Release以及activeConns的删除

func (*Conn) Close

func (c *Conn) Close() error

Close 包装net.Conn.Close, 用于连接计数

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

Read 封装net.Conn Read方法,处理readTimeout的场景

type HostConnStat

type HostConnStat struct {
	Host       string
	Amount     int32
	LastAccess time.Time
	Actives    []string
}

HostConnStat 连接的统计信息

type Listener

type Listener struct {
	net.Listener
	// contains filtered or unexported fields
}

Listener 包装 net.Listener

func GetLimitListener

func GetLimitListener(protocol string) *Listener

GetLimitListener 获取当前的listener

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept 接收连接

func (*Listener) Close

func (l *Listener) Close() error

Close 关闭连接

func (*Listener) GetDistinctHostCount

func (l *Listener) GetDistinctHostCount() int32

GetDistinctHostCount 获取当前缓存的host的个数

func (*Listener) GetHostActiveConns

func (l *Listener) GetHostActiveConns(host string) map[string]*Conn

GetHostActiveConns 获取指定host的活跃的连接

func (*Listener) GetHostConnCount

func (l *Listener) GetHostConnCount(host string) int32

GetHostConnCount 查看对应ip的连接数

func (*Listener) GetHostConnStats

func (l *Listener) GetHostConnStats(host string) []*HostConnStat

GetHostConnStats 获取客户端连接的stat信息

func (*Listener) GetHostConnection

func (l *Listener) GetHostConnection(host string, port int) *Conn

GetHostConnection 获取指定host和port的连接

func (*Listener) GetListenerConnCount

func (l *Listener) GetListenerConnCount() int32

GetListenerConnCount 查看当前监听server保持的连接数

func (*Listener) Range

func (l *Listener) Range(fn func(host string, count int32) bool)

Range 遍历当前持有连接的host

Directories

Path Synopsis
Package mock_net is a generated GoMock package.
Package mock_net is a generated GoMock package.

Jump to

Keyboard shortcuts

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