pointsub

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: Apache-2.0 Imports: 17 Imported by: 2

README

PointSub

PointSub 是一个基于 LibP2P 的网络通信库,提供了使用 LibP2P streams 替换 Go 标准网络栈的功能。

主要特性

  • 基于 LibP2P 的流式通信
  • 提供标准的 net.Conn 和 net.Listener 接口实现
  • 支持多路由、NAT 穿透和流复用
  • 使用 Peer ID 进行寻址,无需传统的 host:port 方式
  • 可配置的连接超时、并发控制、资源限制等

使用限制

  • LibP2P hosts 不能自己连接自己
  • 同一个 Host 不能同时作为服务端和客户端使用

配置选项

支持丰富的服务端配置选项:

  • 连接超时和读写超时
  • 最大并发连接数
  • 每连接最大请求数
  • 空闲连接超时
  • 内存使用限制
  • 压缩选项
  • 连接清理间隔

使用方式

  1. 创建 LibP2P Host
  2. 使用 Host 创建 Server 或 Client
  3. 通过标准的 net.Conn 接口进行通信

详细使用方法请参考测试用例。

Documentation

Overview

Package PointSub 提供了基于 libp2p 的流式处理功能

Package pointsub 提供点对点订阅功能的实现

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 不能同时作为服务端和客户端使用

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServerAlreadyStarted = errors.New("服务器已经启动") // 服务器已启动错误
)

错误定义

View Source
var Network = "pointsub"

Network 定义了 PointSub 连接使用的网络类型名称 用于在调用 net.Addr.Network() 时返回 对应的 net.Addr.String() 将返回 peer ID

Functions

func Dial

func Dial(ctx context.Context, h host.Host, pid peer.ID, tag protocol.ID) (net.Conn, error)

Dial 使用给定的主机打开到目标地址的流 参数:

  • ctx: 上下文对象,用于控制操作的生命周期
  • h: libp2p 主机对象
  • pid: 目标对等节点的 ID
  • tag: 协议标识符

返回值:

  • net.Conn: 标准网络连接接口
  • error: 错误信息

func Listen

func Listen(h host.Host, tag protocol.ID) (net.Listener, error)

Listen 提供一个标准的 net.Listener 接口用于接受"连接" 底层使用带有指定协议 ID 标签的 libp2p 流 参数:

  • h: libp2p 主机对象
  • tag: 协议标识符

返回值:

  • net.Listener: 标准网络监听器接口
  • error: 错误信息

func NewLimitedConn

func NewLimitedConn(conn net.Conn, maxSize int) net.Conn

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) Close

func (c *Client) Close() error

Close 关闭客户端 返回值: - 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

type LimitedConn struct {
	net.Conn // 底层连接
	// contains filtered or unexported fields
}

LimitedConn 包装了一个带有大小限制的连接

func (*LimitedConn) Read

func (c *LimitedConn) Read(b []byte) (n int, err error)

Read 读取数据 参数:

  • b: 读取缓冲区

返回值:

  • n: 读取的字节数
  • err: 错误信息

func (*LimitedConn) Write

func (c *LimitedConn) Write(b []byte) (n int, err error)

Write 写入数据 参数:

  • b: 要写入的数据

返回值:

  • n: 写入的字节数
  • err: 错误信息

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: 连接信息列表

func (*Server) Start

func (s *Server) Start(protocolID protocol.ID, handler StreamHandler) error

Start 启动服务器 参数:

  • protocolID: 协议标识符
  • handler: 消息处理函数

返回值:

  • error: 错误信息

func (*Server) Stop

func (s *Server) Stop() error

Stop 停止服务器 返回值:

  • error: 错误信息

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: 错误信息

type StreamHandler

type StreamHandler func(request []byte) (response []byte, err error)

StreamHandler 定义了处理消息的函数类型 参数:

  • request: 请求消息字节切片

返回值:

  • []byte: 响应消息
  • error: 错误信息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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