Skip to content

Commit b708655

Browse files
committed
fix hang on Win if server is off
1 parent b341fe2 commit b708655

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/redis.nim

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ proc managedSend(
126126
r: Redis | AsyncRedis, data: string
127127
): Future[void] {.multisync.} =
128128
when r is Redis:
129-
r.socket.send(data)
129+
when defined(windows):
130+
if not r.socket.tryRecv(data):
131+
r.socket.close()
132+
else:
133+
r.socket.send(data)
130134
else:
131135
proc doSend() =
132136
r.currentCommand = some(data)
@@ -157,7 +161,7 @@ proc managedRecvLine(r: Redis | AsyncRedis): Future[string] {.multisync.} =
157161
result = ""
158162
else:
159163
when r is Redis:
160-
let taintedResult: TaintedString = recvLine(r.socket)
164+
let taintedResult = recvLine(r.socket)
161165
result = $taintedResult
162166
else:
163167
result = await recvLine(r.socket)
@@ -237,7 +241,7 @@ proc readSingleString(
237241
# Some commands return a /bulk/ value or a /multi-bulk/ nil. Odd.
238242
if allowMBNil:
239243
if line == "*-1":
240-
return
244+
return
241245

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

348352
r.pipeline.expected = 0
349353

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

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

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

929933
proc zincrby*(r: Redis | AsyncRedis, key: string, increment: string,
930-
member: string): Future[RedisString] {.multisync.} =
934+
member: string): Future[RedisString] {.multisync.} =
931935
## Increment the score of a member in a sorted set
932936
await r.sendCommand("ZINCRBY", key, @[increment, member])
933937
result = await r.readBulkString()
@@ -1364,9 +1368,9 @@ proc someTests(r: Redis | AsyncRedis, how: SendMode): Future[seq[string]] {.mult
13641368
for r in res:
13651369
list.add(r)
13661370
list.add(await r.get("invalid_key"))
1367-
list.add($(await r.lPush("mylist","itema")))
1368-
list.add($(await r.lPush("mylist","itemb")))
1369-
await r.lTrim("mylist",0,1)
1371+
list.add($(await r.lPush("mylist", "itema")))
1372+
list.add($(await r.lPush("mylist", "itemb")))
1373+
await r.lTrim("mylist", 0, 1)
13701374
var p = await r.lRange("mylist", 0, -1)
13711375

13721376
for i in items(p):

0 commit comments

Comments
 (0)