cache

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAPICacheNotReady   = errors.New("api cache not ready")
	ErrAPIServiceNotFound = errors.New("api service not found")
	ErrAPIRouteNotFound   = errors.New("api route not found")
)
View Source
var (
	ErrControlSessionChanged = errors.New("control session changed")
)

Functions

This section is empty.

Types

type APIIndex added in v0.0.16

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

func NewAPIIndex added in v0.0.16

func NewAPIIndex() *APIIndex

func (*APIIndex) AllReady added in v0.0.16

func (i *APIIndex) AllReady() bool

func (*APIIndex) AllowsInvoke added in v0.0.16

func (i *APIIndex) AllowsInvoke(roleIDs []uint, serviceID, routeID uint) bool

func (*APIIndex) AnyDirty added in v0.0.16

func (i *APIIndex) AnyDirty() bool

func (*APIIndex) ApplyRole added in v0.0.16

func (i *APIIndex) ApplyRole(action string, value protocol.SyncedAPIRole) error

func (*APIIndex) ApplyRoute added in v0.0.16

func (i *APIIndex) ApplyRoute(action string, value protocol.SyncedAPIRoute) error

func (*APIIndex) ApplyService added in v0.0.16

func (i *APIIndex) ApplyService(action string, value protocol.SyncedAPIService) error

func (*APIIndex) ApplyUpstream added in v0.0.16

func (i *APIIndex) ApplyUpstream(action string, value protocol.SyncedAPIUpstream) error

func (*APIIndex) ApplyUserGroupRoleSet added in v0.0.16

func (i *APIIndex) ApplyUserGroupRoleSet(action string, value protocol.APIRoleSetFetchResult) error

func (*APIIndex) CheckInvoke added in v0.0.16

func (i *APIIndex) CheckInvoke(roleIDs []uint, serviceID, routeID uint) (bool, error)

func (*APIIndex) FindServiceRoute added in v0.0.16

func (i *APIIndex) FindServiceRoute(serviceSlug, routeSlug string) (ServiceRoute, error)

func (*APIIndex) FindServiceRouteByID added in v0.0.16

func (i *APIIndex) FindServiceRouteByID(serviceID, routeID uint) (ServiceRoute, error)

FindServiceRouteByID returns the immutable execution projection named by a Source-frozen service/route pair. It does not perform authorization.

func (*APIIndex) MarkDirty added in v0.0.16

func (i *APIIndex) MarkDirty(entity string)

func (*APIIndex) ReplaceRoles added in v0.0.16

func (i *APIIndex) ReplaceRoles(values []protocol.SyncedAPIRole) error

func (*APIIndex) ReplaceRoutes added in v0.0.16

func (i *APIIndex) ReplaceRoutes(values []protocol.SyncedAPIRoute) error

func (*APIIndex) ReplaceServices added in v0.0.16

func (i *APIIndex) ReplaceServices(values []protocol.SyncedAPIService) error

func (*APIIndex) ReplaceUpstreams added in v0.0.16

func (i *APIIndex) ReplaceUpstreams(values []protocol.SyncedAPIUpstream) error

func (*APIIndex) ReplaceUserGroupRoleSets added in v0.0.16

func (i *APIIndex) ReplaceUserGroupRoleSets(values []protocol.APIRoleSetFetchResult) error

func (*APIIndex) RequireReady added in v0.0.16

func (i *APIIndex) RequireReady() error

func (*APIIndex) ResetReadiness added in v0.0.16

func (i *APIIndex) ResetReadiness()

func (*APIIndex) UpstreamsForBackend added in v0.0.16

func (i *APIIndex) UpstreamsForBackend(backendID uint) []protocol.SyncedAPIUpstream

func (*APIIndex) UserGroupRoleSet added in v0.0.16

func (i *APIIndex) UserGroupRoleSet(groupID uint) protocol.APIRoleSetFetchResult

type APILimiter added in v0.0.16

type APILimiter struct {
	Limiter    *models.RequestLimiter
	TargetType string
	TargetID   uint
}

