From a4ee8427f6b3781e470f2a34d397f626e7885fcb Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 15 Sep 2023 12:04:20 +0200 Subject: [PATCH] make buffered readers and writers --- .../java/com/posthog/internal/PostHogApi.kt | 31 +++++++++++++------ .../java/com/posthog/internal/PostHogQueue.kt | 3 ++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApi.kt b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApi.kt index 8bea5a79..eaa6fe8e 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApi.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApi.kt @@ -7,7 +7,9 @@ import com.posthog.PostHogEvent import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request -import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.RequestBody +import okio.BufferedSink +import java.io.OutputStream import java.util.Date internal class PostHogApi(private val config: PostHogConfig) { @@ -25,7 +27,6 @@ internal class PostHogApi(private val config: PostHogConfig) { fun batch(events: List) { val batch = PostHogBatchEvent(config.apiKey, events) - val json = gson.toJson(batch, gsonBatchBodyType) // """ // { // "api_key": "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI", @@ -41,7 +42,9 @@ internal class PostHogApi(private val config: PostHogConfig) { // "timestamp": "2023-09-13T12:05:30.326Z" // } // """.trimIndent() - val request = makeRequest(json, "${config.host}/batch") + val request = makeRequest("${config.host}/batch") { + gson.toJson(batch, gsonBatchBodyType, it.bufferedWriter()) + } client.newCall(request).execute().use { if (!it.isSuccessful) throw PostHogApiError(it.code, it.message, body = it.body) @@ -53,13 +56,19 @@ internal class PostHogApi(private val config: PostHogConfig) { } } - private fun makeRequest(json: String, url: String): Request { - val body = json.toRequestBody(mediaType) + private fun makeRequest(url: String, serializer: (outputStream: OutputStream) -> Unit): Request { + val requestBody = object : RequestBody() { + override fun contentType() = mediaType + + override fun writeTo(sink: BufferedSink) { + serializer(sink.outputStream()) + } + } return Request.Builder() .url(url) .header("User-Agent", config.userAgent) - .post(body) + .post(requestBody) .build() } @@ -67,21 +76,23 @@ internal class PostHogApi(private val config: PostHogConfig) { val map = mutableMapOf() map.putAll(properties) map["api_key"] = config.apiKey - - val json = gson.toJson(map, gsonDecideBodyType) // """ // { // "distinct_id": "1fc77c1a-5f98-43b3-bb77-7a2dd15fd13a", // "api_key": "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI" // } // """.trimIndent() - val request = makeRequest(json, "${config.host}/decide/?v=3") + val request = makeRequest("${config.host}/decide/?v=3") { + val writer = it.bufferedWriter() + gson.toJson(map, gsonDecideBodyType, writer) + writer.flush() + } client.newCall(request).execute().use { if (!it.isSuccessful) throw PostHogApiError(it.code, it.message, body = it.body) it.body?.let { body -> - return gson.fromJson(body.string(), gsonDecideBodyType) + return gson.fromJson(body.charStream().buffered(), gsonDecideBodyType) } return null } diff --git a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt index a569927a..f7f7901e 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt @@ -99,6 +99,9 @@ internal class PostHogQueue(private val config: PostHogConfig, private val stora retryCount = 0 } catch (e: Throwable) { config.logger?.log("Flushing failed: $e") + + // TODO: when do we actually drop those events? maybe they are broken for good + // and the SDK will be stuck at them retry = true retryCount++ }