Documentation
¶
Overview ¶
Package PointSub 提供了基于 libp2p 的流式处理功能
Package pointsub 提供点对点订阅功能的实现 该包实现了基于 LibP2P 的点对点通信功能,支持请求-响应模式的消息传递
Package PointSub 提供了基于 libp2p 的流式处理功能 ¶
Package PointSub 提供了基于 libp2p 的流式处理功能 ¶
Package PointSub 提供了使用 LibP2P streams 替换 Go 标准网络栈的功能 ¶
主要功能: - 接收一个 libp2p.Host 参数 - 提供 Dial() 和 Listen() 方法,返回 net.Conn 和 net.Listener 的实现
网络寻址: - 不使用传统的 "host:port" 寻址方式 - 使用 Peer ID 进行寻址 - 使用 libp2p 的 net.Stream 替代原始 TCP 连接 - 支持 LibP2P 的多路由、NAT 穿透和流复用功能
使用限制: - LibP2P hosts 不能自己连接自己 - 同一个 Host 不能同时作为服务端和客户端使用
Package pointsub 提供了点对点订阅功能的实现
Index ¶
- Variables
- func Dial(ctx context.Context, h host.Host, pid peer.ID, tag protocol.ID) (net.Conn, error)
- func Listen(h host.Host, tag protocol.ID) (net.Listener, error)
- func NewLimitedConn(conn net.Conn, maxSize int) net.Conn
- func SetLog(filename string, stderr ...bool)
- type Client
- type ClientConfig
- type ConnectionInfo
- type LimitedConn
- type Server
- type ServerConfig
- type StreamHandler
Constants ¶
This section is empty.
Variables ¶
var (
ErrServerAlreadyStarted = errors.New("服务器已经启动") // 服务器已启动错误
)
错误定义
var Network = "pointsub"
Network 定义了 PointSub 连接使用的网络类型名称 用于在调用 net.Addr.Network() 时返回 对应的 net.Addr.String() 将返回 peer ID
Functions ¶
func Dial ¶
Dial 使用给定的主机打开到目标地址的流 参数:
- ctx: 上下文对象,用于控制操作的生命周期
- h: libp2p 主机对象
- pid: 目标对等节点的 ID
- tag: 协议标识符
返回值:
- net.Conn: 标准网络连接接口
- error: 错误信息
func Listen ¶
Listen 提供一个标准的 net.Listener 接口用于接受"连接" 底层使用带有指定协议 ID 标签的 libp2p 流 参数:
- h: libp2p 主机对象
- tag: 协议标识符
返回值:
- net.Listener: 标准网络监听器接口
- error: 错误信息
func NewLimitedConn ¶
NewLimitedConn 创建一个新的限制大小的连接 参数:
- conn: 原始连接
- maxSize: 最大大小限制
返回值:
- net.Conn: 限制大小的连接
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 定义客户端结构体 该结构体封装了客户端的所有功能和状态
func NewClient ¶
func NewClient(h host.Host, config *ClientConfig) (*Client, error)
NewClient 创建新的客户端实例 该函数初始化一个新的客户端,并启动必要的后台任务 参数: - h: libp2p主机实例,用于网络通信 - config: 客户端配置,如果为nil则使用默认配置 返回值: - *Client: 新创建的客户端实例 - error: 如果配置无效则返回错误
func (*Client) Send ¶
func (c *Client) Send(ctx context.Context, peerID peer.ID, protocolID protocol.ID, request []byte) ([]byte, error)
Send 发送请求并接收响应 该方法实现了完整的请求-响应通信流程,支持重试机制 参数: - ctx: 上下文,用于控制请求的生命周期 - peerID: 目标节点ID,指定请求发送的目标节点 - protocolID: 协议ID,指定使用的通信协议 - request: 请求数据,要发送的数据内容 返回值: - []byte: 响应数据,目标节点返回的响应内容 - error: 发送过程中的错误,如果发生错误则返回对应的错误信息
type ClientConfig ¶
type ClientConfig struct {
ReadTimeout time.Duration // 读取超时时间,控制单次读取操作的最大等待时间
WriteTimeout time.Duration // 写入超时时间,控制单次写入操作的最大等待时间
ConnectTimeout time.Duration // 连接超时时间,控制建立连接的最大等待时间
MaxRetries int // 最大重试次数,发送失败时的最大重试次数
RetryInterval time.Duration // 重试间隔时间,两次重试之间的等待时间
MaxBlockSize int // 最大数据块大小,单次传输的最大字节数
EnableCompression bool // 是否启用压缩,控制是否对传输数据进行压缩
MaxIdleConns int // 最大空闲连接数,连接池中保持的最大空闲连接数
IdleConnTimeout time.Duration // 空闲连接超时时间,空闲连接被清理前的最大存活时间
}
ClientConfig 定义客户端配置参数 包含了客户端运行所需的各项配置选项
func DefaultClientConfig ¶
func DefaultClientConfig() *ClientConfig
DefaultClientConfig 返回默认的客户端配置 该函数返回一个使用推荐默认值的配置实例 返回值: - *ClientConfig: 包含默认配置值的 ClientConfig 实例
type ConnectionInfo ¶
type ConnectionInfo struct {
RemoteAddr string // 远程地址
LastActive time.Time // 最后活跃时间
IdleTime time.Duration // 空闲时间
}
ConnectionInfo 连接信息结构体
type LimitedConn ¶
LimitedConn 包装了一个带有大小限制的连接
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 定义服务结构 包含服务器运行所需的各种组件
func NewServer ¶
func NewServer(h host.Host, config *ServerConfig) (*Server, error)
NewServer 创建新的服务器实例 参数:
- h: libp2p 主机实例
- config: 服务器配置,如果为nil则使用默认配置
返回值:
- *Server: 新创建的服务器实例
- error: 错误信息
func (*Server) GetConnectionsInfo ¶
func (s *Server) GetConnectionsInfo() []ConnectionInfo
GetConnectionsInfo 获取当前活跃连接信息 返回值:
- []ConnectionInfo: 连接信息列表
type ServerConfig ¶
type ServerConfig struct {
// 连接相关配置
ReadTimeout time.Duration // 读取超时时间,控制单次读取操作的最大时间
WriteTimeout time.Duration // 写入超时时间,控制单次写入操作的最大时间
MaxBlockSize int // 最大消息大小,限制单条消息的最大字节数
// 并发控制
MaxConcurrentConns int // 最大并发连接数,限制同时处理的最大连接数
ConnectTimeout time.Duration // 连接建立超时时间,控制连接建立的最大等待时间
// 资源控制
MaxRequestsPerConn int // 每个连接最大请求数,限制单个连接可处理的最大请求数
IdleTimeout time.Duration // 空闲连接超时时间,超过此时间的空闲连接将被关闭
// 内存控制
BufferPoolSize int // 缓冲池大小,控制内存池中单个缓冲区的大小
EnableCompression bool // 是否启用压缩,控制是否对消息进行压缩处理
// 连接清理相关配置
CleanupInterval time.Duration // 清理检查间隔,定期检查并清理空闲连接的时间间隔
MaxIdleTime time.Duration // 最大空闲时间,连接允许的最大空闲时间
// 告警相关配置
MaxMemoryUsage uint64 // 最大内存使用量,限制服务器的最大内存使用量
}
ServerConfig 定义服务器配置 包含连接、并发、资源、内存和监控等相关配置项
func DefaultServerConfig ¶
func DefaultServerConfig() *ServerConfig
DefaultServerConfig 返回默认的服务器配置 返回值:
- *ServerConfig: 包含默认值的配置对象
func (*ServerConfig) Validate ¶
func (c *ServerConfig) Validate() error
Validate 验证配置是否有效 返回值:
- error: 错误信息