-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ServiceDiscovery#getInstance impl
add ServiceDiscovery#getInstanceTtl impl
- Loading branch information
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
discovery/src/main/resources/discovery_get_instance_ttl.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local namespace = KEYS[1]; | ||
local serviceId = KEYS[2]; | ||
local instanceId = KEYS[3]; | ||
local instanceIdxKey = namespace .. ':svc_itc_idx:' .. serviceId; | ||
|
||
local function getInstanceKey(instanceId) | ||
return namespace .. ":svc_itc:" .. instanceId; | ||
end | ||
|
||
local function ensureNotExpired(instanceIdxKey, instanceId) | ||
local instanceKey = getInstanceKey(instanceId); | ||
local instanceTtl = redis.call("ttl", instanceKey); | ||
-- -2: The key doesn't exist | -1: The key is fixed | >0: ttl(second) | ||
if instanceTtl == -2 then | ||
redis.call("srem", instanceIdxKey, instanceId); | ||
redis.call("del", instanceKey); | ||
redis.call("publish", instanceKey, "expired"); | ||
end | ||
|
||
return instanceTtl; | ||
end | ||
|
||
local instanceTtl = ensureNotExpired(instanceIdxKey, instanceId); | ||
if instanceTtl == -1 or instanceTtl == -2 then | ||
return instanceTtl; | ||
else | ||
local nowTime = redis.call('time')[1]; | ||
local ttlAt = nowTime + instanceTtl; | ||
return ttlAt; | ||
end |