1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-24 21:11:04 -05:00

make event message processing idempotent

This commit is contained in:
Kyle Spearrin 2019-03-19 17:12:55 -04:00
parent 625ed1a1ee
commit fd8a8c8b67
3 changed files with 11 additions and 6 deletions

View File

@ -8,6 +8,7 @@ namespace Bit.Core.Models.Data
public EventMessage() { } public EventMessage() { }
public EventMessage(CurrentContext currentContext) public EventMessage(CurrentContext currentContext)
: base()
{ {
IpAddress = currentContext.IpAddress; IpAddress = currentContext.IpAddress;
DeviceType = currentContext.DeviceType; DeviceType = currentContext.DeviceType;
@ -24,5 +25,6 @@ namespace Bit.Core.Models.Data
public Guid? ActingUserId { get; set; } public Guid? ActingUserId { get; set; }
public DeviceType? DeviceType { get; set; } public DeviceType? DeviceType { get; set; }
public string IpAddress { get; set; } public string IpAddress { get; set; }
public Guid? IdempotencyId { get; private set; } = Guid.NewGuid();
} }
} }

View File

@ -65,7 +65,8 @@ namespace Bit.Core.Models.Data
return result; return result;
} }
public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) public override void ReadEntity(IDictionary<string, EntityProperty> properties,
OperationContext operationContext)
{ {
base.ReadEntity(properties, operationContext); base.ReadEntity(properties, operationContext);
@ -82,9 +83,9 @@ namespace Bit.Core.Models.Data
} }
} }
public static List<EventTableEntity> IndexEvent(IEvent e) public static List<EventTableEntity> IndexEvent(EventMessage e)
{ {
var uniquifier = Guid.NewGuid(); var uniquifier = e.IdempotencyId.GetValueOrDefault(Guid.NewGuid());
var pKey = e.OrganizationId.HasValue ? $"OrganizationId={e.OrganizationId}" : $"UserId={e.UserId}"; var pKey = e.OrganizationId.HasValue ? $"OrganizationId={e.OrganizationId}" : $"UserId={e.UserId}";
var dateKey = CoreHelpers.DateTimeToTableStorageKey(e.Date); var dateKey = CoreHelpers.DateTimeToTableStorageKey(e.Date);
@ -102,7 +103,8 @@ namespace Bit.Core.Models.Data
entities.Add(new EventTableEntity(e) entities.Add(new EventTableEntity(e)
{ {
PartitionKey = pKey, PartitionKey = pKey,
RowKey = string.Format("ActingUserId={0}__Date={1}__Uniquifier={2}", e.ActingUserId, dateKey, uniquifier) RowKey = string.Format("ActingUserId={0}__Date={1}__Uniquifier={2}",
e.ActingUserId, dateKey, uniquifier)
}); });
} }
@ -111,7 +113,8 @@ namespace Bit.Core.Models.Data
entities.Add(new EventTableEntity(e) entities.Add(new EventTableEntity(e)
{ {
PartitionKey = pKey, PartitionKey = pKey,
RowKey = string.Format("CipherId={0}__Date={1}__Uniquifier={2}", e.CipherId, dateKey, uniquifier) RowKey = string.Format("CipherId={0}__Date={1}__Uniquifier={2}",
e.CipherId, dateKey, uniquifier)
}); });
} }

View File

@ -109,7 +109,7 @@ namespace Bit.Core.Repositories.TableStorage
public async Task CreateEntityAsync(ITableEntity entity) public async Task CreateEntityAsync(ITableEntity entity)
{ {
await _table.ExecuteAsync(TableOperation.Insert(entity)); await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
} }
public async Task<PagedResult<IEvent>> GetManyAsync(string partitionKey, string rowKey, public async Task<PagedResult<IEvent>> GetManyAsync(string partitionKey, string rowKey,