@@ -74,8 +74,9 @@ class KafkaConsumer: public KafkaClient
7474 * Subscribe to the given list of topics to get dynamically assigned partitions.
7575 * An exception would be thrown if assign is called previously (without a subsequent call to unsubscribe())
7676 */
77- void subscribe (const Topics& topics, Consumer::RebalanceCallback cb = Consumer::RebalanceCallback());
78-
77+ void subscribe (const Topics& topics,
78+ Consumer::RebalanceCallback cb = Consumer::RebalanceCallback(),
79+ std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SUBSCRIBE_TIMEOUT_MS));
7980 /* *
8081 * Get the current subscription.
8182 */
@@ -84,7 +85,7 @@ class KafkaConsumer: public KafkaClient
8485 /* *
8586 * Unsubscribe from topics currently subscribed.
8687 */
87- void unsubscribe ();
88+ void unsubscribe (std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_UNSUBSCRIBE_TIMEOUT_MS) );
8889
8990 /* *
9091 * Manually assign a list of partitions to this consumer.
@@ -119,7 +120,7 @@ class KafkaConsumer: public KafkaClient
119120 */
120121 void seekToBeginning (const TopicPartitions& tps,
121122 std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (tps, true , timeout); }
122- void seekToBeginning (std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (assignment ( false ) , true , timeout); }
123+ void seekToBeginning (std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (_assignment , true , timeout); }
123124
124125 /* *
125126 * Seek to the last offset for each of the given partitions.
@@ -132,7 +133,7 @@ class KafkaConsumer: public KafkaClient
132133 */
133134 void seekToEnd (const TopicPartitions& tps,
134135 std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (tps, false , timeout); }
135- void seekToEnd (std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (assignment ( false ) , false , timeout); }
136+ void seekToEnd (std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_SEEK_TIMEOUT_MS)) { seekToBeginningOrEnd (_assignment , false , timeout); }
136137
137138 /* *
138139 * Get the offset of the next record that will be fetched (if a record with that offset exists).
@@ -229,13 +230,17 @@ class KafkaConsumer: public KafkaClient
229230 static const constexpr char * AUTO_COMMIT_INTERVAL_MS = " auto.commit.interval.ms" ;
230231
231232#if __cplusplus >= 201703L
232- static constexpr int DEFAULT_QUERY_TIMEOUT_MS = 10000 ;
233- static constexpr int DEFAULT_SEEK_TIMEOUT_MS = 10000 ;
234- static constexpr int SEEK_RETRY_INTERVAL_MS = 5000 ;
233+ static constexpr int DEFAULT_SUBSCRIBE_TIMEOUT_MS = 30000 ;
234+ static constexpr int DEFAULT_UNSUBSCRIBE_TIMEOUT_MS = 10000 ;
235+ static constexpr int DEFAULT_QUERY_TIMEOUT_MS = 10000 ;
236+ static constexpr int DEFAULT_SEEK_TIMEOUT_MS = 10000 ;
237+ static constexpr int SEEK_RETRY_INTERVAL_MS = 5000 ;
235238#else
236- enum { DEFAULT_QUERY_TIMEOUT_MS = 10000 };
237- enum { DEFAULT_SEEK_TIMEOUT_MS = 10000 };
238- enum { SEEK_RETRY_INTERVAL_MS = 5000 };
239+ enum { DEFAULT_SUBSCRIBE_TIMEOUT_MS = 30000 };
240+ enum { DEFAULT_UNSUBSCRIBE_TIMEOUT_MS = 10000 };
241+ enum { DEFAULT_QUERY_TIMEOUT_MS = 10000 };
242+ enum { DEFAULT_SEEK_TIMEOUT_MS = 10000 };
243+ enum { SEEK_RETRY_INTERVAL_MS = 5000 };
239244#endif
240245
241246 const OffsetCommitOption _offsetCommitOption;
@@ -260,8 +265,6 @@ class KafkaConsumer: public KafkaClient
260265
261266 // Internal interface for "assign"
262267 void _assign (const TopicPartitions& tps);
263- // Internal interface for "assignment"
264- TopicPartitions assignment (bool withQueryRequest) const ;
265268
266269 std::string _groupId;
267270
@@ -357,7 +360,7 @@ KafkaConsumer::close()
357360
358361// Subscription
359362inline void
360- KafkaConsumer::subscribe (const Topics& topics, RebalanceCallback cb)
363+ KafkaConsumer::subscribe (const Topics& topics, RebalanceCallback cb, std::chrono::milliseconds timeout )
361364{
362365 std::string topicsStr = toString (topics);
363366
@@ -376,21 +379,21 @@ KafkaConsumer::subscribe(const Topics& topics, RebalanceCallback cb)
376379 KAFKA_THROW_IF_WITH_RESP_ERROR (err);
377380
378381 // The rebalcance callback (e.g. "assign", etc) would be served during the time (within this thread)
379- rd_kafka_poll (getClientHandle (), TIMEOUT_INFINITE );
382+ rd_kafka_poll (getClientHandle (), timeout. count () );
380383
381384 KAFKA_API_DO_LOG (LOG_INFO, " subscribed, topics[%s]" , topicsStr.c_str ());
382385}
383386
384387inline void
385- KafkaConsumer::unsubscribe ()
388+ KafkaConsumer::unsubscribe (std::chrono::milliseconds timeout )
386389{
387390 KAFKA_API_DO_LOG (LOG_INFO, " will unsubscribe" );
388391
389392 rd_kafka_resp_err_t err = rd_kafka_unsubscribe (getClientHandle ());
390393 KAFKA_THROW_IF_WITH_RESP_ERROR (err);
391394
392395 // The rebalcance callback (e.g. "assign", etc) would be served during the time (within this thread)
393- rd_kafka_poll (getClientHandle (), TIMEOUT_INFINITE );
396+ rd_kafka_poll (getClientHandle (), timeout. count () );
394397
395398 KAFKA_API_DO_LOG (LOG_INFO, " unsubscribed" );
396399}
@@ -438,31 +441,20 @@ KafkaConsumer::assign(const TopicPartitions& tps)
438441 _assign (tps);
439442}
440443
441- // Assignment, -- internal interface
444+ // Assignment
442445inline TopicPartitions
443- KafkaConsumer::assignment (bool withQueryRequest ) const
446+ KafkaConsumer::assignment () const
444447{
445- if (withQueryRequest)
446- {
447- rd_kafka_topic_partition_list_t * raw_tps = nullptr ;
448- rd_kafka_resp_err_t err = rd_kafka_assignment (getClientHandle (), &raw_tps);
448+ rd_kafka_topic_partition_list_t * raw_tps = nullptr ;
449+ rd_kafka_resp_err_t err = rd_kafka_assignment (getClientHandle (), &raw_tps);
449450
450- auto rk_tps = rd_kafka_topic_partition_list_unique_ptr (raw_tps);
451+ auto rk_tps = rd_kafka_topic_partition_list_unique_ptr (raw_tps);
451452
452- KAFKA_THROW_IF_WITH_RESP_ERROR (err);
453-
454- return getTopicPartitions (rk_tps.get ());
455- }
453+ KAFKA_THROW_IF_WITH_RESP_ERROR (err);
456454
457- return _assignment ;
455+ return getTopicPartitions (rk_tps. get ()) ;
458456}
459457
460- // Assignment, -- external interface
461- inline TopicPartitions
462- KafkaConsumer::assignment () const
463- {
464- return subscription ().empty () ? assignment (false ) : TopicPartitions ();
465- }
466458
467459// Seek & Position
468460inline void
0 commit comments