mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:36:30 +00:00
throttle reimplemented
This commit is contained in:
@@ -65,25 +65,22 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
Password = _queueConfig.ConsumerPassword
|
||||
});
|
||||
|
||||
var offset = (ulong)(_api.CurrentUser.UserConfig?.QueueConfig?.AnnotationsOffset ?? 0);
|
||||
var offsets = _api.CurrentUser.UserConfig?.QueueOffsets ?? new UserQueueOffsets();
|
||||
|
||||
_consumer = await Consumer.Create(new ConsumerConfig(consumerSystem, Constants.MQ_ANNOTATIONS_QUEUE)
|
||||
{
|
||||
Reference = _api.CurrentUser.Email,
|
||||
OffsetSpec = new OffsetTypeOffset(offset + 1),
|
||||
OffsetSpec = new OffsetTypeOffset(offsets.AnnotationsOffset + 1),
|
||||
MessageHandler = async (_, _, context, message) =>
|
||||
{
|
||||
var msg = MessagePackSerializer.Deserialize<AnnotationCreatedMessage>(message.Data.Contents);
|
||||
await _dbFactory.Run(async db => await db.QueueOffsets
|
||||
.Where(x => x.QueueName == Constants.MQ_ANNOTATIONS_QUEUE)
|
||||
.Set(x => x.Offset, context.Offset)
|
||||
.UpdateAsync(token: cancellationToken));
|
||||
|
||||
await ThrottleExt.Throttle(() =>
|
||||
offsets.AnnotationsOffset = context.Offset;
|
||||
ThrottleExt.Throttle(() =>
|
||||
{
|
||||
_dbFactory.SaveToDisk();
|
||||
_api.UpdateOffsets(offsets);
|
||||
return Task.CompletedTask;
|
||||
}, TimeSpan.FromSeconds(10), cancellationToken);
|
||||
}, SaveTaskId, TimeSpan.FromSeconds(10));
|
||||
|
||||
if (msg.CreatedEmail == _api.CurrentUser.Email) //Don't process messages by yourself
|
||||
return;
|
||||
@@ -194,11 +191,11 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
await _producer.SendToInnerQueue(annotation, token);
|
||||
|
||||
await _mediator.Publish(new AnnotationCreatedEvent(annotation), token);
|
||||
await ThrottleExt.Throttle(() =>
|
||||
ThrottleExt.Throttle(async () =>
|
||||
{
|
||||
_dbFactory.SaveToDisk();
|
||||
return Task.CompletedTask;
|
||||
}, TimeSpan.FromSeconds(5), token);
|
||||
await Task.CompletedTask;
|
||||
}, SaveTaskId, TimeSpan.FromSeconds(5));
|
||||
return annotation;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user