type ControlSession added in v0.0.13

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

ControlSession is an unforgeable lease for one installed Master connection. Its fields stay private so only Syncer can decide whether a caller may commit.

type LimiterIndex added in v0.0.8

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

LimiterIndex 持有同步下发的 limiters + bindings,重建候选索引, 并按 spec §6.1 就近覆盖解析出每个 (Metric,KeyBy) 维度的生效 limiter。 放在 cache 包(同 RouteIndex),由 Store 持有;relay 侧通过接口读取,避免循环依赖。

func NewLimiterIndex added in v0.0.8

func NewLimiterIndex() *LimiterIndex

func (*LimiterIndex) CacheName added in v0.0.8

func (li *LimiterIndex) CacheName() string

CacheName / CacheStat 实现 NamedCacheStat:报限流器/绑定条数(持锁读)。

func (*LimiterIndex) CacheStat added in v0.0.8

func (li *LimiterIndex) CacheStat() protocol.CacheEntityStats

func (*LimiterIndex) DeleteBinding added in v0.0.8

func (li *LimiterIndex) DeleteBinding(id uint)

func (*LimiterIndex) DeleteLimiter added in v0.0.8

func (li *LimiterIndex) DeleteLimiter(id uint)

func (*LimiterIndex) EffectiveAttemptLimiters added in v0.0.8

func (li *LimiterIndex) EffectiveAttemptLimiters(userID, groupID uint, src string, channelID uint) []*models.RequestLimiter

EffectiveAttemptLimiters 返回尝试级(KeyBy 依赖渠道)的生效 limiter,含 ChannelScope 过滤。 src 是渠道来源字符串:"admin" | "private"(等于 string(state.SourceAdmin/SourcePrivate))。

func (*LimiterIndex) EffectiveRequestLimiters added in v0.0.8

func (li *LimiterIndex) EffectiveRequestLimiters(userID, groupID uint) []*models.RequestLimiter

EffectiveRequestLimiters 返回请求级(KeyBy 不依赖渠道)的生效 limiter。

func (*LimiterIndex) EffectiveSourceAPILimiters added in v0.0.16

func (li *LimiterIndex) EffectiveSourceAPILimiters(userID, groupID, serviceID, routeID uint) []APILimiter

func (*LimiterIndex) EffectiveUpstreamAPILimiters added in v0.0.16

func (li *LimiterIndex) EffectiveUpstreamAPILimiters(upstreamID uint) []APILimiter

func (*LimiterIndex) Limiter added in v0.0.8

func (li *LimiterIndex) Limiter(id uint) *models.RequestLimiter

Limiter 按 ID 返回限流器副本(快照/展示用)。不存在返回 nil。持锁读。

func (*LimiterIndex) LoadBindings added in v0.0.8

func (li *LimiterIndex) LoadBindings(bs []models.LimiterBinding)

func (*LimiterIndex) LoadLimiters added in v0.0.8

func (li *LimiterIndex) LoadLimiters(ls []models.RequestLimiter)

func (*LimiterIndex) PutBinding added in v0.0.8

func (li *LimiterIndex) PutBinding(b *models.LimiterBinding)

func (*LimiterIndex) PutLimiter added in v0.0.8

func (li *LimiterIndex) PutLimiter(l *models.RequestLimiter)

type NamedCacheStat added in v0.0.8

type NamedCacheStat interface {
	CacheName() string // 监控页唯一键,如 "route_index" / "limiter_index"
	CacheStat() protocol.CacheEntityStats
}

NamedCacheStat 是任意"带名字、能自报统计"的缓存/索引。 实现它即可被 Store.CacheSnapshot() 自动纳入心跳上报——无需再在多处手抄清单。 新增缓存务必实现此接口(或由 Store 的反射完整性测试 TestCacheSnapshot_Complete 拦截遗漏)。

type RouteIndex

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

RouteIndex 封装 Agent 路由规则的存储和索引。

func NewRouteIndex

func NewRouteIndex() *RouteIndex

NewRouteIndex 创建空的 RouteIndex。

