Skip to content

Commit

Permalink
add mutex to the in-memory favorites manager
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Oct 5, 2021
1 parent 2eb7aae commit 213ce6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 4 additions & 6 deletions internal/http/services/owncloud/ocdav/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,19 @@ func (s *svc) doFilterFiles(w http.ResponseWriter, r *http.Request, ff *reportFi
statRes, err := client.Stat(ctx, &providerv1beta1.StatRequest{Ref: &providerv1beta1.Reference{ResourceId: favorites[i]}})
if err != nil {
log.Error().Err(err).Msg("error getting resource info")
w.WriteHeader(http.StatusInternalServerError)
return
continue
}
if statRes.Status.Code != rpcv1beta1.Code_CODE_OK {
HandleErrorStatus(log, w, statRes.Status)
return
log.Error().Interface("stat_response", statRes).Msg("error getting resource info")
continue
}

// The paths we receive have the format /user/<username>/<filepath>
// We only want the `<filepath>` part. Thus we remove the /user/<username>/ part.
parts := strings.SplitN(statRes.Info.Path, "/", 4)
if len(parts) != 4 {
log.Error().Str("path", statRes.Info.Path).Msg("path doesn't have the expected format")
w.WriteHeader(http.StatusInternalServerError)
return
continue
}
statRes.Info.Path = parts[3]

Expand Down
18 changes: 13 additions & 5 deletions pkg/storage/favorite/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package favorite

import (
"context"
"sync"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
Expand All @@ -36,18 +37,21 @@ type Manager interface {
}

// NewInMemoryManager returns an instance of the in-memory favorites manager.
func NewInMemoryManager() Manager {
return InMemoryManager{favorites: make(map[string]map[string]*provider.ResourceId)}
func NewInMemoryManager() *InMemoryManager {
return &InMemoryManager{favorites: make(map[string]map[string]*provider.ResourceId)}
}

// InMemoryManager implements the Manager interface to manage favorites using an in-memory storage.
// This should not be used in production but can be used for tests.
type InMemoryManager struct {
sync.RWMutex
favorites map[string]map[string]*provider.ResourceId
}

// ListFavorites returns all resources that were favorited by a user.
func (m InMemoryManager) ListFavorites(ctx context.Context, userID *user.UserId) ([]*provider.ResourceId, error) {
func (m *InMemoryManager) ListFavorites(ctx context.Context, userID *user.UserId) ([]*provider.ResourceId, error) {
m.RLock()
defer m.RUnlock()
favorites := make([]*provider.ResourceId, 0, len(m.favorites[userID.OpaqueId]))
for _, id := range m.favorites[userID.OpaqueId] {
favorites = append(favorites, id)
Expand All @@ -56,7 +60,9 @@ func (m InMemoryManager) ListFavorites(ctx context.Context, userID *user.UserId)
}

// SetFavorite marks a resource as favorited by a user.
func (m InMemoryManager) SetFavorite(_ context.Context, userID *user.UserId, resourceID *provider.ResourceId) error {
func (m *InMemoryManager) SetFavorite(_ context.Context, userID *user.UserId, resourceID *provider.ResourceId) error {
m.Lock()
defer m.Unlock()
if m.favorites[userID.OpaqueId] == nil {
m.favorites[userID.OpaqueId] = make(map[string]*provider.ResourceId)
}
Expand All @@ -65,7 +71,9 @@ func (m InMemoryManager) SetFavorite(_ context.Context, userID *user.UserId, res
}

// UnsetFavorite unmarks a resource as favorited by a user.
func (m InMemoryManager) UnsetFavorite(_ context.Context, userID *user.UserId, resourceID *provider.ResourceId) error {
func (m *InMemoryManager) UnsetFavorite(_ context.Context, userID *user.UserId, resourceID *provider.ResourceId) error {
m.Lock()
defer m.Unlock()
delete(m.favorites[userID.OpaqueId], resourceID.OpaqueId)
return nil
}

0 comments on commit 213ce6f

Please sign in to comment.