Skip to content

Commit

Permalink
Fix memory increase (#18)
Browse files Browse the repository at this point in the history
* fix

Signed-off-by: fum1h1to <75571344+fum1h1to@users.noreply.github.com>

* fix: gache updating

Signed-off-by: fum1h1to <75571344+fum1h1to@users.noreply.github.com>

* fix: comment

Signed-off-by: fum1h1to <75571344+fum1h1to@users.noreply.github.com>

---------

Signed-off-by: fum1h1to <75571344+fum1h1to@users.noreply.github.com>
  • Loading branch information
fum1h1to authored Oct 30, 2024
1 parent e10bc0d commit 94d667f
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 272 deletions.
31 changes: 12 additions & 19 deletions authorizerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ type authority struct {
client *http.Client

// successful result cache
cache gache.Gache[Principal]
cacheExp time.Duration
cacheMemoryUsage *atomic.Int64
cacheMemoryUsageMap gache.Gache[int64]
cache gache.Gache[Principal]
cacheExp time.Duration
cacheMemoryUsage *atomic.Int64

// roleCertURIPrefix
roleCertURIPrefix string
Expand Down Expand Up @@ -132,9 +131,8 @@ const (
func New(opts ...Option) (Authorizerd, error) {
var (
prov = &authority{
cache: gache.New[Principal](),
cacheMemoryUsage: &atomic.Int64{},
cacheMemoryUsageMap: gache.New[int64](),
cache: gache.New[Principal](),
cacheMemoryUsage: &atomic.Int64{},
}
err error
pkPro pubkey.Provider
Expand Down Expand Up @@ -331,7 +329,6 @@ func (a *authority) Start(ctx context.Context) <-chan error {
case <-ctx.Done():
g.Stop()
g.Clear()
a.cacheMemoryUsageMap.Clear()
ech <- ctx.Err()
return
case err := <-cech:
Expand Down Expand Up @@ -481,19 +478,16 @@ func (a *authority) authorize(ctx context.Context, m mode, tok, act, res, query
})
a.cache.SetWithExpire(key.String(), p, a.cacheExp)

// Memory usage that cannot be calculated with gache.Size().
// The memory usage of the principal cache entity and
// the memory usage of the key (cacheMemoryUsage + cacheMemoryUsageMap).
principalCacheSize := principalCacheMemoryUsage(p) + (int64(len(key.String())) * 2)
// Calculate memory usage of key and principal that cannot be calculated with gache.Size()
principalCacheSize := principalCacheMemoryUsage(key.String(), p)

a.cacheMemoryUsageMap.SetWithExpire(key.String(), principalCacheSize, 0)
a.cacheMemoryUsage.Add(principalCacheSize)

return p, nil
}

// principalCacheMemoryUsage returns memory usage of principal
func principalCacheMemoryUsage(p Principal) int64 {
func principalCacheMemoryUsage(key string, p Principal) int64 {
structSize := int64(unsafe.Sizeof(p))
name := p.Name()
domain := p.Domain()
Expand All @@ -515,7 +509,7 @@ func principalCacheMemoryUsage(p Principal) int64 {
const int64Size = 8
timesSize := int64Size * 2

return structSize + nameSize + domainSize + rolesSize + authorizedRolesSize + int64(timesSize)
return structSize + nameSize + domainSize + rolesSize + authorizedRolesSize + int64(timesSize) + int64(len(key))
}

// GetPrincipalCacheLen returns entries number of cached principals
Expand All @@ -525,14 +519,13 @@ func (a *authority) GetPrincipalCacheLen() int {

// GetPrincipalCacheSize returns memory usage of cached principals
func (a *authority) GetPrincipalCacheSize() int64 {
return int64(a.cache.Size()) + a.cacheMemoryUsage.Load() + int64(a.cacheMemoryUsageMap.Size())
return int64(a.cache.Size()) + a.cacheMemoryUsage.Load()
}

// cacheExpiredHook refreshes the value of cacheMemoryUsage when the cache expires.
func (prov *authority) cacheExpiredHook(ctx context.Context, key string) {
cacheUsage, _ := prov.cacheMemoryUsageMap.Get(key)
func (prov *authority) cacheExpiredHook(ctx context.Context, key string, value Principal) {
cacheUsage := principalCacheMemoryUsage(key, value)
prov.cacheMemoryUsage.Add(-cacheUsage)
prov.cacheMemoryUsageMap.Delete(key)
}

// Verify returns error of verification. Returns nil if ANY authorizer succeeds (OR logic).
Expand Down
Loading

0 comments on commit 94d667f

Please sign in to comment.