autorelay

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter(addrs []ma.Multiaddr) []ma.Multiaddr

Filter 过滤掉所有中继地址

方法名称: Filter 参数:

  • addrs []ma.Multiaddr: 多地址切片

返回值:

  • []ma.Multiaddr: 过滤后的多地址切片

已弃用: 如果用户需要此功能,可以很容易地自行实现

Types

type AutoRelay

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

AutoRelay 自动中继服务结构体

func NewAutoRelay

func NewAutoRelay(bhost *basic.BasicHost, opts ...Option) (*AutoRelay, error)

NewAutoRelay 创建一个新的自动中继服务实例 参数:

  • bhost: *basic.BasicHost 基础主机实例
  • opts: ...Option 可选配置项

返回值:

  • *AutoRelay: 返回创建的自动中继服务实例
  • error: 如果发生错误则返回错误信息

func (*AutoRelay) Close

func (r *AutoRelay) Close() error

Close 关闭自动中继服务 返回值:

  • error: 如果发生错误则返回错误信息

func (*AutoRelay) Start

func (r *AutoRelay) Start()

Start 启动自动中继服务

type ClockWithInstantTimer

type ClockWithInstantTimer interface {
	Now() time.Time                           // 获取当前时间
	Since(t time.Time) time.Duration          // 计算自指定时间以来的持续时间
	InstantTimer(when time.Time) InstantTimer // 创建即时计时器
}

ClockWithInstantTimer 定义了带即时计时器的时钟接口

type InstantTimer

type InstantTimer interface {
	Reset(d time.Time) bool // 重置计时器到指定时间
	Stop() bool             // 停止计时器
	Ch() <-chan time.Time   // 获取计时器通道
}

InstantTimer 定义了在特定时刻触发的计时器接口

type MetricsTracer

type MetricsTracer interface {
	// RelayFinderStatus 更新中继查找器状态
	RelayFinderStatus(isActive bool)

	// ReservationEnded 记录已结束的预约数量
	ReservationEnded(cnt int)
	// ReservationOpened 记录已打开的预约数量
	ReservationOpened(cnt int)
	// ReservationRequestFinished 记录预约请求完成情况
	ReservationRequestFinished(isRefresh bool, err error)

	// RelayAddressCount 更新中继地址数量
	RelayAddressCount(int)
	// RelayAddressUpdated 记录中继地址更新
	RelayAddressUpdated()

	// CandidateChecked 记录候选节点检查结果
	CandidateChecked(supportsCircuitV2 bool)
	// CandidateAdded 记录添加的候选节点数量
	CandidateAdded(cnt int)
	// CandidateRemoved 记录移除的候选节点数量
	CandidateRemoved(cnt int)
	// CandidateLoopState 更新候选循环状态
	CandidateLoopState(state candidateLoopState)

	// ScheduledWorkUpdated 更新计划工作时间
	ScheduledWorkUpdated(scheduledWork *scheduledWorkTimes)

	// DesiredReservations 更新期望的预约数量
	DesiredReservations(int)
}

MetricsTracer 定义了自动中继的指标跟踪接口

func NewMetricsTracer

func NewMetricsTracer(opts ...MetricsTracerOption) MetricsTracer

NewMetricsTracer 创建新的指标跟踪器 @param opts 可选的配置选项 @return MetricsTracer 返回指标跟踪器实例

type MetricsTracerOption

type MetricsTracerOption func(*metricsTracerSetting)

MetricsTracerOption 定义指标跟踪器选项函数类型

func WithRegisterer

func WithRegisterer(reg prometheus.Registerer) MetricsTracerOption

WithRegisterer 设置指标注册器的选项函数

type Option

type Option func(*config) error

Option 定义配置函数类型 参数:

  • *config: 配置对象指针

返回值:

  • error: 返回可能的错误

func WithBackoff

func WithBackoff(d time.Duration) Option

WithBackoff 设置与候选节点获取预约失败后的等待时间 参数:

  • d: time.Duration 退避时间

返回值:

  • Option: 返回配置函数

func WithBootDelay

func WithBootDelay(d time.Duration) Option

WithBootDelay 设置查找中继的启动延迟 参数:

  • d: time.Duration 延迟时间

返回值:

  • Option: 返回配置函数

func WithClock

func WithClock(cl ClockWithInstantTimer) Option

WithClock 配置使用指定的时钟实例 参数:

  • cl: ClockWithInstantTimer 时钟实例

返回值:

  • Option: 返回配置函数

