Skip to content

Commit 85bc25c

Browse files
committed
fixes issue debasishg#18 debasishg#18 (reconnect after idle time) - implemented retry in RedisClient#send
1 parent c9f127c commit 85bc25c

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
target
3-
project/boot
3+
project/boot
4+
*.swp

project/ScalaRedisProject.scala

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import Keys._
33

44
object ScalaRedisProject extends Build
55
{
6+
import Resolvers._
67
lazy val root = Project("RedisClient", file(".")) settings(coreSettings : _*)
78

89
lazy val commonSettings: Seq[Setting[_]] = Seq(
910
organization := "net.debasishg",
1011
version := "2.6",
1112
scalaVersion := "2.9.2",
12-
scalacOptions ++= Seq("-deprecation", "-unchecked")
13+
scalacOptions ++= Seq("-deprecation", "-unchecked"),
14+
resolvers ++= Seq(twitterRepo)
1315
)
1416

1517
lazy val coreSettings = commonSettings ++ template ++ Seq(
@@ -21,8 +23,8 @@ object ScalaRedisProject extends Build
2123
"log4j" % "log4j" % "1.2.16" % "provided",
2224
"junit" % "junit" % "4.8.1" % "test",
2325
"org.scalatest" % "scalatest_2.9.1" % "1.6.1" % "test",
24-
"com.twitter" % "util" % "1.11.4" % "test" intransitive(),
25-
"com.twitter" % "finagle-core" % "1.9.0" % "test"),
26+
"com.twitter" % "util_2.9.1" % "1.12.13" % "test" intransitive(),
27+
"com.twitter" % "finagle-core_2.9.1" % "4.0.2" % "test"),
2628

2729
parallelExecution in Test := false,
2830
publishTo <<= version { (v: String) =>
@@ -84,3 +86,7 @@ object ScalaRedisProject extends Build
8486
(output ** filter).get
8587
}
8688
}
89+
90+
object Resolvers {
91+
val twitterRepo = "release" at "http://maven.twttr.com"
92+
}

src/main/scala/com/redis/IO.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.redis
22

33
import java.io._
4-
import java.net.Socket
4+
import java.net.{Socket, InetSocketAddress}
55

66
import serialization.Parse.parseStringSafe
77

@@ -15,7 +15,7 @@ trait IO extends Log {
1515
var db: Int = _
1616

1717
def connected = {
18-
socket != null
18+
socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown();
1919
}
2020
def reconnect = {
2121
disconnect && connect
@@ -25,9 +25,11 @@ trait IO extends Log {
2525
def connect: Boolean = {
2626
try {
2727
socket = new Socket(host, port)
28+
2829
socket.setSoTimeout(0)
2930
socket.setKeepAlive(true)
3031
socket.setTcpNoDelay(true)
32+
3133
out = socket.getOutputStream
3234
in = new BufferedInputStream(socket.getInputStream)
3335
true
@@ -83,9 +85,7 @@ trait IO extends Log {
8385
var found: List[Int] = Nil
8486
var build = new scala.collection.mutable.ArrayBuilder.ofByte
8587
while (delimiter != Nil) {
86-
val next = try {
87-
in.read
88-
} catch {case e => -1}
88+
val next = in.read
8989
if (next < 0) return null
9090
if (next == delimiter.head) {
9191
found ::= delimiter.head

src/main/scala/com/redis/RedisClient.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@ object RedisClient {
1414
}
1515

1616
trait Redis extends IO with Protocol {
17-
def send[A](command: String, args: Seq[Any])(result: => A)(implicit format: Format): A = {
17+
18+
def send[A](command: String, args: Seq[Any])(result: => A)(implicit format: Format): A = try {
1819
write(Commands.multiBulk(command.getBytes("UTF-8") +: (args map (format.apply))))
1920
result
21+
} catch {
22+
case e: RedisConnectionException =>
23+
if (reconnect) send(command, args)(result)
24+
else throw e
2025
}
21-
def send[A](command: String)(result: => A): A = {
26+
27+
def send[A](command: String)(result: => A): A = try {
2228
write(Commands.multiBulk(List(command.getBytes("UTF-8"))))
2329
result
30+
} catch {
31+
case e: RedisConnectionException =>
32+
if (reconnect) send(command)(result)
33+
else throw e
2434
}
35+
2536
def cmd(args: Seq[Array[Byte]]) = Commands.multiBulk(args)
2637

2738
protected def flattenPairs(in: Iterable[Product2[Any, Any]]): List[Any] =

src/test/scala/com/redis/StringOperationsSpec.scala

+13
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,17 @@ class StringOperationsSpec extends Spec
255255
r.getbit("mykey", 100) should equal(Some(0))
256256
}
257257
}
258+
259+
/** uncomment to test timeout : need a custom redis.conf
260+
describe("timeout") {
261+
it("should append value to that of a key") {
262+
r.set("mykey", "Hello World")
263+
r.strlen("mykey") should equal(Some(11))
264+
r.strlen("nonexisting") should equal(Some(0))
265+
Thread.sleep(150000)
266+
r.set("nonexisting", "Hello World")
267+
r.strlen("nonexisting") should equal(Some(11))
268+
}
269+
}
270+
**/
258271
}

0 commit comments

Comments
 (0)