Settings.swift 4.5 KB

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