public IEnumerable <IDataBatch> CreateBatches()
        {
            var context = new DataBatchBuildingContext(messageBuffer);

            foreach ((var routingKey, var bufferSlice) in EnumerateAllBufferSlices())
            {
                foreach (var batch in HandleSlice(routingKey, bufferSlice, context))
                {
                    yield return(batch);
                }
            }

            if (!context.IsEmpty)
            {
                yield return(context.CreateBatch());
            }
        }
        private IEnumerable <IDataBatch> HandleSlice(string routingKey, BufferSlice slice, DataBatchBuildingContext context)
        {
            if (context.CurrentMessageBuilder.TryAppend(routingKey, slice))
            {
                context.CurrentSlices.Add(slice);
                yield break;
            }

            if (context.IsEmpty)
            {
                throw new Exception($"Bug! Somehow there's a buffer slice of size {slice.Length} that does not fit into max batch size {messageBuffer.Length} with overhead considered.");
            }

            yield return(context.CreateBatch());

            context.Reset();
        }