func (*RouteIndex) Apply added in v0.0.13

func (ri *RouteIndex) Apply(action string, route *models.AgentRoute)

Apply 应用 create/update/delete 路由变更,未知动作不修改索引。

func (*RouteIndex) CacheName added in v0.0.8

func (ri *RouteIndex) CacheName() string

CacheName / CacheStat 实现 NamedCacheStat:索引类只报条目数(无 LRU 命中语义)。

func (*RouteIndex) CacheStat added in v0.0.8

func (ri *RouteIndex) CacheStat() protocol.CacheEntityStats

func (*RouteIndex) Delete

func (ri *RouteIndex) Delete(routeID uint)

Delete 单条删除。

func (*RouteIndex) FindAPIRouteRoute added in v0.0.16

func (ri *RouteIndex) FindAPIRouteRoute(routeID uint) *models.AgentRoute

func (*RouteIndex) FindAPIServiceRoute added in v0.0.16

func (ri *RouteIndex) FindAPIServiceRoute(serviceID uint) *models.AgentRoute

func (*RouteIndex) FindAdminChannelRoute added in v0.0.13

func (ri *RouteIndex) FindAdminChannelRoute(channelID uint, realModel string) *models.AgentRoute

FindAdminChannelRoute 查找当前 admin channel scope 下的精确模型或默认路由。

func (*RouteIndex) FindTokenRoute added in v0.0.13

func (ri *RouteIndex) FindTokenRoute(tokenID uint, realModel string) *models.AgentRoute

FindTokenRoute 查找当前 token scope 下的精确模型或默认路由。

func (*RouteIndex) Load

func (ri *RouteIndex) Load(routes []*models.AgentRoute)

Load 保留全量加载的兼容入口。

func (*RouteIndex) Put

func (ri *RouteIndex) Put(route *models.AgentRoute)

Put 单条增改。

func (*RouteIndex) Replace added in v0.0.13

func (ri *RouteIndex) Replace(routes []*models.AgentRoute)

Replace 全量替换路由索引,并在完整构建后一次性发布。

type ServiceRoute added in v0.0.16

type ServiceRoute struct {
	Service protocol.SyncedAPIService
	Route   protocol.SyncedAPIRoute
}

type Store

type Store struct {
	APIIndex *APIIndex

	RouteIndex *RouteIndex

	LimiterIndex *LimiterIndex
	// contains filtered or unexported fields
}

func NewStore

func NewStore(client app.WSClient, cfg config.AgentCacheConfig) *Store

NewStore 装配 agent 端缓存 Store。 client 用于 LRU 实体的 miss 拉取(可为 nil;nil 时 LRU 实体只读缓存)。 cfg 决定 LRU 容量与负缓存 TTL;零值/非法值由 normalize 兜底为默认。

选择性 LRU:tokens / users 走 LRU;channels / modelConfigs / agents / userGroups 仍是 admin 维护的小规模实体,走 FullCache。

func (*Store) AdvanceVersion added in v0.0.13

func (s *Store) AdvanceVersion(version int64)

AdvanceVersion moves the local global version forward without requiring adjacent values. Global versions may have gaps and stale pages may arrive.

func (*Store) AgentCount

func (s *Store) AgentCount() int

func (*Store) ApplyDirectAddressesUpdate added in v0.0.13

func (s *Store) ApplyDirectAddressesUpdate(update protocol.AgentDirectAddressesUpdate) bool

ApplyDirectAddressesUpdate applies a newer event from the active Master epoch. The version high-water advances even when a manual address wins.

func (*Store) BeginDirectAddressSession added in v0.0.13

func (s *Store) BeginDirectAddressSession(masterInstanceID string)

BeginDirectAddressSession removes runtime addresses from the previous control snapshot while preserving authoritative manual configuration.

func (*Store) CacheSnapshot

func (s *Store) CacheSnapshot() map[string]protocol.CacheEntityStats

