Kong / lua-resty-counter

Lock-free counter for OpenResty
22 stars 11 forks source link

counter integer overflow? #7

Open Triple-Z opened 2 years ago

Triple-Z commented 2 years ago

Is possible that the integer counter would be overflowed to be negative?

fffonion commented 2 years ago

@Triple-Z the value stored in shdict is a double https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_shdict.c#L1795, it should be sufficient for most use cases.

Triple-Z commented 2 years ago

@fffonion Thank you for your reply.

I still have two questions:

  1. In lua.org, it said that when using double to represent an integer, it won't get rounding error unless the number is greater than 10^14. As the counter value is integer, is the maximum value for the counter is 10^14 - 1?If the counter is over the 10^14, should I reset it to zero?
  2. My second question is about double overflow. However, I just learned that double overflow is not like integer overflow (INT_MAX -> INT_MIN), more like a rounding error. So the second question is just like the first question.