func WithMaxCandidateAge

func WithMaxCandidateAge(d time.Duration) Option

WithMaxCandidateAge 设置候选节点的最大年龄 参数:

  • d: time.Duration 最大存活时间

返回值:

  • Option: 返回配置函数

func WithMaxCandidates

func WithMaxCandidates(n int) Option

WithMaxCandidates 设置我们缓存的中继候选节点数量 参数:

  • n: int 最大候选节点数量

返回值:

  • Option: 返回配置函数

func WithMetricsTracer

func WithMetricsTracer(mt MetricsTracer) Option

WithMetricsTracer 配置 autorelay 使用指定的指标追踪器 参数:

  • mt: MetricsTracer 指标追踪器实例

返回值:

  • Option: 返回配置函数

func WithMinCandidates

func WithMinCandidates(n int) Option

WithMinCandidates 设置在与任何候选节点获取预约之前收集的最小候选节点数量 参数:

  • n: int 最小候选节点数量

返回值:

  • Option: 返回配置函数

func WithMinInterval

func WithMinInterval(interval time.Duration) Option

WithMinInterval 设置调用 peerSource 回调的最小间隔时间 参数:

  • interval: time.Duration 最小间隔时间

返回值:

  • Option: 返回配置函数

func WithNumRelays

func WithNumRelays(n int) Option

WithNumRelays 设置我们努力获取预约的中继节点数量 参数:

  • n: int 期望的中继节点数量

返回值:

  • Option: 返回配置函数

func WithPeerSource

func WithPeerSource(f PeerSource) Option

WithPeerSource 为 AutoRelay 定义一个回调函数,用于查询更多的中继候选节点 参数:

  • f: PeerSource 节点源函数

返回值:

  • Option: 返回配置函数

func WithStaticRelays

func WithStaticRelays(static []peer.AddrInfo) Option

WithStaticRelays 配置使用静态中继节点列表 参数:

  • static: []peer.AddrInfo 静态中继节点列表

返回值:

  • Option: 返回配置函数

type PeerSource

type PeerSource func(ctx context.Context, num int) <-chan peer.AddrInfo

PeerSource 定义了一个函数类型,用于获取中继候选节点 参数:

  • ctx: context.Context 上下文对象,用于取消操作
  • num: int 请求的节点数量

返回值:

  • <-chan peer.AddrInfo: 返回一个只读通道,用于接收节点地址信息

AutoRelay 在需要新的候选节点时会调用此函数,原因可能是: 1. 未连接到期望数量的中继节点 2. 与某个中继节点断开连接 实现必须最多发送 numPeers 个节点,并在不打算提供更多节点时关闭通道。 在通道关闭之前,AutoRelay 不会再次调用回调。 实现应该发送新节点,但也可以发送之前发送过的节点。 AutoRelay 实现了每个节点的退避机制(参见 WithBackoff)。 使用 WithMinInterval 设置回调调用的最小间隔。 当 AutoRelay 认为满意时,传入的 context.Context 可能会被取消, 当节点关闭时它也会被取消。如果上下文被取消,您必须在某个时候关闭输出通道。

type RealClock

type RealClock struct{}

RealClock 实现了 ClockWithInstantTimer 接口

func (RealClock) InstantTimer

func (RealClock) InstantTimer(when time.Time) InstantTimer

InstantTimer 创建即时计时器 参数:

  • when: time.Time 触发时间

返回值:

  • InstantTimer: 返回即时计时器实例

func (RealClock) Now

func (RealClock) Now() time.Time

Now 获取当前时间 返回值:

  • time.Time: 返回当前时间

func (RealClock) Since

func (RealClock) Since(t time.Time) time.Duration

Since 计算自指定时间以来的持续时间 参数:

  • t: time.Time 起始时间

返回值:

  • time.Duration: 返回持续时间

type RealTimer

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

RealTimer 实现了 InstantTimer 接口

func (RealTimer) Ch

func (t RealTimer) Ch() <-chan time.Time

Ch 获取计时器通道 返回值:

  • <-chan time.Time: 返回只读的时间通道

func (RealTimer) Reset

func (t RealTimer) Reset(d time.Time) bool

Reset 重置计时器到指定时间 参数:

  • d: time.Time 目标时间

返回值:

  • bool: 返回是否成功重置

func (RealTimer) Stop

func (t RealTimer) Stop() bool

Stop 停止计时器 返回值:

  • bool: 返回是否成功停止

Jump to

Keyboard shortcuts

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