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

feature: shdict:incr(): added the "exptime" argument to set the expiry of values when they are first created via the "init" argument. #165

Closed
wants to merge 1 commit into from

Conversation

thibaultcha
Copy link
Member

@thibaultcha thibaultcha commented Jan 8, 2018

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-resty-core project.

Implement the exptime argument in shdict:incr() as discussed in openresty/openresty#328. Contrary to #10, this is only implemented in the FFI counterpart of the shdict API.

If deemed necessary (e.g., for backwards compatibility reasons), I have a commit with support for this argument in the CFunction as well. As per recent guidance, this should apparently not be necessary.

We only set/support the exptime argument when the init argument is given/takes effect, as we do not wish to tamper with the expiry time of values created by other actors than incr().

Sister PR at openresty/lua-nginx-module#1226

end

if has_init == 0 then
error('must provide "init" when providing "exptime"', 2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new case of an shdict API both creating values and updating already existing ones, and now having to deal with conditionally setting their exptime as well.
Unlike set() and expire() which unconditionally replace the exptime, I made incr() only replace it when the value did not exist. If you believe this does not make sense, let me know.

@@ -29,7 +29,7 @@ ffi.cdef[[

int ngx_http_lua_ffi_shdict_incr(void *zone, const unsigned char *key,
size_t key_len, double *value, char **err, int has_init, double init,
int *forcible);
int exptime, int *forcible);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exptime is confusing. We should call it init_ttl?

@@ -337,7 +337,7 @@ local function shdict_get_stale(zone, key)
end


local function shdict_incr(zone, key, value, init)
local function shdict_incr(zone, key, value, init, exptime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

error('bad "exptime" argument', 2)
end

if has_init == 0 then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a boolean typed value here instead of using an integer? It's Lua anyway. Seems like an old issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to update this logic in the above handling of the init argument in the same commit to use a boolean instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaultcha Yes, please.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agentzh Updated, got rid of the has_init variable altogether.

of values when they are first created via the "init" argument.
end

local rc = C.ngx_http_lua_ffi_shdict_incr(zone, key, key_len, num_value,
errmsg, has_init, init,
errmsg, init and 1 or 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I now see why it used has_init as an integer in the first place...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... However, this patch still looks like an improvement to me though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaultcha I didn't disagree :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅


if not init then
error('must provide "init" when providing "init_ttl"', 2)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really happy with such complex argument value check. It looks branchy and expensive. Will you simplify it in a future PR? Thanks!

@agentzh
Copy link
Member

agentzh commented Jan 10, 2018

Merged with comments :) Thanks!

@agentzh agentzh closed this Jan 10, 2018
@thibaultcha thibaultcha deleted the feat/incr-exptime branch January 10, 2018 01:50
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.

None yet

2 participants