Skip to content

Commit

Permalink
Cache user lookups in the oc10-sql share manager
Browse files Browse the repository at this point in the history
That leads to a massive performance boost.
  • Loading branch information
aduffeck committed Jan 28, 2022
1 parent 82714f4 commit a24609b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/share/manager/sql/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package sql
import (
"context"
"strings"
"time"

"github.com/ReneKroon/ttlcache/v2"
grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
userprovider "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand Down Expand Up @@ -65,17 +67,34 @@ type UserConverter interface {
// GatewayUserConverter converts usernames and ids using the gateway
type GatewayUserConverter struct {
gwAddr string

IDCache *ttlcache.Cache
NameCache *ttlcache.Cache
}

// NewGatewayUserConverter returns a instance of GatewayUserConverter
func NewGatewayUserConverter(gwAddr string) *GatewayUserConverter {
IDCache := ttlcache.NewCache()
_ = IDCache.SetTTL(30 * time.Second)
IDCache.SkipTTLExtensionOnHit(true)
NameCache := ttlcache.NewCache()
_ = NameCache.SetTTL(30 * time.Second)
NameCache.SkipTTLExtensionOnHit(true)

return &GatewayUserConverter{
gwAddr: gwAddr,
gwAddr: gwAddr,
IDCache: IDCache,
NameCache: NameCache,
}
}

// UserIDToUserName converts a user ID to an username
func (c *GatewayUserConverter) UserIDToUserName(ctx context.Context, userid *userpb.UserId) (string, error) {
username, err := c.NameCache.Get(userid.String())
if err == nil {
return username.(string), nil
}

gwConn, err := pool.GetGatewayServiceClient(c.gwAddr)
if err != nil {
return "", err
Expand All @@ -89,11 +108,17 @@ func (c *GatewayUserConverter) UserIDToUserName(ctx context.Context, userid *use
if getUserResponse.Status.Code != rpc.Code_CODE_OK {
return "", status.NewErrorFromCode(getUserResponse.Status.Code, "gateway")
}
_ = c.NameCache.Set(userid.String(), getUserResponse.User.Username)
return getUserResponse.User.Username, nil
}

// UserNameToUserID converts a username to an user ID
func (c *GatewayUserConverter) UserNameToUserID(ctx context.Context, username string) (*userpb.UserId, error) {
id, err := c.IDCache.Get(username)
if err == nil {
return id.(*userpb.UserId), nil
}

gwConn, err := pool.GetGatewayServiceClient(c.gwAddr)
if err != nil {
return nil, err
Expand All @@ -108,6 +133,7 @@ func (c *GatewayUserConverter) UserNameToUserID(ctx context.Context, username st
if getUserResponse.Status.Code != rpc.Code_CODE_OK {
return nil, status.NewErrorFromCode(getUserResponse.Status.Code, "gateway")
}
_ = c.IDCache.Set(username, getUserResponse.User.Id)
return getUserResponse.User.Id, nil
}

Expand Down

0 comments on commit a24609b

Please sign in to comment.