doujiang24 / lua-resty-kafka

Lua kafka client driver for the Openresty based on the cosocket API
BSD 3-Clause "New" or "Revised" License
801 stars 274 forks source link

Scope of the producer and bp #40

Closed paularmand closed 7 years ago

paularmand commented 7 years ago

In the readme example, a broker is instantiated in the scope of a location. Is it possible to do the require statement in an init_by_lua_block? I have the same question for the bp?

So will the following work as expected, or do you believe it not to be thread-safe ?

http {
  lua_package_path "/data/programs/lua-resty-kafka/lib/?.lua;;";

  init_by_lua_block {
    cjson = require("cjson")

    local kafka_broker_list = {{host = "kafka1.mydomain.com", port = 9092}}
    local kafka_producer_config = {producer_type = "async"}

    -- can this be instantiated here ?
    local producer = require "resty.kafka.producer"
    -- edited this line: made bp global VM level 
   bp = producer:new(kafka_broker_list, kafka_producer_config)
  }

  server {
    location /test {
      content_by_lua_block {
        local args, err = ngx.req.get_uri_args()
        local message = {
          test_arg1 = args.test_arg1 or nil
        }
        message = cjson.encode(message)
        local ok, err = bp:send("test_topic", "test_key", message)
        if not ok then
          ngx.say("send err:", err)
          return
        end

        ngx.say("send success, ok:", ok)
      }
    }
  }
}
paularmand commented 7 years ago

After looking in the code, the producer:new includes memoization for the async version, so little is gained from instantiating this at a global vm level. We would probably need add a local var pointing to the global table in the content_by_lua anyways for performance reasons. If anyone can shed further insights, please do.

doujiang24 commented 7 years ago

@paularmand Sorry for the delay. Yes, it's thread-safe for async producer. But I think you need to init producer in init_worker_by_lua, because ngx.timer.at is disabled in int_by_lua. And inited producer can be stored in Lua VM level: https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker