the kafka reader got an unknown error reading partition

文章描述了一种在使用github.com/segmentio/kafka-go库时遇到的错误,具体是消费者在读取分区0时遇到了超时异常。示例代码显示了配置,其中`MaxWait`参数被设置为500毫秒。问题的解决方案是将`MaxWait`调整为3秒,因为这个参数影响的是数据获取的等待时间,过短可能导致频繁的IO超时异常和连接重建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

github.com/segmentio/kafka-go consumer报以下异常

the kafka reader got an unknown error reading partition 0 of timeout01 at offset 4: read tcp [localhost]:6210->[localhost]:9092: i/o timeout

示例:

	reader :=kafka.NewReader(kafka.ReaderConfig{
		Brokers:           []string{"localhost:9092"},
		GroupID:           "consumer-timeout",
		Topic:             "timeout01",
		ReadBatchTimeout:  500 * time.Millisecond,
		MaxWait:           500 * time.Millisecond,
		CommitInterval:    20 * time.Second,
		SessionTimeout:    30 * time.Second,
		HeartbeatInterval: 3 * time.Second,
		RetentionTime:     7 * 24 * time.Hour,
		StartOffset:       kafka.FirstOffset,
	})
	
	for {
		kafkaMsg,err :=reader.ReadMessage(context.Background())
		if err!=nil {
			fmt.Println("err:",err)
			continue
		}
		fmt.Println("message:",kafkaMsg)
	}

问题修复方案:

MaxWait 修改为 3*time.Second

max_wait不是fetch data wait time,包中仅reader.go 1493行代码中,conn.ReadBatchWith()中超时作用,设置过短会导致io/timeout异常,conn频繁以为异常重建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值