Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #30 from holdenk/master

Guard against creating multiple ES clients.
  • Loading branch information...
commit 5a900795e56b2e68830bef22e6bf850fd446024b 2 parents 98f9246 + a2ccf5f
@holdenk holdenk authored
View
2  build.sbt
@@ -1,6 +1,6 @@
name := "slashem"
-version := "0.9.11"
+version := "0.9.12"
organization := "com.foursquare"
View
41 src/main/scala/com/foursquare/slashem/Schema.scala
@@ -219,29 +219,40 @@ trait ElasticMeta[T <: Record[T]] extends SlashemMeta[T] {
new InetSocketTransportAddress(s, p.toInt)})
var node: Node = null
- var myClient: Option[Client]
+ //This is volatile for double check locking to work see http://jeremymanson.blogspot.com/2008/05/double-checked-locking.html
+ //This requires JDK5 or later
+ @volatile var myClient: Client = null
+ val clientCreateLock : AnyRef = new Object()
val executorService: ExecutorService = Executors.newCachedThreadPool()
val executorServiceFuturePool: FuturePool = FuturePool(executorService)
/** Create or get the MetaRecord's client */
def client: Client = {
- myClient match {
- case Some(cl) => cl
- case _ => {
- myClient = Some({
- if (useTransport) {
- val settings = ImmutableSettings.settingsBuilder().put("cluster.name",clusterName).put("client.transport.sniff",sniffMode)
- val tc = new TransportClient(settings)
- serverInetSockets.map(tc.addTransportAddress(_))
- tc
- } else {
- node.client()
- }
- })
- myClient.get
+ //Double check locking (safe with new JDKs and the @volatile up above ^)
+ if (myClient == null) {
+ clientCreateLock.synchronized {
+ if (myClient == null) {
+ myClient =
+ if (useTransport) {
+ val settings = ImmutableSettings.settingsBuilder().put("cluster.name",clusterName).put("client.transport.sniff",sniffMode)
+ val tc = new TransportClient(settings)
+ serverInetSockets.map(tc.addTransportAddress(_))
+ tc
+ } else {
+ node.client()
+ }
+ //When shut down the JVM we want to explicitly shut down our connections
+ //so we are a well behaved client
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ override def run(): Unit = {
+ myClient.close();
+ }
+ });
+ }
}
}
+ myClient
}
}
View
2  src/test/scala/com/foursquare/slashem/ElasticTest.scala
@@ -4,7 +4,6 @@ object ESimplePanda extends ESimplePanda with ElasticMeta[ESimplePanda] {
//Force local for testing
override val useTransport = false
override val clusterName = "simpletest" //Override me knthx
- var myClient:Option[org.elasticsearch.client.Client] = None
}
class ESimplePanda extends ElasticSchema[ESimplePanda] {
def meta = ESimplePanda
@@ -26,7 +25,6 @@ object ESimpleGeoPanda extends ESimpleGeoPanda with ElasticMeta[ESimpleGeoPanda]
override val useTransport = false
override val clusterName = "simpletest" //Override me knthx
override val indexName = "geopanda"
- var myClient:Option[org.elasticsearch.client.Client] = None
}
class ESimpleGeoPanda extends ElasticSchema[ESimpleGeoPanda] {
def meta = ESimpleGeoPanda
Please sign in to comment.
Something went wrong with that request. Please try again.