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

Refresh key ttl #1

Open
wants to merge 6 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
5 changes: 4 additions & 1 deletion etcd_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# implement/test ttl on dir
# implement reconnect
# implement Atomic Compare-and-Delete & similar
# implement Refreshing key TTL

import httpclient,
logging,
Expand Down Expand Up @@ -140,6 +139,10 @@ proc set*(self: EtcdClient, key, value: string, ttl= -1): JsonNode {.discardable
else:
return self.call_api_form("keys" / key, HttpPut, {"value": value, "ttl": $ttl}, from_key="node")

proc refresh*(self: EtcdClient, key: string, ttl=0): JsonNode {.discardable} =
## Refresh key ttl
return self.call_api_form("keys" / key & "?prevExist=true&refresh=true", HttpPut, {"ttl": $ttl}, from_key="node")

proc create*(self: EtcdClient, key, value: string): JsonNode {.discardable.} =
## Create a new key
return self.call_api_form("keys" / key & "?prevExist=false", HttpPut, {"value": value}, from_key="node")
Expand Down
20 changes: 20 additions & 0 deletions tests/functional.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ suite "functional tests":
let r = c.get(test_key)
##assert r["value"].str == "Foo", $r

test "refresh ttl":
c.set(test_key, "Foo", ttl=1)
let t0 = epochTime()
sleep 800
c.refresh(test_key, ttl=1)
for tries in 0..20:
sleep 100
try:
discard c.get(test_key)
except:
# the key timed out
break

expect Exception:
discard c.get(test_key)

let elapsed = epochTime() - t0
echo elapsed
assert elapsed > 1.8

test "set, get with low TTL":
c.set(test_key, "Foo", ttl=1)
let t0 = epochTime()
Expand Down