Ejemplo n.º 1
0
 /// <inheritdoc />
 public bool Equals(ProduceRequest other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(timeout.Equals(other.timeout) &&
            acks == other.acks &&
            topics.HasEqualElementsInOrder(other.topics));
 }
Ejemplo n.º 2
0
 /// <inheritdoc />
 public bool Equals(ProduceRequest other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Timeout.Equals(other.Timeout) &&
            Acks == other.Acks &&
            Payloads.HasEqualElementsInOrder(other.Payloads));
 }
Ejemplo n.º 3
0
        private static byte[] EncodeRequest(IRequestContext context, ProduceRequest request)
        {
            var totalCompressedBytes = 0;
            var groupedPayloads      = (from p in request.Payloads
                                        group p by new
            {
                p.TopicName,
                p.PartitionId,
                p.Codec
            } into tpc
                                        select tpc).ToList();

            using (var writer = EncodeHeader(context, request)) {
                writer.Write(request.Acks)
                .Write((int)request.Timeout.TotalMilliseconds)
                .Write(groupedPayloads.Count);

                foreach (var groupedPayload in groupedPayloads)
                {
                    var payloads = groupedPayload.ToList();
                    writer.Write(groupedPayload.Key.TopicName)
                    .Write(payloads.Count)
                    .Write(groupedPayload.Key.PartitionId);

                    switch (groupedPayload.Key.Codec)
                    {
                    case MessageCodec.CodecNone:
                        writer.Write(payloads.SelectMany(x => x.Messages));
                        break;

                    case MessageCodec.CodecGzip:
                        var compressedBytes = CreateGzipCompressedMessage(payloads.SelectMany(x => x.Messages));
                        Interlocked.Add(ref totalCompressedBytes, compressedBytes.CompressedAmount);
                        writer.Write(new[] { compressedBytes.CompressedMessage });
                        break;

                    default:
                        throw new NotSupportedException($"Codec type of {groupedPayload.Key.Codec} is not supported.");
                    }
                }

                var bytes = writer.ToBytes();
                context.OnProduceRequestMessages?.Invoke(request.Payloads.Sum(_ => _.Messages.Count), bytes.Length, totalCompressedBytes);
                return(bytes);
            }
        }