remove locking from accounts service

add a cached named rwlock pkg
use sync.map in the cache pkg
use named rwlock in indexer pkg
use sync.map in indexer pkg
remove husky
This commit is contained in:
Florian Schade
2021-01-19 23:23:41 +01:00
parent f97f0d7342
commit a02fb890f7
11 changed files with 10554 additions and 131 deletions
+2 -20
View File
@@ -11,7 +11,6 @@ import (
"path"
"regexp"
"strconv"
"sync"
"time"
"github.com/owncloud/ocis/ocis-pkg/log"
@@ -33,11 +32,8 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)
// accLock mutually exclude readers from writers on account files
var accLock sync.Mutex
// passwordValidCache caches basic auth password validations
var passwordValidCache = cache.NewCache(cache.Size(1024))
var passwordValidCache = cache.NewCache(1024)
// passwordValidCacheExpiration defines the entry lifetime
const passwordValidCacheExpiration = 10 * time.Minute
@@ -133,9 +129,6 @@ func (s Service) ListAccounts(ctx context.Context, in *proto.ListAccountsRequest
}
onlySelf := hasSelf && !hasManagement
accLock.Lock()
defer accLock.Unlock()
teardownServiceUser := s.serviceUserToIndex()
defer teardownServiceUser()
match, authRequest := getAuthQueryMatch(in.Query)
@@ -172,11 +165,8 @@ func (s Service) ListAccounts(ctx context.Context, in *proto.ListAccountsRequest
// - it checks if the cache already contains an entry that matches found account Id // account PasswordProfile.LastPasswordChangeDateTime (k)
// - if no entry exists it runs the bcrypt.CompareHashAndPassword as before and if everything is ok it stores the
// result by the (k) as key and (v) as value. If not it errors
// - if a entry is found it checks if the given value matches (v). If it doesnt match the cache entry gets removed
// - if a entry is found it checks if the given value matches (v). If it doesnt match, the cache entry gets removed
// and it errors.
//
// if many concurrent requests from the same user come in within a short period of time, it's possible that e is still nil.
// This is why all this needs to be wrapped within a sync.Mutex locking to prevent calling bcrypt.CompareHashAndPassword unnecessarily too often.
{
var suspicious bool
@@ -284,8 +274,6 @@ func (s Service) GetAccount(ctx context.Context, in *proto.GetAccountRequest, ou
}
onlySelf := hasSelf && !hasManagement
accLock.Lock()
defer accLock.Unlock()
var id string
if id, err = cleanupID(in.Id); err != nil {
return merrors.InternalServerError(s.id, "could not clean up account id: %v", err.Error())
@@ -331,8 +319,6 @@ func (s Service) CreateAccount(ctx context.Context, in *proto.CreateAccountReque
return merrors.Forbidden(s.id, "no permission for CreateAccount")
}
accLock.Lock()
defer accLock.Unlock()
var id string
if in.Account == nil {
@@ -468,8 +454,6 @@ func (s Service) UpdateAccount(ctx context.Context, in *proto.UpdateAccountReque
}
onlySelf := hasSelf && !hasManagement
accLock.Lock()
defer accLock.Unlock()
var id string
if in.Account == nil {
return merrors.BadRequest(s.id, "account missing")
@@ -633,8 +617,6 @@ func (s Service) DeleteAccount(ctx context.Context, in *proto.DeleteAccountReque
return merrors.Forbidden(s.id, "no permission for DeleteAccount")
}
accLock.Lock()
defer accLock.Unlock()
var id string
if id, err = cleanupID(in.Id); err != nil {
return merrors.InternalServerError(s.id, "could not clean up account id: %v", err.Error())