Skip to content

Commit

Permalink
feat: use new header to set polling interval (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernardGatt authored Feb 12, 2024
1 parent 391d79b commit 966c9cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import java.util.regex.PatternSyntaxException
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.Cache
import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.Retrofit
Expand Down Expand Up @@ -132,6 +133,9 @@ class Queue : GistListener {
)
latestMessagesResponse.body()?.let { handleMessages(it) }
}

// Check if the polling interval changed and update timer.
updatePollingInterval(latestMessagesResponse.headers())
} catch (e: Exception) {
Log.e(
GIST_TAG,
Expand All @@ -141,6 +145,20 @@ class Queue : GistListener {
}
}

private fun updatePollingInterval(headers: Headers) {
headers["X-Gist-Queue-Polling-Interval"]?.toIntOrNull()?.let { pollingIntervalSeconds ->
if (pollingIntervalSeconds > 0) {
val newPollingIntervalMilliseconds = (pollingIntervalSeconds * 1000).toLong()
if (newPollingIntervalMilliseconds != GistSdk.pollInterval) {
GistSdk.pollInterval = newPollingIntervalMilliseconds
// Queue check fetches messages again and could result in infinite loop.
GistSdk.observeMessagesForUser(true)
Log.i(GIST_TAG, "Polling interval changed to: $pollingIntervalSeconds seconds")
}
}
}
}

private fun handleMessages(messages: List<Message>) {
// Sorting messages by priority and placing nulls last
val sortedMessages = messages.sortedWith(compareBy(nullsLast()) { it.priority })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const val GIST_TAG: String = "[CIO]"
object GistSdk : Application.ActivityLifecycleCallbacks {
private const val SHARED_PREFERENCES_NAME = "gist-sdk"
private const val SHARED_PREFERENCES_USER_TOKEN_KEY = "userToken"
private const val POLL_INTERVAL = 10_000L

private val sharedPreferences by lazy {
application.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
}

internal var pollInterval = 600_000L
lateinit var siteId: String
lateinit var dataCenter: String
internal lateinit var gistEnvironment: GistEnvironment
Expand Down Expand Up @@ -187,16 +187,18 @@ object GistSdk : Application.ActivityLifecycleCallbacks {

// Gist Message Observer

private fun observeMessagesForUser() {
internal fun observeMessagesForUser(skipQueueCheck: Boolean = false) {
// Clean up any previous observers
observeUserMessagesJob?.cancel()

Log.i(GIST_TAG, "Messages timer started")
gistQueue.fetchUserMessages()
if (!skipQueueCheck) {
gistQueue.fetchUserMessages()
}
observeUserMessagesJob = GlobalScope.launch {
try {
// Poll for user messages
val ticker = ticker(POLL_INTERVAL, context = this.coroutineContext)
val ticker = ticker(pollInterval, context = this.coroutineContext)
for (tick in ticker) {
gistQueue.fetchUserMessages()
}
Expand Down

0 comments on commit 966c9cb

Please sign in to comment.