-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rodrigo Gomez Palacio
committed
Aug 6, 2024
1 parent
43c7d61
commit df351c8
Showing
11 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...lSDK/onesignal/core/src/main/java/com/onesignal/common/kafka/CreateOffsetsIamCondition.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,16 @@ | ||
package com.onesignal.common.kafka | ||
|
||
/** | ||
* Used for read your write consistency when fetching In-App Messages. Corresponds to conditions | ||
* for fetching after upserting models via HTTP CREATE. | ||
*/ | ||
class CreateOffsetsIamCondition: ICondition { | ||
override fun isMet(offsets: Map<String, Long?>): Boolean { | ||
// one or the other is available | ||
return offsets["userCreateOffset"] != null || offsets["subscriptionCreateOffset"] != null | ||
} | ||
|
||
override fun getNewestOffset(offsets: Map<String, Long?>): Long? { | ||
return listOfNotNull(offsets["userCreateOffset"], offsets["subscriptionCreateOffset"]).maxOrNull() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/kafka/ICondition.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,6 @@ | ||
package com.onesignal.common.kafka | ||
|
||
interface ICondition { | ||
fun isMet(offsets: Map<String, Long?>): Boolean | ||
fun getNewestOffset(offsets: Map<String, Long?>): Long? | ||
} |
51 changes: 51 additions & 0 deletions
51
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/kafka/KafkaOffsetManager.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,51 @@ | ||
package com.onesignal.common | ||
|
||
import com.onesignal.common.kafka.ICondition | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
/** | ||
* Manages offsets that function as read-your-write tokens for more accurate segment membership | ||
* calculation. | ||
*/ | ||
class KafkaOffsetManager { | ||
|
||
private val mutex = Mutex() | ||
private val offsets: MutableMap<String, Long?> = mutableMapOf() | ||
private var offsetDeferred = CompletableDeferred<Long?>() | ||
|
||
private val conditions: MutableList<Pair<ICondition, CompletableDeferred<Long?>>> = | ||
mutableListOf() | ||
|
||
/** | ||
* Set method to update the offset based on the key. | ||
*/ | ||
suspend fun setOffset(key: String, value: Long?) { | ||
mutex.withLock { | ||
offsets[key] = value | ||
checkConditionsAndComplete() | ||
} | ||
} | ||
|
||
/** | ||
* Register a condition with its corresponding deferred action. Returns a deferred condition. | ||
*/ | ||
fun registerCondition(condition: ICondition): CompletableDeferred<Long?> { | ||
val deferred = CompletableDeferred<Long?>() | ||
conditions.add(condition to deferred) | ||
checkConditionsAndComplete() | ||
return deferred | ||
} | ||
|
||
private fun checkConditionsAndComplete() { | ||
for ((condition, deferred) in conditions) { | ||
if (condition.isMet(offsets)) { | ||
val newestOffset = condition.getNewestOffset(offsets) | ||
if (!deferred.isCompleted) { | ||
deferred.complete(newestOffset) | ||
} | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...lSDK/onesignal/core/src/main/java/com/onesignal/common/kafka/UpdateOffsetsIamCondition.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,16 @@ | ||
package com.onesignal.common.kafka | ||
|
||
/** | ||
* Used for read your write consistency when fetching In-App Messages. Corresponds to conditions | ||
* for fetching after updating models via HTTP PATCH. | ||
*/ | ||
class UpdateOffsetsIamCondition : ICondition { | ||
override fun isMet(offsets: Map<String, Long?>): Boolean { | ||
// both are available | ||
return offsets["userUpdateOffset"] != null && offsets["subscriptionUpdateOffset"] != null | ||
} | ||
|
||
override fun getNewestOffset(offsets: Map<String, Long?>): Long? { | ||
return listOfNotNull(offsets["userUpdateOffset"], offsets["subscriptionUpdateOffset"]).maxOrNull() | ||
} | ||
} |
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
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
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
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
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
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
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