Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions kafka/consumer.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,87 @@ lua_check_consumer(struct lua_State *L, int index) {
return *consumer_p;
}

int
lua_consumer_pause(struct lua_State *L) {
if (lua_gettop(L) != 2 || !lua_istable(L, 2))
luaL_error(L, "Usage: err = consumer:pause({'topic'})");

consumer_t *consumer = lua_check_consumer(L,1);

rd_kafka_topic_partition_list_t *list = rd_kafka_topic_partition_list_new(lua_objlen(L, 1));

lua_pushnil(L);

// stack now contains: -1 => nil; -2 => table; -3 => consumer
while (lua_next(L, -2)) {
// stack now contains: -1 => value; -2 => key; -3 => table; -4 => consumer
const char *value = lua_tostring(L, -1);

rd_kafka_topic_partition_list_add(list, value, RD_KAFKA_PARTITION_UA);

// pop value, leaving original key
lua_pop(L, 1);
// stack now contains: -1 => key; -2 => table; -3 => consumer
}

rd_kafka_pause_partitions(consumer->rd_consumer, list);


for(int i=0; i<list->cnt;i++) {
if (list->elems[i].err != RD_KAFKA_RESP_ERR_NO_ERROR) {
const char *const_err_str = rd_kafka_err2str(list->elems[i].err);
char err_str[512];
strcpy(err_str, const_err_str);
int fail = safe_pushstring(L, err_str);
rd_kafka_topic_partition_list_destroy(list);
return fail ? lua_push_error(L): 1;
}
}
rd_kafka_topic_partition_list_destroy(list);
return 0;
}

int
lua_consumer_resume(struct lua_State *L) {
if (lua_gettop(L) != 2 || !lua_istable(L, 2))
luaL_error(L, "Usage: err = consumer:resume({'topic'})");

consumer_t *consumer = lua_check_consumer(L, 1);

rd_kafka_topic_partition_list_t *list = rd_kafka_topic_partition_list_new(lua_objlen(L, 1));

lua_pushnil(L);
// stack now contains: -1 => nil; -2 => table; -3 => consumer
while (lua_next(L, -2)) {
// stack now contains: -1 => value; -2 => key; -3 => table; -4 => consumer
const char *value = lua_tostring(L, -1);

rd_kafka_topic_partition_list_add(list, value, RD_KAFKA_PARTITION_UA);
// pop value, leaving original key
lua_pop(L, 1);
// stack now contains: -1 => key; -2 => table; -3 => consumer
}
// stack now contains: -1 => table; -2 => consumer

rd_kafka_resume_partitions(consumer->rd_consumer, list);


for(int i=0; i<list->cnt;i++) {
if (list->elems[i].err != RD_KAFKA_RESP_ERR_NO_ERROR) {
const char *const_err_str = rd_kafka_err2str(list->elems[i].err);
char err_str[512];
strcpy(err_str, const_err_str);
int fail = safe_pushstring(L, err_str);
rd_kafka_topic_partition_list_destroy(list);
return fail ? lua_push_error(L): 1;
}
}

rd_kafka_topic_partition_list_destroy(list);

return 0;
}

int
lua_consumer_subscribe(struct lua_State *L) {
if (lua_gettop(L) != 2 || !lua_istable(L, 2))
Expand Down
4 changes: 4 additions & 0 deletions kafka/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ int lua_consumer_subscribe(struct lua_State *L);

int lua_consumer_unsubscribe(struct lua_State *L);

int lua_consumer_pause(struct lua_State *L);

int lua_consumer_resume(struct lua_State *L);

int lua_consumer_tostring(struct lua_State *L);

int lua_consumer_poll_msg(struct lua_State *L);
Expand Down
8 changes: 8 additions & 0 deletions kafka/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ function Consumer:close()
return ok, err
end

function Consumer:pause(topics)
return self._consumer:pause(topics)
end

function Consumer:resume(topics)
return self._consumer.resume(topics)
end

function Consumer:subscribe(topics)
return self._consumer:subscribe(topics)
end
Expand Down
2 changes: 2 additions & 0 deletions kafka/tnt_kafka.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ luaopen_kafka_tntkafka(lua_State *L) {
static const struct luaL_Reg consumer_methods [] = {
{"subscribe", lua_consumer_subscribe},
{"unsubscribe", lua_consumer_unsubscribe},
{"pause", lua_consumer_pause},
{"resume", lua_consumer_resume},
{"poll_msg", lua_consumer_poll_msg},
{"poll_logs", lua_consumer_poll_logs},
{"poll_errors", lua_consumer_poll_errors},
Expand Down