CacheSnapshot 收集每实体/索引的 Stats 用于 heartbeat 上报。 单一 provider 列表:实体缓存(LRU/Full)+ 实现 NamedCacheStat 的索引。 新增缓存请加进 namedStats() 或让其实现 NamedCacheStat; TestCacheSnapshot_Complete 反射兜底拦截遗漏。

func (*Store) ChannelCount

func (s *Store) ChannelCount() int

func (*Store) ClearAPIRoleSets added in v0.0.16

func (s *Store) ClearAPIRoleSets()

func (*Store) ClearAgentCapabilities added in v0.0.13

func (s *Store) ClearAgentCapabilities()

func (*Store) Close added in v0.0.13

func (s *Store) Close()

func (*Store) DeleteAgent

func (s *Store) DeleteAgent(agentID string)

func (*Store) DeleteAgentCapabilities added in v0.0.13

func (s *Store) DeleteAgentCapabilities(agentID string)

func (*Store) DeleteChannel

func (s *Store) DeleteChannel(id uint)

func (*Store) DeleteGlobalRouting

func (s *Store) DeleteGlobalRouting(name string)

DeleteGlobalRouting 删除全局 routing。

func (*Store) DeleteModelConfig

func (s *Store) DeleteModelConfig(modelName string)

func (*Store) DeleteToken

func (s *Store) DeleteToken(key string)

func (*Store) DeleteTokenAPIRoleSet added in v0.0.16

func (s *Store) DeleteTokenAPIRoleSet(id uint)

func (*Store) DeleteTokenByID

func (s *Store) DeleteTokenByID(id uint)

func (*Store) DeleteUser

func (s *Store) DeleteUser(id uint)

func (*Store) DeleteUserAPIRoleSet added in v0.0.16

func (s *Store) DeleteUserAPIRoleSet(id uint)

func (*Store) DeleteUserGroup

func (s *Store) DeleteUserGroup(id uint)

func (*Store) Done added in v0.0.13

func (s *Store) Done() <-chan struct{}

func (*Store) EffectiveAttemptLimiters added in v0.0.8

func (s *Store) EffectiveAttemptLimiters(userID, groupID uint, src string, channelID uint) []*models.RequestLimiter

func (*Store) EffectiveRequestLimiters added in v0.0.8

func (s *Store) EffectiveRequestLimiters(userID, groupID uint) []*models.RequestLimiter

EffectiveRequestLimiters / EffectiveAttemptLimiters 委托给 LimiterIndex, 供 relay 侧 Gate 通过 app.AgentCache 接口读取(src: "admin"|"private")。

func (*Store) FallbackSleepMs added in v0.0.2

func (s *Store) FallbackSleepMs() int

FallbackSleepMs 实现 exec.SleepReader 接口。 单独抽出方法是为了避免 exec 包 import cache 包(叶子原则)。

func (*Store) FindTokenAPIRoleSet added in v0.0.16

func (s *Store) FindTokenAPIRoleSet(ctx context.Context, id uint) (*protocol.APIRoleSet, bool, error)

func (*Store) FindTokenByID added in v0.0.16

func (s *Store) FindTokenByID(ctx context.Context, id uint) (*models.Token, bool, error)

func (*Store) FindUser added in v0.0.16

func (s *Store) FindUser(ctx context.Context, id uint) (*protocol.SyncedUser, bool, error)

func (*Store) FindUserAPIRoleSet added in v0.0.16

func (s *Store) FindUserAPIRoleSet(ctx context.Context, id uint) (*protocol.APIRoleSet, bool, error)

func (*Store) GetAgent

func (s *Store) GetAgent(agentID string) *models.Agent

func (*Store) GetAgentCapabilities added in v0.0.13

func (s *Store) GetAgentCapabilities(agentID string) []string

func (*Store) GetAgentsByTag

func (s *Store) GetAgentsByTag(tag string) []*models.Agent

GetAgentsByTag returns all active agents that have the given tag.

func (*Store) GetAllAgents

func (s *Store) GetAllAgents() []*models.Agent

GetAllAgents returns all cached agents.

func (*Store) GetAllModelNames

