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

New atomic functions(cas, cog) for shm #1955

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ install:
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
- git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
- git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone -b atomic_shm https://github.com/webcore-no/lua-resty-core.git ../lua-resty-core
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql
- git clone https://github.com/openresty/lua-resty-string.git ../lua-resty-string
- git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module
- git clone -b atomic_shm https://github.com/webcore-no/stream-lua-nginx-module.git ../stream-lua-nginx-module
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git luajit2

before_script:
Expand Down
46 changes: 46 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3581,10 +3581,12 @@ Nginx API for Lua
* [ngx.shared.DICT](#ngxshareddict)
* [ngx.shared.DICT.get](#ngxshareddictget)
* [ngx.shared.DICT.get_stale](#ngxshareddictget_stale)
* [ngx.shared.DICT](#ngxshareddict)
* [ngx.shared.DICT.set](#ngxshareddictset)
* [ngx.shared.DICT.safe_set](#ngxshareddictsafe_set)
* [ngx.shared.DICT.add](#ngxshareddictadd)
* [ngx.shared.DICT.safe_add](#ngxshareddictsafe_add)
* [ngx.shared.DICT.cas](#ngxshareddictcas)
* [ngx.shared.DICT.replace](#ngxshareddictreplace)
* [ngx.shared.DICT.delete](#ngxshareddictdelete)
* [ngx.shared.DICT.incr](#ngxshareddictincr)
Expand Down Expand Up @@ -6784,10 +6786,12 @@ The resulting object `dict` has the following methods:

* [get](#ngxshareddictget)
* [get_stale](#ngxshareddictget_stale)
* [get_if_not_eq](#ngxshareddictget_if_not_eq)
* [set](#ngxshareddictset)
* [safe_set](#ngxshareddictsafe_set)
* [add](#ngxshareddictadd)
* [safe_add](#ngxshareddictsafe_add)
* [cas](#ngxshareddictcas)
* [replace](#ngxshareddictreplace)
* [delete](#ngxshareddictdelete)
* [incr](#ngxshareddictincr)
Expand Down Expand Up @@ -6912,6 +6916,26 @@ See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT
---------------
**syntax:** *value, flags = ngx.shared.DICT:get_if_not_eq(key, old_value?, old_flags?)*

**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua**

Similar to the [get](#ngxshareddictget) method, but only returns if
Copy link
Member

Choose a reason for hiding this comment

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

What does the "only returns if" part mean? If the if condition is not met, this method will never return? It cannot be. Maybe you mean "will only return the value and the flags" here? And you should also be explicit about what values it returns otherwise (like two nil values?)

The cog name is a bit confusing. How about get_if_not_eq?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah the "only returns if" part is not true.
Maybe something like:

Similar to the [get](#ngxshareddictget) method, but if ``old_value`` and
``old_flags`` match value and flag in dictionary ngx.shared.DICT, 
returns ``nil, true``.

I'm fine with get_if_not_eq

`old_value` or `old_flags` do not match.

If `old_value` or `old_flags` is `nil`
it will be ignored when comparing.
Copy link
Member

Choose a reason for hiding this comment

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

This semantics does not feel right to me. When old value is nil, then it means the expected value in the shdict should also be nil. It should not get ignored? So it always return nil, nilin this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function first lookups the key, and if cant find it return nil, nil.
And after that apply comparison logic.

The reason for the "don't compare nil" is because of old_flags, and i simply tried to have old_value and old_flags act as similar as possible.

For old_flags i added a distinction between 0 and nil simply because it adds more functionality compared to having nil == 0.

Its also the fact that flags cannot exist if value is nil. Does that make flags nil or 0 maybe both maybe neither?

Having different logic where only old_flags is not compared when nil, might be best.

Copy link

Choose a reason for hiding this comment

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

When old value is nil, it should be ignored when comparing (so value could be anything), when old flag is nil, flag is ignored.
This way the api user could have either old_value or old_flag (or both) and whichever is not nil will be compared to the value in shm


In case of match, `nil, false` will be returned.

This method was first introduced in the `0.10.21` release.

See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.set
-------------------

Expand Down Expand Up @@ -7012,6 +7036,28 @@ See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.cas
-------------------
**syntax:** *success, err, forcible = ngx.shared.DICT:cas(key, old_value?, old_flags?, value?, flags?, exptime?)*

**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua**

Conditionally sets key-value pair in shm.

If `old_value` or `old_flags` is `nil` it will
be ignored.

If either `value` or `flags` is `nil` it will
remain unchanged. If both are `nil`, key-value pair will be deleted.

In case of mismatch, `false, false` will be returned.

This method was first introduced in the `0.10.21` release.

See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.replace
-----------------------

Expand Down
38 changes: 38 additions & 0 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -5739,10 +5739,12 @@ The resulting object <code>dict</code> has the following methods:

* [[#ngx.shared.DICT.get|get]]
* [[#ngx.shared.DICT.get_stale|get_stale]]
* [[#ngx.shared.DICT.get_if_not_eq|get_if_not_eq]]
* [[#ngx.shared.DICT.set|set]]
* [[#ngx.shared.DICT.safe_set|safe_set]]
* [[#ngx.shared.DICT.add|add]]
* [[#ngx.shared.DICT.safe_add|safe_add]]
* [[#ngx.shared.DICT.cas|cas]]
* [[#ngx.shared.DICT.replace|replace]]
* [[#ngx.shared.DICT.delete|delete]]
* [[#ngx.shared.DICT.incr|incr]]
Expand Down Expand Up @@ -5855,6 +5857,23 @@ This method was first introduced in the <code>0.8.6</code> release.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT ==
'''syntax:''' ''value, flags = ngx.shared.DICT:get_if_not_eq(key, old_value?, old_flags?)''

'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua*''

Similar to the [[#ngx.shared.DICT.get|get]] method, but only returns if
<code>old_value</code> or <code>old_flags</code> do not match.

If <code>old_value</code> or <code>old_flags</code> is <code>nil</code>
it will be ignored when comparing.

In case of match, <code>nil, false</code> will be returned.

This method was first introduced in the <code>0.10.21</code> release.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.set ==

'''syntax:''' ''success, err, forcible = ngx.shared.DICT:set(key, value, exptime?, flags?)''
Expand Down Expand Up @@ -5941,6 +5960,25 @@ This feature was first introduced in the <code>v0.7.18</code> release.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.cas ==
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:cas(key, old_value?, old_flags?, value?, flags?, exptime?)''

'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua*''

Conditionally sets key-value pair in shm.

If <code>old_value</code> or <code>old_flags</code> is <code>nil</code> it will
be ignored.

If either <code>value</code> or <code>flags</code> is <code>nil</code> it will
remain unchanged. If both are <code>nil</code>, key-value pair will be deleted.

In case of mismatch, <code>false, false</code> will be returned.

This method was first introduced in the <code>0.10.21</code> release.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.replace ==

'''syntax:''' ''success, err, forcible = ngx.shared.DICT:replace(key, value, exptime?, flags?)''
Expand Down
Loading