Documentation
¶
Overview ¶
本文件对应任务 H07:Shared Registry 只读化。
背景:Agent/Tool/Skill/Command 等共享热注册表在启动完成后基本只读(高频读、低写), 但原实现每次读都加锁(sync.RWMutex.RLock),高并发读在锁上串行。
Freezable[K,V] 提供"可冻结"的泛型注册中心:
- 冻结前:读写走 mutex(允许启动期热注册插件 / 动态装载);
- Freeze():构建一次性只读快照(不可逆);冻结后 Get/Contains/Len/Keys 全部走 快照直接读 map,**完全无锁**(快照 map 冻结后不再写,天然并发安全);
- 冻结后 Put/Remove 返回 ErrFrozen(写入拒绝,符合 H07「Freeze 后写入报错/NOP」)。
读路径在 freeze 后由 RWMutex 读锁降级为裸 map 读,是 H07 的核心收益。
Index ¶
- Variables
- type Freezable
- func (r *Freezable[K, V]) Clone() *Freezable[K, V]
- func (r *Freezable[K, V]) Contains(k K) bool
- func (r *Freezable[K, V]) Freeze()
- func (r *Freezable[K, V]) Get(k K) (V, bool)
- func (r *Freezable[K, V]) IsFrozen() bool
- func (r *Freezable[K, V]) Keys() []K
- func (r *Freezable[K, V]) Len() int
- func (r *Freezable[K, V]) Put(k K, v V) error
- func (r *Freezable[K, V]) Remove(k K) error
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrFrozen = errors.New("registry: frozen, mutation not allowed")
ErrFrozen 表示注册中心已 Freeze,禁止再写。
Functions ¶
This section is empty.
Types ¶
type Freezable ¶
type Freezable[K comparable, V any] struct { // contains filtered or unexported fields }
Freezable 是可冻结的泛型共享注册中心。
func NewFreezable ¶
func NewFreezable[K comparable, V any]() *Freezable[K, V]
NewFreezable 创建空的 Freezable 注册中心。
func NewFreezableFrom ¶
func NewFreezableFrom[K comparable, V any](entries map[K]V) *Freezable[K, V]
NewFreezableFrom 用给定初值构建(深拷贝 map,避免外部共享底层表)。
func (*Freezable[K, V]) Freeze ¶
func (r *Freezable[K, V]) Freeze()
Freeze 构建只读快照并锁定(不可逆)。之后 Put/Remove 返回 ErrFrozen, Get/Contains/Len/Keys 走无锁快照。多次调用幂等。
func (*Freezable[K, V]) Keys ¶
func (r *Freezable[K, V]) Keys() []K
Keys 返回全部键(map 迭代序;调用方如需确定性顺序自行排序)。
Click to show internal directories.
Click to hide internal directories.