func (s *Store) GetAllModelNames() []string

GetAllModelNames 返回所有暴露给 /v1/models 的 model 名: - 真实 model(来自 channel.Models 派生的 modelChannels 索引) - 全局 enabled routing 的 name 用户级 routing 不进全局列表(避免命名冲突;用户调 /v1/models 时由 handler 叠加该用户的 user routing)。

func (*Store) GetChannel

func (s *Store) GetChannel(id uint) *models.Channel

func (*Store) GetChannelsForModel

func (s *Store) GetChannelsForModel(model string) []*models.Channel

func (*Store) GetGlobalRouting

func (s *Store) GetGlobalRouting(ctx context.Context, name string) *protocol.SyncedRouting

GetGlobalRouting 返回 enabled 的全局 routing;disabled 或 not found 都返回 nil。 disabled 等同于运行时"临时移除"——校验层另有一份不过滤 enabled 的查询。

func (*Store) GetModelConfig

func (s *Store) GetModelConfig(modelName string) *models.ModelConfig

func (*Store) GetSystemTestToken

func (s *Store) GetSystemTestToken() *models.Token

GetSystemTestToken finds the system test token by name. 走 Range 遍历主存储——LRU 模式下只看缓存中的 token,未缓存的不会被找到。

func (*Store) GetToken

func (s *Store) GetToken(ctx context.Context, key string) *models.Token

func (*Store) GetTokenByID

func (s *Store) GetTokenByID(ctx context.Context, id uint) *models.Token

func (*Store) GetUser

func (s *Store) GetUser(ctx context.Context, id uint) *protocol.SyncedUser

func (*Store) GetUserGroup

func (s *Store) GetUserGroup(id uint) *models.UserGroup

func (*Store) GetVisiblePrivateChannelsForUser added in v0.0.2

func (s *Store) GetVisiblePrivateChannelsForUser(userID uint, model string) []*protocol.SyncedPrivateChannel

func (*Store) GlobalRoutingCount

func (s *Store) GlobalRoutingCount() int

GlobalRoutingCount 返回当前缓存的全局 routing 数(用于 stats / 日志)。

func (*Store) HandleSyncEvent

func (s *Store) HandleSyncEvent(entity, action string, data []byte)

func (*Store) HasRealModel added in v0.0.6

func (s *Store) HasRealModel(name string) bool

HasRealModel 判断 name 是否有 channel 支撑的真实模型(不含 routing)。

func (*Store) InvalidateTokenRoutings added in v0.0.13

func (s *Store) InvalidateTokenRoutings(tokenID uint)

func (*Store) InvalidateUserRoutings

func (s *Store) InvalidateUserRoutings(userID uint)

InvalidateUserRoutings 清掉某 user 的整块 cache,下次 ResolveRouting LRU miss 时由 Loader 重新拉取(B3.4 接入)。

func (*Store) InvalidateVisiblePrivateChannels added in v0.0.2

func (s *Store) InvalidateVisiblePrivateChannels(userID uint)

InvalidateVisiblePrivateChannels 删该 user 的整块 cache,下次 LRU miss 由 loader 重新拉取(与 user_routings 同模式:不增量合并,整块失效)。 share 表变更 / channel CRUD / user 离开 group 都触发。

func (*Store) ListGlobalRoutingNames

func (s *Store) ListGlobalRoutingNames() []string

ListGlobalRoutingNames 返回所有 enabled 全局 routing 名,按字典序排序。 用于 /v1/models 暴露 routing 作为可调用 model 名。

func (*Store) ListTokenRoutingNames added in v0.0.13

func (s *Store) ListTokenRoutingNames(ctx context.Context, tokenID uint) []string

func (*Store) ListUserRoutingNames

func (s *Store) ListUserRoutingNames(ctx context.Context, userID uint) []string

ListUserRoutingNames 返回当前用户 enabled user-scope routing 名,按字典序排序。 userID==0 或无 entry 时返回 nil。LRU miss 触发 loader 失败时同样返回 nil。

