NewReaderClient returns the kgo.Client that should be used by the Reader.
(kafkaCfg KafkaConfig, metrics *kprom.Metrics, logger log.Logger, opts ...kgo.Opt)
| 17 | |
| 18 | // NewReaderClient returns the kgo.Client that should be used by the Reader. |
| 19 | func NewReaderClient(kafkaCfg KafkaConfig, metrics *kprom.Metrics, logger log.Logger, opts ...kgo.Opt) (*kgo.Client, error) { |
| 20 | const fetchMaxBytes = 100_000_000 |
| 21 | |
| 22 | opts = append(opts, commonKafkaClientOptions(kafkaCfg, metrics, logger)...) |
| 23 | opts = append(opts, |
| 24 | kgo.FetchMinBytes(1), |
| 25 | kgo.FetchMaxBytes(fetchMaxBytes), |
| 26 | kgo.FetchMaxWait(5*time.Second), |
| 27 | kgo.FetchMaxPartitionBytes(50_000_000), |
| 28 | |
| 29 | // BrokerMaxReadBytes sets the maximum response size that can be read from |
| 30 | // Kafka. This is a safety measure to avoid OOMing on invalid responses. |
| 31 | // franz-go recommendation is to set it 2x FetchMaxBytes. |
| 32 | kgo.BrokerMaxReadBytes(2*fetchMaxBytes), |
| 33 | ) |
| 34 | client, err := kgo.NewClient(opts...) |
| 35 | if err != nil { |
| 36 | return nil, errors.Wrap(err, "creating kafka client") |
| 37 | } |
| 38 | if kafkaCfg.AutoCreateTopicEnabled { |
| 39 | kafkaCfg.SetDefaultNumberOfPartitionsForAutocreatedTopics(logger) |
| 40 | } |
| 41 | return client, nil |
| 42 | } |
| 43 | |
| 44 | type Client struct { |
| 45 | logger log.Logger |