-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
const messages = { | ||
BAD_REQUEST: "Bad Request", | ||
UNAUTHORIZED: "Unauthorized", | ||
FORBIDDEN: "Forbidden", | ||
INTERNAL_SERVER_ERROR: "Internal Server Error", | ||
BAD_REQUEST: | ||
"An unexpected issue occurred. If the error persists, please contact [email protected].", | ||
UNAUTHORIZED: | ||
"Unauthorized. Refresh the page, or log out and log in again. If the error persists, contact [email protected].", | ||
FORBIDDEN: | ||
"Access forbidden. Refresh the page, or log out and log in again. If the error persists, contact [email protected].", | ||
INTERNAL_SERVER_ERROR: | ||
"An unexpected issue occurred. Try refreshing the page. If the error persists, please contact [email protected].", | ||
} as const; | ||
|
||
const getStatusCodeErrorMessage = (statusCode: number) => { | ||
|
@@ -24,7 +28,7 @@ const getStatusCodeErrorMessage = (statusCode: number) => { | |
return messages.BAD_REQUEST; | ||
} | ||
|
||
return `Internal error occurred. You may try refreshing the page. If the error persists, please contact [email protected] (status code: ${statusCode})`; | ||
return `An unexpected issue occurred (status ${statusCode}). You may try refreshing the page. If the error persists, please contact [email protected].`; | ||
}; | ||
|
||
export default getStatusCodeErrorMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
services/feed-requests/src/host-rate-limiter/host-rate-limiter.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CacheStorageModule } from '../cache-storage/cache-storage.module'; | ||
import { HostRateLimiterService } from './host-rate-limiter.service'; | ||
|
||
@Module({ | ||
providers: [HostRateLimiterService], | ||
imports: [CacheStorageModule], | ||
exports: [HostRateLimiterService], | ||
}) | ||
export class HostRateLimiterModule {} |
67 changes: 67 additions & 0 deletions
67
services/feed-requests/src/host-rate-limiter/host-rate-limiter.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { URL } from 'node:url'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { CacheStorageService } from '../cache-storage/cache-storage.service'; | ||
|
||
interface RateLimitData { | ||
requestLimit: number; | ||
intervalSec: number; | ||
} | ||
|
||
const RATE_LIMITED_HOSTS = new Map<string, RateLimitData>([ | ||
[ | ||
'data.sec.gov', | ||
{ | ||
requestLimit: 10, | ||
intervalSec: 2, | ||
}, | ||
], | ||
]); | ||
|
||
@Injectable() | ||
export class HostRateLimiterService { | ||
constructor(private readonly cacheStorageService: CacheStorageService) {} | ||
|
||
async incrementUrlCount(url: string): Promise<{ isRateLimited: boolean }> { | ||
const applicableLimit = this.getLimitForUrl(url); | ||
|
||
if (!applicableLimit) { | ||
return { | ||
isRateLimited: false, | ||
}; | ||
} | ||
|
||
const { | ||
host, | ||
data: { intervalSec, requestLimit }, | ||
} = applicableLimit; | ||
|
||
const cacheKey = this.getCacheKeyForHost(host); | ||
|
||
const newVal = await this.cacheStorageService.increment(cacheKey, { | ||
expire: { | ||
seconds: intervalSec, | ||
mode: 'NX', | ||
}, | ||
}); | ||
|
||
return { | ||
isRateLimited: newVal - 1 >= requestLimit, | ||
}; | ||
} | ||
|
||
getLimitForUrl(url: string): null | { host: string; data: RateLimitData } { | ||
const host = new URL(url).host; | ||
|
||
const found = RATE_LIMITED_HOSTS.get(host); | ||
|
||
if (!found) { | ||
return null; | ||
} | ||
|
||
return { host, data: found }; | ||
} | ||
|
||
private getCacheKeyForHost(urlHost: string) { | ||
return `host-rate-limiter:${urlHost}`; | ||
} | ||
} |