Skip to content

Commit

Permalink
Extend lease if it's less than expires_in and we're re-using the one …
Browse files Browse the repository at this point in the history
…currently assigned to the device
  • Loading branch information
arr2036 committed Apr 4, 2019
1 parent 56df130 commit 2b8795c
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/modules/rlm_redis_ippool/rlm_redis_ippool.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,37 +191,40 @@ static char lua_alloc_cmd[] =
" if expires_in > 0 then" EOL /* 11 */
" ip = redis.call('HMGET', '{' .. KEYS[1] .. '}:"IPPOOL_ADDRESS_KEY":' .. exists, 'device', 'range', 'counter')" EOL /* 12 */
" if ip and (ip[1] == ARGV[3]) then" EOL /* 13 */
" return {" STRINGIFY(_IPPOOL_RCODE_SUCCESS) ", exists, ip[2], expires_in, ip[3] }" EOL /* 14 */
" end" EOL /* 15 */
" end" EOL /* 16 */
"end" EOL /* 17 */
" if expires_in < ARGV[2] then" EOL /* 14 */
" redis.call('ZADD', pool_key, ARGV[1] + ARGV[2], ip[1])" EOL /* 15 */
" end" EOL /* 16 */
" return {" STRINGIFY(_IPPOOL_RCODE_SUCCESS) ", exists, ip[2], expires_in, ip[3] }" EOL /* 17 */
" end" EOL /* 18 */
" end" EOL /* 19 */
"end" EOL /* 20 */

/*
* Else, get the IP address which expired the longest time ago.
*/
"ip = redis.call('ZREVRANGE', pool_key, -1, -1, 'WITHSCORES')" EOL /* 18 */
"if not ip or not ip[1] then" EOL /* 19 */
" return {" STRINGIFY(_IPPOOL_RCODE_POOL_EMPTY) "}" EOL /* 20 */
"end" EOL /* 21 */
"if ip[2] >= ARGV[1] then" EOL /* 22 */
"ip = redis.call('ZREVRANGE', pool_key, -1, -1, 'WITHSCORES')" EOL /* 21 */
"if not ip or not ip[1] then" EOL /* 22 */
" return {" STRINGIFY(_IPPOOL_RCODE_POOL_EMPTY) "}" EOL /* 23 */
"end" EOL /* 24 */
"redis.call('ZADD', pool_key, ARGV[1] + ARGV[2], ip[1])" EOL /* 25 */
"if ip[2] >= ARGV[1] then" EOL /* 25 */
" return {" STRINGIFY(_IPPOOL_RCODE_POOL_EMPTY) "}" EOL /* 26 */
"end" EOL /* 27 */
"redis.call('ZADD', pool_key, ARGV[1] + ARGV[2], ip[1])" EOL /* 28 */

/*
* Set the device/gateway keys
*/
"address_key = '{' .. KEYS[1] .. '}:"IPPOOL_ADDRESS_KEY":' .. ip[1]" EOL /* 26 */
"redis.call('HMSET', address_key, 'device', ARGV[3], 'gateway', ARGV[4])" EOL /* 27 */
"redis.call('SET', device_key, ip[1])" EOL /* 28 */
"redis.call('EXPIRE', device_key, ARGV[2])" EOL /* 29 */
"return { " EOL /* 30 */
" " STRINGIFY(_IPPOOL_RCODE_SUCCESS) "," EOL /* 31 */
" ip[1], " EOL /* 32 */
" redis.call('HGET', address_key, 'range'), " EOL /* 33 */
" tonumber(ARGV[2]), " EOL /* 34 */
" redis.call('HINCRBY', address_key, 'counter', 1)" EOL /* 35 */
"}" EOL; /* 36 */
"address_key = '{' .. KEYS[1] .. '}:"IPPOOL_ADDRESS_KEY":' .. ip[1]" EOL /* 29 */
"redis.call('HMSET', address_key, 'device', ARGV[3], 'gateway', ARGV[4])" EOL /* 30 */
"redis.call('SET', device_key, ip[1])" EOL /* 31 */
"redis.call('EXPIRE', device_key, ARGV[2])" EOL /* 32 */
"return { " EOL /* 33 */
" " STRINGIFY(_IPPOOL_RCODE_SUCCESS) "," EOL /* 34 */
" ip[1], " EOL /* 35 */
" redis.call('HGET', address_key, 'range'), " EOL /* 36 */
" tonumber(ARGV[2]), " EOL /* 37 */
" redis.call('HINCRBY', address_key, 'counter', 1)" EOL /* 38 */
"}" EOL; /* 39 */
static char lua_alloc_digest[(SHA1_DIGEST_LENGTH * 2) + 1];

/** Lua script for updating leases
Expand Down

0 comments on commit 2b8795c

Please sign in to comment.