NM-256: fix DB performance degradation with large user base and Entra IDP

- Replace full table scan in FetchRecord with indexed single-key lookup
      (SELECT WHERE key = ?) for PostgreSQL, SQLite, and RQLite backends
    - Add in-memory user cache (gated behind CACHING_ENABLED) to eliminate
      DB round-trips on the auth hot path (GetUser called per API request)
    - Configure PostgreSQL connection pool limits (max open/idle conns,
      conn lifetime) to prevent connection churn under load
    - Add periodic cleanup of expired SSO state entries to prevent
      unbounded table growth
    - Route GitHub OAuth user rename through standard logic functions
      to keep user cache consistent
This commit is contained in:
abhishek9686
2026-02-20 14:47:50 +04:00
parent a3fde5ab2f
commit ad3af6ee7c
9 changed files with 215 additions and 88 deletions
+13
View File
@@ -20,6 +20,7 @@ var SQLITE_FUNCTIONS = map[string]interface{}{
DELETE: sqliteDeleteRecord,
DELETE_ALL: sqliteDeleteAllRecords,
FETCH_ALL: sqliteFetchRecords,
FETCH_ONE: sqliteFetchRecord,
CLOSE_DB: sqliteCloseDB,
isConnected: sqliteConnected,
}
@@ -103,6 +104,18 @@ func sqliteDeleteAllRecords(tableName string) error {
return nil
}
func sqliteFetchRecord(tableName string, key string) (string, error) {
var value string
err := SqliteDB.QueryRow("SELECT value FROM "+tableName+" WHERE key = ?", key).Scan(&value)
if err != nil {
if err == sql.ErrNoRows {
return "", errors.New(NO_RECORD)
}
return "", err
}
return value, nil
}
func sqliteFetchRecords(tableName string) (map[string]string, error) {
row, err := SqliteDB.Query("SELECT * FROM " + tableName + " ORDER BY key")
if err != nil {