Beispiel #1
0
        public bool Produce(string topic, object key, object data, int partition)
        {
            if (_configuration.MaxBufferedMessages > 0)
            {
                if (_cluster.Entered - _cluster.PassedThrough >= _configuration.MaxBufferedMessages)
                {
                    switch (_configuration.OverflowStrategy)
                    {
                    case OverflowStrategy.Discard:
                        return(false);

                    case OverflowStrategy.Block:
                        SpinWait.SpinUntil(() => _cluster.Entered - _cluster.PassedThrough < _configuration.MaxBufferedMessages);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            _cluster.UpdateEntered();
            _cluster.ProduceRouter.Route(topic, new Message {
                Key = key, Value = data
            }, partition, DateTime.UtcNow.Add(_configuration.MessageTtl));
            return(true);
        }
Beispiel #2
0
        public bool Produce(string topic, object key, object data, ICollection <KafkaRecordHeader> headers, int partition, DateTime timestamp)
        {
            if (_configuration.MaxBufferedMessages > 0)
            {
                if (_cluster.Entered - _cluster.PassedThrough >= _configuration.MaxBufferedMessages)
                {
                    switch (_configuration.OverflowStrategy)
                    {
                    case OverflowStrategy.Discard:
                        return(false);

                    case OverflowStrategy.Block:
                        lock (_lock)
                        {
                            SpinWait.SpinUntil(
                                () =>
                                _cluster.Entered - _cluster.PassedThrough
                                < _configuration.MaxBufferedMessages);
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            _cluster.UpdateEntered();
            _cluster.ProduceRouter.Route(topic,
                                         new Message
            {
                Key       = key,
                Value     = data,
                Headers   = _configuration.Compatibility >= Compatibility.V0_11_0 ? headers : null,
                TimeStamp =
                    _configuration.Compatibility >= Compatibility.V0_10_1 ? Timestamp.ToUnixTimestamp(timestamp) : 0
            },
                                         partition, DateTime.UtcNow.Add(_configuration.MessageTtl));
            return(true);
        }