Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable usage in init_by_lua context #20

Merged
merged 1 commit into from
Feb 10, 2016

Conversation

thibaultcha
Copy link
Contributor

Hey there!

I wanted to comment on #11 but at the same time, the trickiness of
the patch made me want not to come empty-handed to the discussion.

So here is a patch enabling the use of pgmoon in the init_by_lua
context. It is a handy feature to have since performing queries before
the workers start could be much simpler than having to deal with
concurrency issues in init_worker_by_lua. As I am not extremely
familiar with Moonscript, please excuse inconsistencies with the
existing code style if any, I will fix them if this change is considered.

Changes

The trick to enabling the use inside of init_by_lua is to
fallback to Luasocket without being stuck with it in future contexts,
which means we must check whether cosockets are supported every
time we are trying to create one.

If we leave the previous behavior, and check for the availability of
cosockets at the module level, like so:

if ngx and ngx.get_phase() == "init" then
  -- return the luasocket constructor
else
  -- return the cosocket constructor
end

Now pgmoon will always create a luasocket instead of a cosocket in
future contexts, since the module is required only once.

Hence, we must ensure of the availability of cosockets in future calls
to query(), in order to not be stuck to using luasocket.

However, we do not want users using pgmoon only in the cosocket-enabled
contexts to depend on Luasocket. This dependency is only required when:

  • pgmoon runs in plain Lua
  • pgmoon runs in init_by_lua

Hence, we only require() Luasocket when one of those two condition
arises.

This addresses leafo#11, and allows pgmoon to be used in the 'init_by_lua'
context. A major change compared to the previous implementation is that
now, we must check whether cosockets are supported every time we are
trying to create one.

If we leave the previous behavior, and check for the availability of
cosockets at the module level, like so:

  if ngx and ngx.get_phase() == "init" then
    -- return the luasocket constructor
  else
    -- return the cosocket constructor
  end

Now pgmoon will always create a luasocket instead of a cosocket in
future contexts, since the module is required only once.

Hence, we must ensure of the availability of cosockets in future calls
to 'query()', in order to not be stuck to using luasocket.

However, we do not want users using pgmoon only in the cosocket-enabled
contexts to depend on Luasocket. This dependency is only required when:

- pgmoon runs in plain Lua
- pgmoon runs in init_by_lua

Hence, we only 'require()' Luasocket when one of those two condition
arises.
leafo added a commit that referenced this pull request Feb 10, 2016
feat: enable usage in init_by_lua context
@leafo leafo merged commit 8cc7030 into leafo:master Feb 10, 2016
@leafo
Copy link
Owner

leafo commented Feb 10, 2016

Thanks for the patch, looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants