Skip to content

Commit

Permalink
fix hang on Win if server is off
Browse files Browse the repository at this point in the history
  • Loading branch information
pfpulux committed Mar 27, 2024
1 parent b341fe2 commit b708655
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/redis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ proc managedSend(
r: Redis | AsyncRedis, data: string
): Future[void] {.multisync.} =
when r is Redis:
r.socket.send(data)
when defined(windows):
if not r.socket.tryRecv(data):
r.socket.close()
else:
r.socket.send(data)
else:
proc doSend() =
r.currentCommand = some(data)
Expand Down Expand Up @@ -157,7 +161,7 @@ proc managedRecvLine(r: Redis | AsyncRedis): Future[string] {.multisync.} =
result = ""
else:
when r is Redis:
let taintedResult: TaintedString = recvLine(r.socket)
let taintedResult = recvLine(r.socket)
result = $taintedResult
else:
result = await recvLine(r.socket)
Expand Down Expand Up @@ -237,7 +241,7 @@ proc readSingleString(
# Some commands return a /bulk/ value or a /multi-bulk/ nil. Odd.
if allowMBNil:
if line == "*-1":
return
return

if line[0] != '$':
raiseInvalidReply(r, '$', line[0])
Expand Down Expand Up @@ -342,8 +346,8 @@ proc flushPipeline*(r: Redis | AsyncRedis, wasMulti = false): Future[RedisList]
for i in 0..tot-1:
var ret = await r.readNext()
for item in ret:
if not (item.contains("OK") or item.contains("QUEUED")):
result.add(item)
if not (item.contains("OK") or item.contains("QUEUED")):
result.add(item)

r.pipeline.expected = 0

Expand Down Expand Up @@ -727,7 +731,7 @@ proc bRPopLPush*(r: Redis | AsyncRedis, source, destination: string,
await r.sendCommand("BRPOPLPUSH", source, @[destination, $timeout])
result = await r.readBulkString(true) # Multi-Bulk nil allowed.

proc lIndex*(r: Redis | AsyncRedis, key: string, index: int): Future[RedisString] {.multisync.} =
proc lIndex*(r: Redis | AsyncRedis, key: string, index: int): Future[RedisString] {.multisync.} =
## Get an element from a list by its index
await r.sendCommand("LINDEX", key, @[$index])
result = await r.readBulkString()
Expand Down Expand Up @@ -789,7 +793,7 @@ proc lSet*(r: Redis | AsyncRedis, key: string, index: int, value: string): Futur
await r.sendCommand("LSET", key, @[$index, value])
raiseNoOK(r, await r.readStatus())

proc lTrim*(r: Redis | AsyncRedis, key: string, start, stop: int): Future[void] {.multisync.} =
proc lTrim*(r: Redis | AsyncRedis, key: string, start, stop: int): Future[void] {.multisync.} =
## Trim a list to the specified range
await r.sendCommand("LTRIM", key, @[$start, $stop])
raiseNoOK(r, await r.readStatus())
Expand Down Expand Up @@ -927,7 +931,7 @@ proc zcount*(r: Redis | AsyncRedis, key: string, min: string, max: string): Futu
result = await r.readInteger()

proc zincrby*(r: Redis | AsyncRedis, key: string, increment: string,
member: string): Future[RedisString] {.multisync.} =
member: string): Future[RedisString] {.multisync.} =
## Increment the score of a member in a sorted set
await r.sendCommand("ZINCRBY", key, @[increment, member])
result = await r.readBulkString()
Expand Down Expand Up @@ -1364,9 +1368,9 @@ proc someTests(r: Redis | AsyncRedis, how: SendMode): Future[seq[string]] {.mult
for r in res:
list.add(r)
list.add(await r.get("invalid_key"))
list.add($(await r.lPush("mylist","itema")))
list.add($(await r.lPush("mylist","itemb")))
await r.lTrim("mylist",0,1)
list.add($(await r.lPush("mylist", "itema")))
list.add($(await r.lPush("mylist", "itemb")))
await r.lTrim("mylist", 0, 1)
var p = await r.lRange("mylist", 0, -1)

for i in items(p):
Expand Down

0 comments on commit b708655

Please sign in to comment.