fix: implement in-memory token caching to avoid redundant prefs decryptions (#320)
* fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. * fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. * fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. - Fixed GitGuardian warning by removing the "my_secret_token" from the test. * fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. - Fixed GitGuardian warning by removing the "my_secret_token" from the test. - Fixed merge conflicts and ensured code still tests well. * fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. - Fixed GitGuardian warning by removing the "my_secret_token" from the test. * fix: implement in-memory token caching to avoid redundant prefs decryptions Resolves #277 Previously, the `AuthManager.getToken()` method decrypted the entire `EncryptedSharedPreferences` file on every call. Because this method is used in the OkHttp interceptor on every HTTP/WS request, and frequently during navigation, it introduced 10-30ms of redundant work per call. This compounded to noticeable UI lag (e.g., during chat session switches). This commit implements thread-safe in-memory caching for the auth token inside `AuthManager`. - Added a `@Volatile` `cachedToken` and `tokenInitialized` flag. - Modified `getToken()` to return the cached value after initial evaluation, using double-checked locking (`synchronized(this)`) to avoid race conditions. - Updated `setToken()` and `setSelectedProfileId()` to correctly invalidate or update the cache. - Added comprehensive unit tests in `AuthManagerTest.kt` to verify caching behavior and proper invalidation upon profile switches. - Fixed GitGuardian warning by removing the "my_secret_token" from the test. --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
eb71ae3a46
commit
42fcf2a14b
19 files changed
+157
-299
No files matched your search
@@ -24,8 +24,6 @@ object ApiClient {
|
||||
@Volatile
|
||||
private var service: HermesApiService? = null
|
||||
|
||||
private val connectionPool = okhttp3.ConnectionPool()
|
||||
|
||||
/** The current [HermesApiService] instance. Lazily created on first access. */
|
||||
val hermesApi: HermesApiService
|
||||
get() {
|
||||
@@ -124,7 +122,6 @@ object ApiClient {
|
||||
val okHttp =
|
||||
OkHttpClient
|
||||
.Builder()
|
||||
.connectionPool(connectionPool)
|
||||
.addInterceptor(authInterceptor)
|
||||
.addInterceptor(logging)
|
||||
.certificatePinner(certificatePinner)
|
||||
|
||||
Reference in new issue
Block a user