Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache whether a user home was created or not #2248

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/gateway-create-home-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Cache whether a user home was created or not

Previously, on every call, we used to stat the user home to make sure that it
existed. Now we cache it for a given amount of time so as to avoid repeated
calls.

https://github.com/cs3org/reva/pull/2248
31 changes: 18 additions & 13 deletions internal/grpc/services/gateway/authprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,25 @@ func (s *svc) Authenticate(ctx context.Context, req *gateway.AuthenticateRequest
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, token) // TODO(jfd): hardcoded metadata key. use PerRPCCredentials?

// create home directory
createHomeRes, err := s.CreateHome(ctx, &storageprovider.CreateHomeRequest{})
if err != nil {
log.Err(err).Msg("error calling CreateHome")
return &gateway.AuthenticateResponse{
Status: status.NewInternal(ctx, err, "error creating user home"),
}, nil
}
if _, err = s.createHomeCache.Get(res.User.Id.OpaqueId); err != nil {
createHomeRes, err := s.CreateHome(ctx, &storageprovider.CreateHomeRequest{})
if err != nil {
log.Err(err).Msg("error calling CreateHome")
return &gateway.AuthenticateResponse{
Status: status.NewInternal(ctx, err, "error creating user home"),
}, nil
}

if createHomeRes.Status.Code != rpc.Code_CODE_OK {
err := status.NewErrorFromCode(createHomeRes.Status.Code, "gateway")
log.Err(err).Msg("error calling Createhome")
return &gateway.AuthenticateResponse{
Status: status.NewInternal(ctx, err, "error creating user home"),
}, nil
if createHomeRes.Status.Code != rpc.Code_CODE_OK {
err := status.NewErrorFromCode(createHomeRes.Status.Code, "gateway")
log.Err(err).Msg("error calling Createhome")
return &gateway.AuthenticateResponse{
Status: status.NewInternal(ctx, err, "error creating user home"),
}, nil
}
if s.c.CreateHomeCacheTTL > 0 {
_ = s.createHomeCache.Set(res.User.Id.OpaqueId, true)
}
}

gwRes := &gateway.AuthenticateResponse{
Expand Down
23 changes: 15 additions & 8 deletions internal/grpc/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type config struct {
HomeMapping string `mapstructure:"home_mapping"`
TokenManagers map[string]map[string]interface{} `mapstructure:"token_managers"`
EtagCacheTTL int `mapstructure:"etag_cache_ttl"`
CreateHomeCacheTTL int `mapstructure:"create_home_cache_ttl"`
}

// sets defaults
Expand Down Expand Up @@ -116,10 +117,11 @@ func (c *config) init() {
}

type svc struct {
c *config
dataGatewayURL url.URL
tokenmgr token.Manager
etagCache *ttlcache.Cache `mapstructure:"etag_cache"`
c *config
dataGatewayURL url.URL
tokenmgr token.Manager
etagCache *ttlcache.Cache `mapstructure:"etag_cache"`
createHomeCache *ttlcache.Cache `mapstructure:"create_home_cache"`
}

// New creates a new gateway svc that acts as a proxy for any grpc operation.
Expand Down Expand Up @@ -148,11 +150,16 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
_ = etagCache.SetTTL(time.Duration(c.EtagCacheTTL) * time.Second)
etagCache.SkipTTLExtensionOnHit(true)

createHomeCache := ttlcache.NewCache()
_ = createHomeCache.SetTTL(time.Duration(c.CreateHomeCacheTTL) * time.Second)
createHomeCache.SkipTTLExtensionOnHit(true)

s := &svc{
c: c,
dataGatewayURL: *u,
tokenmgr: tokenManager,
etagCache: etagCache,
c: c,
dataGatewayURL: *u,
tokenmgr: tokenManager,
etagCache: etagCache,
createHomeCache: createHomeCache,
}

return s, nil
Expand Down