RemoteSettings.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. internal import FirebaseCoreInternal
  17. /// Extends ApplicationInfoProtocol to string-format a combined appDisplayVersion and
  18. /// appBuildVersion
  19. extension ApplicationInfoProtocol {
  20. var synthesizedVersion: String { return "\(appDisplayVersion) (\(appBuildVersion))" }
  21. }
  22. final class RemoteSettings: SettingsProvider, Sendable {
  23. private static let cacheDurationSecondsDefault: TimeInterval = 60 * 60
  24. private static let flagSessionsEnabled = "sessions_enabled"
  25. private static let flagSamplingRate = "sampling_rate"
  26. private static let flagSessionTimeout = "session_timeout_seconds"
  27. private static let flagCacheDuration = "cache_duration"
  28. private static let flagSessionsCache = "app_quality"
  29. private let appInfo: ApplicationInfoProtocol
  30. private let downloader: SettingsDownloadClient
  31. private let cache: FIRAllocatedUnfairLock<SettingsCacheClient>
  32. private func cacheDuration(_ cache: SettingsCacheClient) -> TimeInterval {
  33. guard let duration = cache.cacheContent[RemoteSettings.flagCacheDuration] as? Double else {
  34. return RemoteSettings.cacheDurationSecondsDefault
  35. }
  36. print("Duration: \(duration)")
  37. return duration
  38. }
  39. private var sessionsCache: [String: Any] {
  40. cache.withLock { cache in
  41. cache.cacheContent[RemoteSettings.flagSessionsCache] as? [String: Any] ?? [:]
  42. }
  43. }
  44. init(appInfo: ApplicationInfoProtocol,
  45. downloader: SettingsDownloadClient,
  46. cache: SettingsCacheClient = SettingsCache()) {
  47. self.appInfo = appInfo
  48. self.cache = FIRAllocatedUnfairLock(initialState: cache)
  49. self.downloader = downloader
  50. }
  51. private func fetchAndCacheSettings(currentTime: Date) {
  52. let shouldFetch = cache.withLock { cache in
  53. // Only fetch if cache is expired, otherwise do nothing
  54. guard isCacheExpired(cache, time: currentTime) else {
  55. Logger.logDebug("[Settings] Cache is not expired, no fetch will be made.")
  56. return false
  57. }
  58. return true
  59. }
  60. if shouldFetch {
  61. downloader.fetch { result in
  62. switch result {
  63. case let .success(dictionary):
  64. self.cache.withLock { cache in
  65. // Saves all newly fetched Settings to cache
  66. cache.cacheContent = dictionary
  67. // Saves a "cache-key" which carries TTL metadata about current cache
  68. cache.cacheKey = CacheKey(
  69. createdAt: currentTime,
  70. googleAppID: self.appInfo.appID,
  71. appVersion: self.appInfo.synthesizedVersion
  72. )
  73. }
  74. case let .failure(error):
  75. Logger.logError("[Settings] Fetching newest settings failed with error: \(error)")
  76. }
  77. }
  78. }
  79. }
  80. }
  81. typealias RemoteSettingsConfigurations = RemoteSettings
  82. extension RemoteSettingsConfigurations {
  83. var sessionsEnabled: Bool? {
  84. return sessionsCache[RemoteSettings.flagSessionsEnabled] as? Bool
  85. }
  86. var samplingRate: Double? {
  87. return sessionsCache[RemoteSettings.flagSamplingRate] as? Double
  88. }
  89. var sessionTimeout: TimeInterval? {
  90. return sessionsCache[RemoteSettings.flagSessionTimeout] as? Double
  91. }
  92. }
  93. typealias RemoteSettingsProvider = RemoteSettings
  94. extension RemoteSettingsConfigurations {
  95. func updateSettings(currentTime: Date) {
  96. fetchAndCacheSettings(currentTime: currentTime)
  97. }
  98. func updateSettings() {
  99. updateSettings(currentTime: Date())
  100. }
  101. func isSettingsStale() -> Bool {
  102. cache.withLock { cache in
  103. isCacheExpired(cache, time: Date())
  104. }
  105. }
  106. private func isCacheExpired(_ cache: SettingsCacheClient, time: Date) -> Bool {
  107. guard !cache.cacheContent.isEmpty else {
  108. cache.removeCache()
  109. return true
  110. }
  111. guard let cacheKey = cache.cacheKey else {
  112. Logger.logError("[Settings] Could not load settings cache key")
  113. cache.removeCache()
  114. return true
  115. }
  116. guard cacheKey.googleAppID == appInfo.appID else {
  117. Logger
  118. .logDebug("[Settings] Cache expired because Google App ID changed")
  119. cache.removeCache()
  120. return true
  121. }
  122. if time.timeIntervalSince(cacheKey.createdAt) > cacheDuration(cache) {
  123. Logger.logDebug("[Settings] Cache TTL expired")
  124. return true
  125. }
  126. if appInfo.synthesizedVersion != cacheKey.appVersion {
  127. Logger.logDebug("[Settings] Cache expired because app version changed")
  128. return true
  129. }
  130. return false
  131. }
  132. }