feat: enable usage in init_by_lua context #20
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 extremelyfamiliar 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 tofallback 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:
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:
init_by_lua
Hence, we only
require()
Luasocket when one of those two conditionarises.