Sitelet https://github.com/Philippus/elastic4s/commit/8c31bd43fe0aa52e45caa3ccc87a0eabe26c665b
Skip to content

Commit 8c31bd4

Browse files
authored
Close Sniffer in JavaClientSniffed after use (#4114)
1 parent 4a395e6 commit 8c31bd4

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎elastic4s-client-esjava/src/main/scala/com/sksamuel/elastic4s/http/JavaClient.scala‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sksamuel.elastic4s.http
22

3-
import java.io.InputStream
3+
import java.io.{Closeable, InputStream}
44
import java.nio.charset.StandardCharsets
55
import java.util.zip.GZIPInputStream
66
import com.sksamuel.elastic4s.{
@@ -24,7 +24,8 @@ case class JavaClientExceptionWrapper(t: Throwable) extends RuntimeException(t)
2424

2525
/** An implementation of HttpClient that wraps the Elasticsearch Java Rest Client
2626
*/
27-
class JavaClient(client: RestClient)(implicit ec: ExecutionContext) extends HttpClient[Future] {
27+
class JavaClient(client: RestClient, onClose: Option[Closeable] = None)(implicit ec: ExecutionContext)
28+
extends HttpClient[Future] {
2829

2930
def apacheEntity(entity: HttpEntity): AbstractHttpEntity = entity match {
3031
case e: HttpEntity.StringEntity =>
@@ -87,7 +88,10 @@ class JavaClient(client: RestClient)(implicit ec: ExecutionContext) extends Http
8788
promise.future
8889
}
8990

90-
override def close(): Future[Unit] = Future(client.close())
91+
override def close(): Future[Unit] = Future {
92+
onClose.foreach(_.close()) // close sniffer first, it references the client
93+
client.close()
94+
}
9195

9296
private def isEntityGziped(entity: org.apache.http.HttpEntity): Boolean = {
9397
Option(entity.getContentEncoding).flatMap(x => Option(x.getValue)).contains("gzip")
@@ -105,7 +109,8 @@ object JavaClient {
105109
* @return
106110
* newly created Scala client
107111
*/
108-
def fromRestClient(client: RestClient)(implicit ec: ExecutionContext): JavaClient = new JavaClient(client)
112+
def fromRestClient(client: RestClient, onClose: Option[Closeable] = None)(implicit ec: ExecutionContext): JavaClient =
113+
new JavaClient(client, onClose)
109114

110115
/** Creates a new [[ElasticClient]] using the elasticsearch Java API rest client as the underlying client. Optional
111116
* callbacks can be passed in to configure the client.

‎elastic4s-client-sniffed/src/main/scala/com/sksamuel/elastic4s/http/JavaClientSniffed.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ object JavaClientSniffed {
8282

8383
if (sniffAfterFailureInterval.isDefined) failureSniffer.setSniffer(sniffer)
8484

85-
fromRestClient(client)
85+
fromRestClient(client, Some(sniffer)) // Sniffer implements java.io.Closeable
8686
}
8787
}

0 commit comments

Comments
 (0)