From 5750f47a0d201196504576faa64973403111cea8 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Mon, 8 Nov 2021 19:13:48 +0100 Subject: [PATCH] Cache whether a user home was created or not --- .../unreleased/gateway-create-home-cache.md | 7 +++++ .../grpc/services/gateway/authprovider.go | 31 +++++++++++-------- internal/grpc/services/gateway/gateway.go | 23 +++++++++----- 3 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 changelog/unreleased/gateway-create-home-cache.md diff --git a/changelog/unreleased/gateway-create-home-cache.md b/changelog/unreleased/gateway-create-home-cache.md new file mode 100644 index 0000000000..9ae99099e0 --- /dev/null +++ b/changelog/unreleased/gateway-create-home-cache.md @@ -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 \ No newline at end of file diff --git a/internal/grpc/services/gateway/authprovider.go b/internal/grpc/services/gateway/authprovider.go index 6a2eb5ae3e..81156facd8 100644 --- a/internal/grpc/services/gateway/authprovider.go +++ b/internal/grpc/services/gateway/authprovider.go @@ -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{ diff --git a/internal/grpc/services/gateway/gateway.go b/internal/grpc/services/gateway/gateway.go index 6908174e16..0de4137d89 100644 --- a/internal/grpc/services/gateway/gateway.go +++ b/internal/grpc/services/gateway/gateway.go @@ -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 @@ -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. @@ -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