func (*Store) ListVisibleBYOKModelNamesForUser added in v0.0.2

func (s *Store) ListVisibleBYOKModelNamesForUser(userID uint) []string

ListVisibleBYOKModelNamesForUser 返回某 user 全部 enabled BYOK channel 的 Models 字段并集(去重,保序:channel 内部 Models 原序,跨 channel 先到先得)。 userID == 0 / 缓存 miss / 无 enabled channel 时返回 nil。 复用 visiblePrivateChannels LRU 缓存层,不引入新缓存。

func (*Store) LoadAgents

func (s *Store) LoadAgents(agents []models.Agent)

func (*Store) LoadChannels

func (s *Store) LoadChannels(channels []models.Channel)

func (*Store) LoadGlobalRoutings

func (s *Store) LoadGlobalRoutings(items []models.ModelRouting)

LoadGlobalRoutings 全量替换 globalRoutings 缓存(用于 FullSync)。 把 models.ModelRouting 投影成 protocol.SyncedRouting;只加载 enabled=true 的条目。

func (*Store) LoadModelConfigs

func (s *Store) LoadModelConfigs(configs []models.ModelConfig)

func (*Store) LoadScripts added in v0.0.6

func (s *Store) LoadScripts(list []models.AdminScript)

LoadScripts 增量入列(full sync,分页友好——逐条 set,不整体替换, 避免分页第 2 页清掉第 1 页)。坏脚本不入列、disabled 入列但 match 时被过滤。

func (*Store) LoadSettings

func (s *Store) LoadSettings(settings []models.Setting)

func (*Store) LoadTokens

func (s *Store) LoadTokens(tokens []models.Token)

func (*Store) LoadUserGroups

func (s *Store) LoadUserGroups(groups []models.UserGroup)

func (*Store) LoadUsers

func (s *Store) LoadUsers(users []protocol.SyncedUser)

func (*Store) MatchScripts added in v0.0.6

func (s *Store) MatchScripts(input script.MatchInput) []*script.Compiled

MatchScripts 实现 script.ScriptProvider,转发给内部 scriptStore。

func (*Store) ModelConfigCount

func (s *Store) ModelConfigCount() int

func (*Store) OnChannelChange

func (s *Store) OnChannelChange(fn func(old, new *models.Channel))

OnChannelChange 注册一个 channel upsert 时的回调。 old 可能是 nil(首次出现);new 可能是 nil(删除)。 同步调用,回调函数应保持轻量。

func (*Store) OverrideVisiblePrivateChannels added in v0.0.2

func (s *Store) OverrideVisiblePrivateChannels(userID uint, channels []protocol.SyncedPrivateChannel)

OverrideVisiblePrivateChannels 直接写入 visiblePrivateChannels LRU, 绕过 loader——仅供跨包测试用,调用方必须在测试上下文中(testing.Testing())。 非测试上下文调用会 panic,避免生产代码意外污染用户 BYOK 缓存。

func (*Store) RebuildModelIndex

func (s *Store) RebuildModelIndex()

RebuildModelIndex 从 channels 重建 model→channels 派生索引。

func (*Store) ResolveRouting

func (s *Store) ResolveRouting(ctx context.Context, name string, owner protocol.RoutingOwner) *protocol.SyncedRouting

ResolveRouting 按 token > user > global 的优先级解析顶层路由。

func (*Store) ResourceCounts added in v0.0.13

func (s *Store) ResourceCounts() (loads, refreshes int64)

func (*Store) ScriptCount added in v0.0.6

func (s *Store) ScriptCount() int

ScriptCount 返回成功编译入列的脚本数。

func (*Store) ScriptEngine added in v0.0.6

func (s *Store) ScriptEngine() *script.Engine

ScriptEngine 返回共享的脚本执行引擎。

func (*Store) SetAgent

func (s *Store) SetAgent(agent *models.Agent)

func (*Store) SetAgentCapabilities added in v0.0.13

func (s *Store) SetAgentCapabilities(agentID string, capabilities []string)

func (*Store) SetChannel

