Skip to content

Commit

Permalink
Extract cache invalidation to extra function and call only on cache
Browse files Browse the repository at this point in the history
update
  • Loading branch information
Klaas Freitag committed Jun 5, 2023
1 parent 1fbfb30 commit 7fb083c
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions pkg/share/manager/jsoncs3/jsoncs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ func (m *Manager) UpdateShare(ctx context.Context, ref *collaboration.ShareRefer
m.Lock()
defer m.Unlock()

user := ctxpkg.ContextMustGetUser(ctx)

m.invalidateCache(ctx, user)

var toUpdate *collaboration.Share

if ref != nil {
Expand Down Expand Up @@ -521,7 +525,6 @@ func (m *Manager) UpdateShare(ctx context.Context, ref *collaboration.ShareRefer
}
}

user := ctxpkg.ContextMustGetUser(ctx)
if !share.IsCreatedByUser(toUpdate, user) {
req := &provider.StatRequest{
Ref: &provider.Reference{ResourceId: toUpdate.ResourceId},
Expand Down Expand Up @@ -688,22 +691,47 @@ func (m *Manager) listCreatedShares(ctx context.Context, user *userv1beta1.User,
}

// call in locked context (write lock)
func (m *Manager) invalidateCache(ctx context.Context, expiredShares []*collaboration.Share) {
func (m *Manager) invalidateCache(ctx context.Context, user *userv1beta1.User) {
ssids := map[string]*receivedsharecache.Space{}

for _, s := range expiredShares {
if err := m.removeShare(ctx, s); err != nil {
log.Error().Err(err).
Msg("failed to unshare expired share")
for _, group := range user.Groups {
if err := m.GroupReceivedCache.Sync(ctx, group); err != nil {
continue // ignore error, cache will be updated on next read
}
if err := events.Publish(m.eventStream, events.ShareExpired{
ShareOwner: s.GetOwner(),
ItemID: s.GetResourceId(),
ExpiredAt: time.Unix(int64(s.GetExpiration().GetSeconds()), int64(s.GetExpiration().GetNanos())),
GranteeUserID: s.GetGrantee().GetUserId(),
GranteeGroupID: s.GetGrantee().GetGroupId(),
}); err != nil {
log.Error().Err(err).
Msg("failed to publish share expired event")

for ssid, spaceShareIDs := range m.GroupReceivedCache.List(group) {
// add a pending entry, the state will be updated
// when reading the received shares below if they have already been accepted or denied
var rs *receivedsharecache.Space
var ok bool
if rs, ok = ssids[ssid]; !ok {
rs = &receivedsharecache.Space{
Mtime: spaceShareIDs.Mtime,
States: make(map[string]*receivedsharecache.State, len(spaceShareIDs.IDs)),
}
ssids[ssid] = rs
}

for shareid := range spaceShareIDs.IDs {
rs.States[shareid] = &receivedsharecache.State{
State: collaboration.ShareState_SHARE_STATE_PENDING,
}
}
}
}

for _, s := range ssids {
for sid := range s.States {
sh, err := m.getByID(ctx, &collaboration.ShareId{
OpaqueId: sid,
})

if err != nil && share.IsExpired(sh) {
if err := m.removeShare(ctx, sh); err != nil {
log.Error().Err(err).
Msg("failed to unshare expired share")
}
}
}
}
}
Expand Down

0 comments on commit 7fb083c

Please sign in to comment.