private async Task Dispatch(IMessageContext context, MiddlewareDelegate next) { try { var batchContext = new BatchConsumeMessageContext( context.WorkerId, context.GroupId, context.Consumer, this.batch.ToList()); await next(batchContext).ConfigureAwait(false); } catch (Exception ex) { this.logHandler.Error( "Error executing a message batch", ex, new { context.Topic, context.GroupId, context.WorkerId }); } finally { foreach (var messageContext in this.batch) { messageContext.Consumer.StoreOffset(); } this.dispatchTask = null; this.batch.Clear(); } }
private async Task Dispatch(IMessageContext context, MiddlewareDelegate next) { var localBatch = Interlocked.Exchange(ref this.batch, new(this.batchSize)); try { var batchContext = new BatchConsumeMessageContext(context.ConsumerContext, localBatch); await next(batchContext).ConfigureAwait(false); } catch (Exception ex) { this.logHandler.Error( "Error executing a message batch", ex, new { context.ConsumerContext.Topic, context.ConsumerContext.GroupId, context.ConsumerContext.WorkerId, }); } finally { foreach (var messageContext in localBatch) { messageContext.ConsumerContext.StoreOffset(); } } }