Skip to content

Commit

Permalink
chore: add feature flag for using replica to look up redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
halfwhole committed Mar 21, 2024
1 parent 842cdc1 commit f30ecc1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ After these have been set up, set the environment variables according to the tab
|API_LINK_RANDOM_STR_LENGTH|No|String length of randomly generated shortUrl in API created links. Defaults to 8|
|FF_EXTERNAL_API|No|Boolean, feature flag for enabling the external and admin API. Defaults to false|
|ADMIN_API_EMAILS|No|Emails with admin API access, separated by commas without spaces. Defaults to none.|
|FF_USE_REPLICA_FOR_REDIRECTS|No|Boolean, feature flag for using the replica database to look up redirects to long URLs. Defaults to false|

#### Serverless functions for link migration

Expand Down
2 changes: 2 additions & 0 deletions src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ export const databaseUri = process.env.DB_URI as string
export const masterDatabaseCredentials = parseDbUri(databaseUri)
export const replicaUri = process.env.REPLICA_URI as string
export const replicaDatabaseCredentials = parseDbUri(replicaUri)
export const ffUseReplicaForRedirects =
process.env.FF_USE_REPLICA_FOR_REDIRECTS === 'true'
export const redisOtpUri = process.env.REDIS_OTP_URI as string
export const redisSessionUri = process.env.REDIS_SESSION_URI as string
export const redisRedirectUri = process.env.REDIS_REDIRECT_URI as string
Expand Down
7 changes: 5 additions & 2 deletions src/server/repositories/UrlRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Url, UrlType } from '../models/url'
import { UrlClicks } from '../models/statistics/clicks'
import { NotFoundError } from '../util/error'
import { redirectClient } from '../redis'
import { logger, redirectExpiry } from '../config'
import { ffUseReplicaForRedirects, logger, redirectExpiry } from '../config'
import { sequelize } from '../util/sequelize'
import { DependencyIds } from '../constants'
import { FileVisibility, S3Interface } from '../services/aws'
Expand Down Expand Up @@ -484,7 +484,10 @@ export class UrlRepository implements UrlRepositoryInterface {
*/
private getLongUrlFromDatabase: (shortUrl: string) => Promise<string> =
async (shortUrl) => {
const url = await Url.scope('useReplica').findOne({
const url = await (ffUseReplicaForRedirects
? Url.scope('useReplica')
: Url
).findOne({
where: { shortUrl, state: StorableUrlState.Active },
})
if (!url) {
Expand Down

0 comments on commit f30ecc1

Please sign in to comment.