Permalink
Browse files

Guard against creating multiple ES clients.

  • Loading branch information...
1 parent fa4754e commit ac77c6466d59455f39893fc036954b9d12cda7ed Holden Karau committed May 3, 2012
View
@@ -1,6 +1,6 @@
name := "slashem"
-version := "0.9.11"
+version := "0.9.12"
organization := "com.foursquare"
@@ -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
}
}
@@ -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

0 comments on commit ac77c64

Please sign in to comment.