Skip to content

Commit

Permalink
fix(redis): only remove a key if it wouldn't be overwritten (#529)
Browse files Browse the repository at this point in the history
Fixes a potential race condition persisting user permissions where previously we would
remove the entire set of permissions for a resource, then write the current set. This
changes to only remove the permissions if the updated UserPermission is empty for that
resource type, and to write a new value and swap over the old value if there are
Permissions so that an API request to load that UserPermission that overlaps with
a refresh of the UserPermission in redis doesn't get less data than it should
  • Loading branch information
cfieber committed Dec 12, 2019
1 parent a40265e commit e3935c9
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ public RedisPermissionsRepository put(@NonNull UserPermission permission) {

for (ResourceType r : ResourceType.values()) {
String userResourceKey = userKey(userId, r);

pipeline.del(userResourceKey);

Map<String, String> redisValue = resourceTypeToRedisValue.get(r);
String tempKey = UUID.randomUUID().toString();
if (redisValue != null && !redisValue.isEmpty()) {
pipeline.hmset(userResourceKey, redisValue);
pipeline.hmset(tempKey, redisValue);
pipeline.rename(tempKey, userResourceKey);
} else {
pipeline.del(userResourceKey);
}
}
pipeline.sync();
Expand Down

0 comments on commit e3935c9

Please sign in to comment.