Refresh snode pool at least once a day

Changes from cfb7ff529
This commit is contained in:
Niels Andriesse 2020-12-07 09:02:48 +11:00 committed by Anton Chekulaev
parent 3cb7d2169d
commit 48adb63fb7
5 changed files with 33 additions and 2 deletions

View File

@ -29,7 +29,7 @@ class ClosedGroupPoller private constructor(private val context: Context, privat
// region Settings // region Settings
companion object { companion object {
private val pollInterval: Long = 2 * 1000 private val pollInterval: Long = 4 * 1000
public lateinit var shared: ClosedGroupPoller public lateinit var shared: ClosedGroupPoller

View File

@ -9,6 +9,8 @@ import org.thoughtcrime.securesms.loki.utilities.*
import org.session.libsignal.service.loki.api.Snode import org.session.libsignal.service.loki.api.Snode
import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol
import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink
import org.thoughtcrime.securesms.util.TextSecurePreferences
import java.util.*
class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol { class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol {
@ -375,6 +377,16 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
return database.delete(openGroupProfilePictureTable, "$publicChatID = ?", arrayOf(index)) > 0 return database.delete(openGroupProfilePictureTable, "$publicChatID = ?", arrayOf(index)) > 0
} }
override fun getLastSnodePoolRefreshDate(): Date? {
val time = TextSecurePreferences.getLastSnodePoolRefreshDate(context)
if (time <= 0) { return null }
return Date(time)
}
override fun setLastSnodePoolRefreshDate(date: Date) {
TextSecurePreferences.setLastSnodePoolRefreshDate(context, date)
}
// region Deprecated // region Deprecated
override fun getDeviceLinks(publicKey: String): Set<DeviceLink> { override fun getDeviceLinks(publicKey: String): Set<DeviceLink> {
return setOf() return setOf()

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -1237,6 +1238,14 @@ public class TextSecurePreferences {
public static void setHasSeenLightThemeIntroSheet(Context context) { public static void setHasSeenLightThemeIntroSheet(Context context) {
setBooleanPreference(context, "has_seen_light_theme_intro_sheet", true); setBooleanPreference(context, "has_seen_light_theme_intro_sheet", true);
} }
public static long getLastSnodePoolRefreshDate(Context context) {
return getLongPreference(context, "last_snode_pool_refresh_date", 0);
}
public static void setLastSnodePoolRefreshDate(Context context, Date date) {
setLongPreference(context, "last_snode_pool_refresh_date", date.getTime());
}
// endregion // endregion
// region Backup related // region Backup related

View File

@ -11,6 +11,7 @@ import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol
import org.session.libsignal.service.loki.utilities.getRandomElement import org.session.libsignal.service.loki.utilities.getRandomElement
import org.session.libsignal.service.loki.utilities.prettifiedDescription import org.session.libsignal.service.loki.utilities.prettifiedDescription
import java.security.SecureRandom import java.security.SecureRandom
import java.util.*
class SwarmAPI private constructor(private val database: LokiAPIDatabaseProtocol) { class SwarmAPI private constructor(private val database: LokiAPIDatabaseProtocol) {
internal var snodeFailureCount: MutableMap<Snode, Int> = mutableMapOf() internal var snodeFailureCount: MutableMap<Snode, Int> = mutableMapOf()
@ -46,7 +47,12 @@ class SwarmAPI private constructor(private val database: LokiAPIDatabaseProtocol
// region Swarm API // region Swarm API
internal fun getRandomSnode(): Promise<Snode, Exception> { internal fun getRandomSnode(): Promise<Snode, Exception> {
val snodePool = this.snodePool val snodePool = this.snodePool
if (snodePool.count() < minimumSnodePoolCount) { val lastRefreshDate = database.getLastSnodePoolRefreshDate()
val now = Date()
val needsRefresh = (snodePool.count() < minimumSnodePoolCount) || lastRefreshDate == null || (now.time - lastRefreshDate.time) > 24 * 60 * 60 * 1000
if (needsRefresh) {
database.setLastSnodePoolRefreshDate(now)
val target = seedNodePool.random() val target = seedNodePool.random()
val url = "$target/json_rpc" val url = "$target/json_rpc"
Log.d("Loki", "Populating snode pool using: $target.") Log.d("Loki", "Populating snode pool using: $target.")

View File

@ -2,6 +2,7 @@ package org.session.libsignal.service.loki.database
import org.session.libsignal.service.loki.api.Snode import org.session.libsignal.service.loki.api.Snode
import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink
import java.util.*
interface LokiAPIDatabaseProtocol { interface LokiAPIDatabaseProtocol {
@ -31,6 +32,9 @@ interface LokiAPIDatabaseProtocol {
fun setOpenGroupPublicKey(server: String, newValue: String) fun setOpenGroupPublicKey(server: String, newValue: String)
fun setOpenGroupProfilePictureURL(group: Long, server: String, newValue: String) fun setOpenGroupProfilePictureURL(group: Long, server: String, newValue: String)
fun getOpenGroupProfilePictureURL(group: Long, server: String): String? fun getOpenGroupProfilePictureURL(group: Long, server: String): String?
fun getLastSnodePoolRefreshDate(): Date?
fun setLastSnodePoolRefreshDate(newValue: Date)
// region Deprecated // region Deprecated
fun getDeviceLinks(publicKey: String): Set<DeviceLink> fun getDeviceLinks(publicKey: String): Set<DeviceLink>