Skip to content

Commit b85201a

Browse files
committed
fix(voip): reuse OkHttpClient and close response body in MediaCallsAnswerRequest
Address CodeRabbit resource leak warning: reuse the OkHttpClient singleton instead of creating a new instance per request, and wrap the Response in response.use {} to ensure the body is closed after use.
1 parent 2188f77 commit b85201a

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

android/app/src/main/java/chat/rocket/reactnative/voip/DDPClient.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ class DDPClient {
2121

2222
companion object {
2323
private const val TAG = "RocketChat.DDPClient"
24+
private val sharedClient = OkHttpClient.Builder()
25+
.pingInterval(30, TimeUnit.SECONDS)
26+
.build()
2427
}
2528

2629
private var webSocket: WebSocket? = null
27-
private var client: OkHttpClient? = null
30+
private val client: OkHttpClient = sharedClient
2831
private var sendCounter = 0
2932
private var isConnected = false
3033
private val mainHandler = Handler(Looper.getMainLooper())
@@ -40,14 +43,9 @@ class DDPClient {
4043

4144
Log.d(TAG, "Connecting to $wsUrl")
4245

43-
val httpClient = OkHttpClient.Builder()
44-
.pingInterval(30, TimeUnit.SECONDS)
45-
.build()
46-
client = httpClient
47-
4846
val request = Request.Builder().url(wsUrl).build()
4947

50-
webSocket = httpClient.newWebSocket(request, object : WebSocketListener() {
48+
webSocket = client.newWebSocket(request, object : WebSocketListener() {
5149
override fun onOpen(webSocket: WebSocket, response: Response) {
5250
Log.d(TAG, "WebSocket opened")
5351
val connectMsg = JSONObject().apply {
@@ -142,8 +140,6 @@ class DDPClient {
142140
onCollectionMessage = null
143141
webSocket?.close(1000, null)
144142
webSocket = null
145-
client?.dispatcher?.executorService?.shutdown()
146-
client = null
147143
}
148144

149145
private fun nextMessage(msg: String): JSONObject {

android/app/src/main/java/chat/rocket/reactnative/voip/MediaCallsAnswerRequest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MediaCallsAnswerRequest(
3636
companion object {
3737
private const val TAG = "RocketChat.MediaCallsAnswerRequest"
3838
private val JSON_MEDIA_TYPE = "application/json; charset=utf-8".toMediaType()
39+
private val httpClient = OkHttpClient()
3940

4041
@JvmStatic
4142
fun fetch(
@@ -107,18 +108,19 @@ class MediaCallsAnswerRequest(
107108
.post(requestBody)
108109
.build()
109110

110-
val client = OkHttpClient()
111-
client.newCall(request).enqueue(object : Callback {
111+
httpClient.newCall(request).enqueue(object : Callback {
112112
override fun onFailure(call: Call, e: IOException) {
113113
Log.e(TAG, "MediaCallsAnswerRequest failed for callId=$callId: ${e.message}")
114114
onResult(false)
115115
}
116116

117117
override fun onResponse(call: Call, response: okhttp3.Response) {
118-
val code = response.code
119-
val success = code in 200..299
120-
Log.d(TAG, "MediaCallsAnswerRequest response for callId=$callId: code=$code success=$success")
121-
onResult(success)
118+
response.use {
119+
val code = it.code
120+
val success = code in 200..299
121+
Log.d(TAG, "MediaCallsAnswerRequest response for callId=$callId: code=$code success=$success")
122+
onResult(success)
123+
}
122124
}
123125
})
124126
}

0 commit comments

Comments
 (0)