feat(sync): paginate Android sync with change tokens
This commit is contained in:
1 parent
0ff94f9b10
commit
97c237be80
76 files changed
+3401
-851
No files matched your search
@@ -10,12 +10,20 @@ const (
|
||||
maxValidationCacheSize = 10000
|
||||
)
|
||||
|
||||
// A session revoked directly in AuthGuard may remain accepted by this client
|
||||
// for up to two seconds. Logout through this Client clears the cached result.
|
||||
|
||||
type validationCacheEntry struct {
|
||||
user User
|
||||
valid bool
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
type profileCacheEntry struct {
|
||||
profile PublicUser
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
func (c *Client) cachedValidation(token string) (*User, error, bool) {
|
||||
key := sha256.Sum256([]byte(token))
|
||||
now := time.Now()
|
||||
@@ -64,15 +72,61 @@ func (c *Client) cacheValidation(token string, user *User, valid bool) {
|
||||
c.validationCache[key] = entry
|
||||
}
|
||||
|
||||
func (c *Client) cachedProfile(token string) (*PublicUser, bool) {
|
||||
key := sha256.Sum256([]byte(token))
|
||||
now := time.Now()
|
||||
c.validationMu.Lock()
|
||||
defer c.validationMu.Unlock()
|
||||
entry, ok := c.profileCache[key]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if !now.Before(entry.expires) {
|
||||
delete(c.profileCache, key)
|
||||
return nil, false
|
||||
}
|
||||
profile := entry.profile
|
||||
return &profile, true
|
||||
}
|
||||
|
||||
func (c *Client) cacheProfile(token string, profile *PublicUser) {
|
||||
if profile == nil {
|
||||
return
|
||||
}
|
||||
key := sha256.Sum256([]byte(token))
|
||||
now := time.Now()
|
||||
c.validationMu.Lock()
|
||||
defer c.validationMu.Unlock()
|
||||
if c.profileCache == nil {
|
||||
c.profileCache = make(map[[32]byte]profileCacheEntry)
|
||||
}
|
||||
if len(c.profileCache) >= maxValidationCacheSize {
|
||||
for existing, entry := range c.profileCache {
|
||||
if !now.Before(entry.expires) {
|
||||
delete(c.profileCache, existing)
|
||||
}
|
||||
}
|
||||
if len(c.profileCache) >= maxValidationCacheSize {
|
||||
for existing := range c.profileCache {
|
||||
delete(c.profileCache, existing)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
c.profileCache[key] = profileCacheEntry{profile: *profile, expires: now.Add(validationCacheTTL)}
|
||||
}
|
||||
|
||||
func (c *Client) invalidateValidation(token string) {
|
||||
key := sha256.Sum256([]byte(token))
|
||||
c.validationMu.Lock()
|
||||
delete(c.validationCache, key)
|
||||
delete(c.profileCache, key)
|
||||
c.validationMu.Unlock()
|
||||
}
|
||||
|
||||
func (c *Client) clearValidationCache() {
|
||||
c.validationMu.Lock()
|
||||
c.validationCache = make(map[[32]byte]validationCacheEntry)
|
||||
c.profileCache = make(map[[32]byte]profileCacheEntry)
|
||||
c.validationMu.Unlock()
|
||||
}
|
||||
Reference in new issue
Block a user