-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add scope to the payload before send
- Loading branch information
1 parent
3d48deb
commit 37050f1
Showing
2 changed files
with
95 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Provider/src/main/java/com/spotify/confidence/EventSenderEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.spotify.confidence | ||
|
||
import com.spotify.confidence.client.Clock | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.launch | ||
|
||
internal class EventSenderEngine( | ||
private val eventStorage: EventStorage, | ||
private val clientSecret: String, | ||
private val flushPolicies: List<FlushPolicy> = listOf(), | ||
private val clock: Clock = Clock.CalendarBacked.systemUTC(), | ||
private val dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) { | ||
private val writeReqChannel: Channel<Event> = Channel() | ||
private val sendChannel: Channel<String> = Channel() | ||
private val coroutineScope by lazy { | ||
CoroutineScope(SupervisorJob() + dispatcher) | ||
} | ||
private val exceptionHandler by lazy { | ||
CoroutineExceptionHandler { _, _ -> | ||
// do nothing | ||
} | ||
} | ||
private lateinit var uploader: EventSenderUploader | ||
|
||
init { | ||
coroutineScope.launch { | ||
for (event in writeReqChannel) { | ||
eventStorage.writeEvent(event) | ||
flushPolicies.forEach { it.hit(event) } | ||
val shouldFlush = flushPolicies.any { it.shouldFlush() } | ||
if (shouldFlush) { | ||
flushPolicies.forEach { it.reset() } | ||
sendChannel.send(SEND_SIG) | ||
} | ||
} | ||
} | ||
|
||
// upload might throw exceptions | ||
coroutineScope.launch(exceptionHandler) { | ||
for (flush in sendChannel) { | ||
eventStorage.rollover() | ||
val readyFiles = eventStorage.batchReadyFiles() | ||
for (readyFile in readyFiles) { | ||
val batch = EventBatch( | ||
clientSecret = clientSecret, | ||
events = eventStorage.eventsFor(readyFile), | ||
sendTime = clock.currentTime() | ||
) | ||
val shouldCleanup = uploader.upload(batch) | ||
if (shouldCleanup) { | ||
readyFile.delete() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fun emit(definition: String, payload: Map<String, String>) { | ||
coroutineScope.launch { | ||
val event = Event( | ||
eventDefinition = definition, | ||
eventTime = clock.currentTime(), | ||
payload = payload | ||
) | ||
writeReqChannel.send(event) | ||
} | ||
} | ||
|
||
fun stop() { | ||
coroutineScope.cancel() | ||
} | ||
|
||
companion object { | ||
private const val SEND_SIG = "FLUSH" | ||
} | ||
} |