Skip to content

Commit

Permalink
feat(ngx-conf) do not unnecessarily create C* shm
Browse files Browse the repository at this point in the history
When using PostgreSQL as a datastore, there is no need for the C* shm,
to be created and left unused.
  • Loading branch information
thibaultcha committed Jun 20, 2017
1 parent f51c871 commit e811282
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 0 additions & 1 deletion kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ return {
DICTS = {
"kong",
"kong_cache",
"kong_cassandra",
"kong_process_events",
"kong_cluster_events",
},
Expand Down
7 changes: 7 additions & 0 deletions kong/dao/db/cassandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ function _M.new(kong_config)
prepared = true
}

if not ngx.shared.kong_cassandra then
error("cannot use Cassandra datastore: missing shared dict " ..
"'kong_cassandra' in Nginx configuration, are you using a " ..
"custom template? Make sure the 'lua_shared_dict kong_cassandra " ..
"[SIZE];' directive is defined.")
end

local cluster_options = {
shm = "kong_cassandra",
contact_points = kong_config.cassandra_contact_points,
Expand Down
4 changes: 3 additions & 1 deletion kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ lua_max_running_timers 4096;
lua_max_pending_timers 16384;
lua_shared_dict kong 4m;
lua_shared_dict kong_cache ${{MEM_CACHE_SIZE}};
lua_shared_dict kong_cassandra 5m;
lua_shared_dict kong_process_events 1m;
lua_shared_dict kong_cluster_events 1m;
> if database == "cassandra" then
lua_shared_dict kong_cassandra 5m;
> end
lua_socket_log_errors off;
> if lua_ssl_trusted_certificate then
lua_ssl_trusted_certificate '${{LUA_SSL_TRUSTED_CERTIFICATE}}';
Expand Down
14 changes: 14 additions & 0 deletions spec/01-unit/003-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ describe("NGINX conf compiler", function()
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("client_body_buffer_size 128k", nginx_conf, nil, true)
end)
it("writes kong_cassandra shm if using Cassandra", function()
local conf = assert(conf_loader(nil, {
database = "cassandra",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_shared_dict%s+kong_cassandra", nginx_conf)
end)
it("does not write kong_cassandra shm if not using Cassandra", function()
local conf = assert(conf_loader(nil, {
database = "postgres",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("lua_shared_dict%s+kong_cassandra", nginx_conf)
end)

describe("user directive", function()
it("is not included by default", function()
Expand Down
4 changes: 3 additions & 1 deletion spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ http {
lua_max_pending_timers 16384;
lua_shared_dict kong 4m;
lua_shared_dict kong_cache ${{MEM_CACHE_SIZE}};
lua_shared_dict kong_cassandra 5m;
lua_shared_dict kong_process_events 1m;
lua_shared_dict kong_cluster_events 1m;
> if database == "cassandra" then
lua_shared_dict kong_cassandra 5m;
> end
lua_socket_log_errors off;
> if lua_ssl_trusted_certificate then
lua_ssl_trusted_certificate '${{LUA_SSL_TRUSTED_CERTIFICATE}}';
Expand Down

0 comments on commit e811282

Please sign in to comment.