/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
go/libkb/team_member_count_cache.go
94 строки
2 KB
zoom-ua
remove golang.org/x/net/context in favor of context (#28708)
17 дек 2025, 18:24
Не верифицирован
17 дек 2025, 18:24
67b0ffe
Код
Авторство
О чём код?
package libkb import ( "context" "sync" "time" "github.com/keybase/client/go/protocol/keybase1" "golang.org/x/time/rate" ) type TeamMemberCountCache struct { g *GlobalContext notifyCh chan struct{} lock sync.RWMutex cache map[keybase1.TeamID]int } func newTeamMemberCountCache(g *GlobalContext) *TeamMemberCountCache { cache := &TeamMemberCountCache{ cache: make(map[keybase1.TeamID]int), g: g, notifyCh: make(chan struct{}, 1), } cache.startNotifyLoop() g.AddLogoutHook(cache, "TeamMemberCountCache") return cache } func (c *TeamMemberCountCache) OnLogout(mctx MetaContext) error { mctx.Debug("TeamMemberCountCache OnLogout: clearing cache") c.lock.Lock() defer c.lock.Unlock() c.cache = make(map[keybase1.TeamID]int) return nil } func (c *TeamMemberCountCache) startNotifyLoop() { ctx, cancel := context.WithCancel(context.Background()) c.g.PushShutdownHook(func(mctx MetaContext) error { mctx.Debug("TeamMemberCountCache shutdown") cancel() return nil }) go c.notifyLoop(ctx) } func (c *TeamMemberCountCache) notifyLoop(ctx context.Context) { const notifyInterval = 5 * time.Second const notifyTimeout = 10 * time.Second limiter := rate.NewLimiter(rate.Every(notifyInterval), 1) for { if err := limiter.Wait(ctx); err != nil { return } select { case <-c.notifyCh: ctx, cancel := context.WithTimeout(context.Background(), notifyTimeout) c.g.NotifyRouter.HandleTeamMetadataUpdate(ctx) cancel() case <-ctx.Done(): return } } } func (c *TeamMemberCountCache) Set(teamID keybase1.TeamID, count int) { c.lock.Lock() defer c.lock.Unlock() if c, ok := c.cache[teamID]; ok && c == count { return } c.cache[teamID] = count select { case c.notifyCh <- struct{}{}: default: } } func (c *TeamMemberCountCache) Get(teamID keybase1.TeamID) (count int, ok bool) { c.lock.RLock() defer c.lock.RUnlock() count, ok = c.cache[teamID] return count, ok } func (c *TeamMemberCountCache) GetWithFallback(teamID keybase1.TeamID, fallback int) (count int) { count, ok := c.Get(teamID) if ok { return count } return fallback }