func (s *Store) SetChannel(ch *models.Channel)

func (*Store) SetGlobalRouting

func (s *Store) SetGlobalRouting(name string, r *protocol.SyncedRouting)

SetGlobalRouting 写入全局 routing。WS push / FullSync 调用。

func (*Store) SetLogger

func (s *Store) SetLogger(l *zap.Logger)

SetLogger 注入 zap.Logger,用于 routing apply / resolve 等可观测性日志。 默认 NewStore 使用 zap.NewNop();server 装配时调用以接入实际 logger。

func (*Store) SetModelConfig

func (s *Store) SetModelConfig(mc *models.ModelConfig)

func (*Store) SetToken

func (s *Store) SetToken(token *models.Token)

func (*Store) SetTokenAPIRoleSet added in v0.0.16

func (s *Store) SetTokenAPIRoleSet(id uint, roleSet *protocol.APIRoleSet)

func (*Store) SetTokenRoutings added in v0.0.13

func (s *Store) SetTokenRoutings(tokenID uint, routings map[string]*protocol.SyncedRouting)

func (*Store) SetUser

func (s *Store) SetUser(u *protocol.SyncedUser)

func (*Store) SetUserAPIRoleSet added in v0.0.16

func (s *Store) SetUserAPIRoleSet(id uint, roleSet *protocol.APIRoleSet)

func (*Store) SetUserGroup

func (s *Store) SetUserGroup(g *models.UserGroup)

func (*Store) SetUserQuota added in v0.0.8

func (s *Store) SetUserQuota(id uint, quota int64)

SetUserQuota 更新已缓存 user 的 Quota 字段;用于配额扣减后的原地刷新。 若 user 不在缓存中则静默忽略(不触发 loader 拉取)。

func (*Store) SetUserRoutings

func (s *Store) SetUserRoutings(userID uint, routings map[string]*protocol.SyncedRouting)

SetUserRoutings 用整块替换某 user 的全部 user-scope routings。 WS push 按 user 粒度推送时调用。

func (*Store) SetVersion

func (s *Store) SetVersion(v int64)

func (*Store) Settings added in v0.0.2

func (s *Store) Settings() settings.AgentSettings

Settings 返回当前同步配置快照(value copy,不可变,无锁读)。

func (*Store) TokenCount

func (s *Store) TokenCount() int

func (*Store) TokenRoutingsCount added in v0.0.13

func (s *Store) TokenRoutingsCount() int

func (*Store) TraceMaxBodySize

func (s *Store) TraceMaxBodySize() int

TraceMaxBodySize 兼容老调用方(internal/agent/relay/handler.go:125-126 等)。 新代码请走 Settings().TraceMaxBodySize 直读。

func (*Store) UpdateAgentAutoAddresses

func (s *Store) UpdateAgentAutoAddresses(agentID string, addrs []agentproxy.Address)

UpdateAgentAutoAddresses updates in-memory auto-detected addresses for an agent without overriding manually configured addresses. It remains for callers that replace an already-authorized local snapshot.

func (*Store) UserCount

func (s *Store) UserCount() int

func (*Store) UserGroupCount

func (s *Store) UserGroupCount() int

func (*Store) UserRoutingsCount

func (s *Store) UserRoutingsCount() int

UserRoutingsCount 返回当前 LRU 中缓存的 user-scope routing 块数(每个 user 一块)。

func (*Store) Version

func (s *Store) Version() int64

func (*Store) VisiblePrivateChannelsCount added in v0.0.2

func (s *Store) VisiblePrivateChannelsCount() int

VisiblePrivateChannelsCount 返回当前 LRU 中缓存的 user-scope private channel 块数。

type Syncer

type Syncer struct {
	Store            *Store
	Bus              app.EventBus
	Logger           *zap.Logger
	FullSyncInterval time.Duration
	// contains filtered or unexported fields
}

func NewSyncer

func NewSyncer(store *Store, client app.WSClient, bus app.EventBus, logger *zap.Logger, interval time.Duration) *Syncer

