stream

package
v0.0.0-...-37f8b3f Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultResolution = 1 * time.Minute

DefaultResolution 是衰减跟踪器的默认分辨率

Functions

func NewDecayer

func NewDecayer(cfg *DecayerCfg, mgr *Manager) (*decayer, error)

NewDecayer 创建新的衰减标签注册表

Types

type CMInfo

type CMInfo struct {
	// 低水位线,如NewConnManager中所述
	LowWater int

	// 高水位线,如NewConnManager中所述
	HighWater int

	// 上次触发修剪的时间戳
	LastTrim time.Time

	// 配置的宽限期,如NewConnManager中所述
	GracePeriod time.Duration

	// 当前连接数
	ConnCount int
}

CMInfo 保存Manager的配置以及状态数据

type DecayerCfg

type DecayerCfg struct {
	Resolution time.Duration // 分辨率
	Clock      clock.Clock   // 时钟(用于测试)
}

DecayerCfg 是衰减器的配置对象

func (*DecayerCfg) WithDefaults

func (cfg *DecayerCfg) WithDefaults() *DecayerCfg

WithDefaults 在此DecayerConfig实例上写入默认值, 并返回自身以支持链式调用。

cfg := (&DecayerCfg{}).WithDefaults()
cfg.Resolution = 30 * time.Second
t := NewDecayer(cfg, cm)

type Manager

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

Manager 是一个连接管理器,当连接数超过高水位线时会修剪连接。 新连接在被修剪之前有一个宽限期。修剪会按需自动运行, 只有当距离上次修剪的时间超过10秒时才会执行。 此外,可以通过此结构体的公共接口显式请求修剪(参见 TrimOpenConns)。

参见 NewConnManager 中的配置参数。

func NewConnManager

func NewConnManager(low, hi int, opts ...Option) (*Manager, error)

NewConnManager 使用提供的参数创建新的Manager: lo和hi是水位线,控制将维护的连接数量。 当对等节点数超过"高水位线"时,将修剪尽可能多的对等节点(并终止其连接), 直到剩余"低水位线"个对等节点。 参数 low 为低水位线,hi 为高水位线,opts 为可选配置

func (*Manager) Close

func (cm *Manager) Close() error

Close 关闭连接管理器

func (*Manager) Connected

func (cm *Manager) Connected(n network.Network, c network.Stream)

Connected 由通知器调用,通知已建立新连接。 通知器更新Manager以开始跟踪该连接。 如果新连接数超过高水位线,可能会触发修剪。

func (*Manager) Disconnected

func (cm *Manager) Disconnected(n network.Network, c network.Stream)

Disconnected 由通知器调用,通知现有连接已关闭或终止。 通知器相应地更新Manager以停止跟踪该连接,并执行清理工作。

func (*Manager) GetInfo

func (cm *Manager) GetInfo() CMInfo

GetInfo 返回此连接管理器的配置和状态数据

func (*Manager) GetTagInfo

func (cm *Manager) GetTagInfo(p peer.ID) *connmgr.TagInfo

GetTagInfo 获取与给定对等节点关联的标签信息 如果p引用的是未知对等节点,则返回nil

func (*Manager) HasStream

func (cm *Manager) HasStream(n network.Network, pid peer.ID) (network.Stream, error)

HasStream 检索指定对等节点ID的流(如果存在) 参数 n 为网络,pid 为对等节点ID

func (*Manager) IsProtected

func (cm *Manager) IsProtected(id peer.ID, tag string) (protected bool)

IsProtected 检查指定对等节点是否受保护 参数 id 为对等节点ID,tag 为保护标签(空字符串表示检查任何标签) 返回是否受保护

func (*Manager) Protect

func (cm *Manager) Protect(id peer.ID, tag string)

Protect 保护指定的对等节点不被修剪 参数 id 为对等节点ID,tag 为保护标签

func (Manager) RegisterDecayingTag

func (d Manager) RegisterDecayingTag(name string, interval time.Duration, decayFn connmgr.DecayFn, bumpFn connmgr.BumpFn) (connmgr.DecayingTag, error)

RegisterDecayingTag 注册衰减标签 参数 name 为标签名称,interval 为衰减间隔,decayFn 为衰减函数,bumpFn 为增量函数

func (*Manager) TagPeer

func (cm *Manager) TagPeer(p peer.ID, tag string, val int)

TagPeer 将字符串和整数与给定对等节点关联

func (*Manager) TrimOpenConns

func (cm *Manager) TrimOpenConns(_ context.Context)

TrimOpenConns 关闭尽可能多的对等节点的连接,使对等节点数等于低水位线。 对等节点按其总值的升序排序,优先修剪得分最低的对等节点, 只要它们不在宽限期内。

此函数会阻塞直到修剪完成。如果正在进行修剪, 则不会启动新的修剪,而是等待该修剪完成后再返回。

func (*Manager) Unprotect

func (cm *Manager) Unprotect(id peer.ID, tag string) (protected bool)

Unprotect 取消对指定对等节点的保护 参数 id 为对等节点ID,tag 为保护标签 返回该对等节点是否仍受保护

func (*Manager) UntagPeer

func (cm *Manager) UntagPeer(p peer.ID, tag string)

UntagPeer 取消字符串和整数与给定对等节点的关联

func (*Manager) UpsertTag

func (cm *Manager) UpsertTag(p peer.ID, tag string, upsert func(int) int)

UpsertTag 插入/更新对等节点标签

type Option

type Option func(*config) error

Option 表示基本连接管理器的选项

func DecayerConfig

func DecayerConfig(opts *DecayerCfg) Option

DecayerConfig 应用衰减器的配置

func WithGracePeriod

func WithGracePeriod(p time.Duration) Option

WithGracePeriod 设置宽限期 宽限期是新打开的连接在被修剪之前获得的时间 参数 p 为宽限期时长

func WithSilencePeriod

func WithSilencePeriod(p time.Duration) Option

WithSilencePeriod 设置静默期 如果连接数超过高水位线,连接管理器将在每个静默期执行一次清理 参数 p 为静默期时长

Jump to

Keyboard shortcuts

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