Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Guard against creating multiple ES clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holden Karau committed May 3, 2012
1 parent fa4754e commit ac77c64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "slashem"

version := "0.9.11"
version := "0.9.12"

organization := "com.foursquare"

Expand Down
36 changes: 21 additions & 15 deletions src/main/scala/com/foursquare/slashem/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,29 +219,35 @@ trait ElasticMeta[T <: Record[T]] extends SlashemMeta[T] {
new InetSocketTransportAddress(s, p.toInt)})

var node: Node = null
var myClient: Option[Client]
@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
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()
}
Runtime.getRuntime().addShutdownHook(new Thread() {
override def run(): Unit = {
myClient.close();
}
});
}
}
}
myClient
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/test/scala/com/foursquare/slashem/ElasticTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ac77c64

Please sign in to comment.