RemoteSettings.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. /// Extends ApplicationInfoProtocol to string-format a combined appDisplayVersion and
  17. /// appBuildVersion
  18. extension ApplicationInfoProtocol {
  19. var synthesizedVersion: String { return "\(appDisplayVersion) (\(appBuildVersion))" }
  20. }
  21. class RemoteSettings: SettingsProvider {
  22. private static let cacheDurationSecondsDefault: TimeInterval = 60 * 60
  23. private static let flagSessionsEnabled = "sessions_enabled"
  24. private static let flagSamplingRate = "sampling_rate"
  25. private static let flagSessionTimeout = "session_timeout_seconds"
  26. private static let flagCacheDuration = "cache_duration"
  27. private static let flagSessionsCache = "app_quality"
  28. private let appInfo: ApplicationInfoProtocol
  29. private let downloader: SettingsDownloadClient
  30. private var cache: SettingsCacheClient
  31. private var cacheDurationSeconds: TimeInterval {
  32. guard let duration = cache.cacheContent[RemoteSettings.flagCacheDuration] as? Double else {
  33. return RemoteSettings.cacheDurationSecondsDefault
  34. }
  35. return duration
  36. }
  37. private var sessionsCache: [String: Any] {
  38. return cache.cacheContent[RemoteSettings.flagSessionsCache] as? [String: Any] ?? [:]
  39. }
  40. init(appInfo: ApplicationInfoProtocol,
  41. downloader: SettingsDownloadClient,
  42. cache: SettingsCacheClient = SettingsCache()) {
  43. self.appInfo = appInfo
  44. self.cache = cache
  45. self.downloader = downloader
  46. }
  47. private func fetchAndCacheSettings(currentTime: Date) {
  48. // Only fetch if cache is expired, otherwise do nothing
  49. guard isCacheExpired(time: currentTime) else {
  50. Logger.logDebug("[Settings] Cache is not expired, no fetch will be made.")
  51. return
  52. }
  53. downloader.fetch { result in
  54. switch result {
  55. case let .success(dictionary):
  56. // Saves all newly fetched Settings to cache
  57. self.cache.cacheContent = dictionary
  58. // Saves a "cache-key" which carries TTL metadata about current cache
  59. self.cache.cacheKey = CacheKey(
  60. createdAt: currentTime,
  61. googleAppID: self.appInfo.appID,
  62. appVersion: self.appInfo.synthesizedVersion
  63. )
  64. case let .failure(error):
  65. Logger.logError("[Settings] Fetching newest settings failed with error: \(error)")
  66. }
  67. }
  68. }
  69. }
  70. typealias RemoteSettingsConfigurations = RemoteSettings
  71. extension RemoteSettingsConfigurations {
  72. var sessionsEnabled: Bool? {
  73. return sessionsCache[RemoteSettings.flagSessionsEnabled] as? Bool
  74. }
  75. var samplingRate: Double? {
  76. return sessionsCache[RemoteSettings.flagSamplingRate] as? Double
  77. }
  78. var sessionTimeout: TimeInterval? {
  79. return sessionsCache[RemoteSettings.flagSessionTimeout] as? Double
  80. }
  81. }
  82. typealias RemoteSettingsProvider = RemoteSettings
  83. extension RemoteSettingsConfigurations {
  84. func updateSettings(currentTime: Date) {
  85. fetchAndCacheSettings(currentTime: currentTime)
  86. }
  87. func updateSettings() {
  88. updateSettings(currentTime: Date())
  89. }
  90. func isSettingsStale() -> Bool {
  91. return isCacheExpired(time: Date())
  92. }
  93. private func isCacheExpired(time: Date) -> Bool {
  94. guard !cache.cacheContent.isEmpty else {
  95. cache.removeCache()
  96. return true
  97. }
  98. guard let cacheKey = cache.cacheKey else {
  99. Logger.logError("[Settings] Could not load settings cache key")
  100. cache.removeCache()
  101. return true
  102. }
  103. guard cacheKey.googleAppID == appInfo.appID else {
  104. Logger
  105. .logDebug("[Settings] Cache expired because Google App ID changed")
  106. cache.removeCache()
  107. return true
  108. }
  109. if time.timeIntervalSince(cacheKey.createdAt) > cacheDurationSeconds {
  110. Logger.logDebug("[Settings] Cache TTL expired")
  111. return true
  112. }
  113. if appInfo.synthesizedVersion != cacheKey.appVersion {
  114. Logger.logDebug("[Settings] Cache expired because app version changed")
  115. return true
  116. }
  117. return false
  118. }
  119. }