func (*Syncer) ApplySyncPushForSession added in v0.0.13

func (s *Syncer) ApplySyncPushForSession(
	expected *ControlSession,
	params protocol.SyncPushParams,
) (bool, error)

ApplySyncPushForSession atomically checks the connection lease and applies one ordered push. Stale handlers are ignored without touching dirty state.

func (*Syncer) BeginControlSession added in v0.0.13

func (s *Syncer) BeginControlSession(client app.WSClient) *ControlSession

BeginControlSession installs a new client lease and cancels the previous one.

func (*Syncer) CurrentControlSession added in v0.0.13

func (s *Syncer) CurrentControlSession() *ControlSession

func (*Syncer) EndControlSession added in v0.0.13

func (s *Syncer) EndControlSession(expected *ControlSession) bool

EndControlSession clears only the expected lease; stale cleanup cannot clear a replacement connection.

func (*Syncer) FullSync

func (s *Syncer) FullSync(ctx context.Context) error

FullSync pulls all data from master.

LRU 模式实体(token / user / private_channel)不参与 FullSync——它们靠 push invalidate + miss 时 RPC 拉取保持最新。全量重拉违背 LRU 容量目标,会让 agent 内存重新涨回基线。

func (*Syncer) FullSyncForSession added in v0.0.13

func (s *Syncer) FullSyncForSession(ctx context.Context, expected *ControlSession) error

FullSyncForSession runs and commits one pass only while expected remains the current control connection.

func (*Syncer) RequestFullSync added in v0.0.13

func (s *Syncer) RequestFullSync() bool

RequestFullSync queues at most one pass behind the currently running pass.

func (*Syncer) RequestFullSyncForSession added in v0.0.13

func (s *Syncer) RequestFullSyncForSession(expected *ControlSession) bool

func (*Syncer) RunPeriodicCheck added in v0.0.13

func (s *Syncer) RunPeriodicCheck(ctx context.Context)

func (*Syncer) RunRequestedFullSyncs added in v0.0.13

func (s *Syncer) RunRequestedFullSyncs(ctx context.Context)

RunRequestedFullSyncs owns the capacity-one request signal and executes requested passes serially until its server-derived context is cancelled.

func (*Syncer) SetClient

func (s *Syncer) SetClient(client app.WSClient)

SetClient replaces the WS client (e.g., after reconnection)

func (*Syncer) StartPeriodicCheck

func (s *Syncer) StartPeriodicCheck(ctx context.Context)

StartPeriodicCheck starts periodic version comparison

func (*Syncer) SubscribeEvents

func (s *Syncer) SubscribeEvents() error

SubscribeEvents subscribes to local EventBus for sync events from WSBridge

type TokenIDFetcher

type TokenIDFetcher interface {
	LoadByID(ctx context.Context, id uint) (*models.Token, error)
}

TokenIDFetcher 负责按 id 从 master 拉 token—— 当 byID 命中但 primary 已被 LRU 淘汰时使用。 接入将在 Task 16 / Task 18 完成;当前可传 nil。

type WSBridge

type WSBridge struct {
	Client               app.WSClient
	Store                *Store
	Bus                  app.EventBus
	Logger               *zap.Logger
	Syncer               *Syncer
	ControlSession       *ControlSession
	SetAgentCapabilities func(string, []string)
	ApplyDirectAddresses func(protocol.AgentDirectAddressesUpdate) bool
}

func NewWSBridge

func NewWSBridge(client app.WSClient, store *Store, bus app.EventBus, logger *zap.Logger) *WSBridge

func (*WSBridge) Start

func (b *WSBridge) Start()

Start registers WS notification handlers that bridge to local EventBus

Directories

Path Synopsis
Package entitycache 提供通用实体缓存抽象。
Package entitycache 提供通用实体缓存抽象。
Package loaders 提供 entitycache.Loader 的具体实现(每实体一个文件)。
Package loaders 提供 entitycache.Loader 的具体实现(每实体一个文件)。

Jump to

Keyboard shortcuts

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