Documentation
¶
Overview ¶
已弃用: 基于数据库的 peerstore 将在未来从 go-dep2p 中移除。 请改用内存 peerstore (pstoremem)。 更多详情请参见 https://github.com/dep2p/issues/2329 和 https://github.com/dep2p/issues/2355。
Index ¶
- func NewAddrBook(ctx context.Context, store ds.Batching, opts Options) (ab *dsAddrBook, err error)
- func NewKeyBook(_ context.Context, store ds.Datastore, _ Options) (*dsKeyBook, error)
- func NewPeerMetadata(_ context.Context, store ds.Datastore, _ Options) (*dsPeerMetadata, error)
- func NewPeerstore(ctx context.Context, store ds.Batching, opts Options) (*pstoreds, error)
- func NewProtoBook(meta pstore.PeerMetadata, opts ...ProtoBookOption) (*dsProtoBook, error)
- type Options
- type ProtoBookOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAddrBook ¶
NewAddrBook 初始化一个基于数据存储的地址簿
参数:
- ctx: context.Context 上下文
- store: ds.Batching 数据存储接口
- opts: Options 配置选项
返回:
- *dsAddrBook 地址簿实例
- error 错误信息
说明: 它可以作为 pstoremem(内存存储)的替代品,可以与任何实现 ds.Batching 接口的数据存储一起工作。
地址和对等点记录被序列化为 protobuf,每个对等点存储一个数据条目,同时包含控制地址过期的元数据。 为了减轻磁盘访问和序列化开销,我们在内部使用读/写 ARC 缓存,其大小可通过 Options.CacheSize 调整。
用户可以选择两种 GC 算法:
前瞻 GC:通过维护一个时间索引列表来最小化完整存储遍历,该列表包含在 Options.GCLookaheadInterval 指定的时间段内需要访问的条目。这在 TTL 差异较大且数据存储的原生迭代器按字典序返回条目的场景中很有用。 通过设置 Options.GCLookaheadInterval > 0 启用此模式。前瞻窗口是跳跃式的,而不是滑动式的。 清除操作仅在前瞻窗口内以 Options.GCPurgeInterval 的周期执行。
完全清除 GC(默认):以 Options.GCPurgeInterval 的周期对存储进行完整访问。当可能的 TTL 值范围较小 且值本身也很极端时很有用,例如在其他 dep2p 模块中常用的 10 分钟或永久值。在这种情况下,使用前瞻窗口 优化意义不大。
func NewKeyBook ¶
NewKeyBook 创建一个新的基于数据存储的密钥簿
参数:
- ctx: context.Context 上下文
- store: ds.Datastore 数据存储接口
- opts: Options 配置选项
返回:
- *dsKeyBook 密钥簿实例
- error 错误信息
func NewPeerMetadata ¶
NewPeerMetadata 创建一个由持久化数据库支持的元数据存储。它使用 gob 进行序列化。
参数:
- ctx: 上下文对象
- store: 数据存储实例
- opts: 配置选项
返回值:
- *dsPeerMetadata: 元数据存储实例
- error: 错误信息
查看 `init()` 函数了解默认注册的类型。 想要存储其他类型值的模块需要显式调用 `gob.Register()`, 否则调用者将收到运行时错误。
func NewPeerstore ¶
NewPeerstore 创建一个由持久化数据存储支持的 peerstore。 调用者需要负责调用 RemovePeer 以确保 peerstore 的内存消耗不会无限增长。
参数:
- ctx: 上下文对象
- store: 支持批处理的数据存储
- opts: 配置选项
返回值:
- *pstoreds: peerstore 实例
- error: 错误信息
func NewProtoBook ¶
func NewProtoBook(meta pstore.PeerMetadata, opts ...ProtoBookOption) (*dsProtoBook, error)
NewProtoBook 创建新的协议簿 参数:
- meta: peer元数据接口
- opts: 配置选项
返回:
- *dsProtoBook: 新创建的协议簿
- error: 错误信息
Types ¶
type Options ¶
type Options struct { // 内存缓存的大小。值为 0 或更小时禁用缓存 CacheSize uint // MaxProtocols 是每个对等节点存储的最大协议数 MaxProtocols int // 从数据存储中清除过期地址的扫描间隔。如果为零值,GC 不会自动运行,但可以通过显式调用触发 GCPurgeInterval time.Duration // 更新 GC 预读窗口的间隔。如果为零值,预读将被禁用,每个清除周期都会遍历整个数据存储 GCLookaheadInterval time.Duration // GC 进程启动前的初始延迟。目的是在启动 GC 前给系统充分的启动时间 GCInitialDelay time.Duration // 时钟接口 Clock clock }
Options 是 peerstore 的配置对象
func DefaultOpts ¶
func DefaultOpts() Options
DefaultOpts 返回持久化 peerstore 的默认选项,使用完整清除 GC 算法:
* 缓存大小: 1024 * 最大协议数: 1024 * GC 清除间隔: 2小时 * GC 预读间隔: 禁用 * GC 初始延迟: 60秒
type ProtoBookOption ¶
type ProtoBookOption func(*dsProtoBook) error
ProtoBookOption 协议簿配置选项函数类型
func WithMaxProtocols ¶
func WithMaxProtocols(num int) ProtoBookOption
WithMaxProtocols 设置最大协议数量的选项 参数:
- num: 最大协议数量
返回:
- ProtoBookOption: 配置选项函数