Skip to content

Commit

Permalink
feat: check the buffer every second
Browse files Browse the repository at this point in the history
To reduce overhead of constant loop, the `InMemoryBuffer` will check `buffer` list every second.
  • Loading branch information
wzieba committed Nov 7, 2023
1 parent 1905ffc commit 8a518ce
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.parsely.parselyandroid

import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -15,13 +18,14 @@ internal class InMemoryBuffer(

init {
coroutineScope.launch {
while (true) {
while (isActive) {
mutex.withLock {
if (buffer.isNotEmpty()) {
localStorageRepository.insertEvents(buffer)
buffer.clear()
}
}
delay(1.seconds)
}
}
}
Expand Down

0 comments on commit 8a518ce

Please sign in to comment.