@@ -21,6 +21,9 @@ import {
2121 METADATA_COLUMNS ,
2222 METADATA_ATTRIBUTES_COLUMNS ,
2323 METADATA_PROPERTIES_COLUMNS ,
24+ DbRateLimitedHostInsert ,
25+ DbRateLimitedHost ,
26+ RATE_LIMITED_HOSTS_COLUMNS ,
2427} from './types' ;
2528import { connectPostgres } from './postgres-tools' ;
2629import { BasePgStore } from './postgres-tools/base-pg-store' ;
@@ -393,7 +396,38 @@ export class PgStore extends BasePgStore {
393396 INSERT INTO jobs (token_id) (SELECT id AS token_id FROM token_inserts)
394397 ON CONFLICT (token_id) WHERE smart_contract_id IS NULL DO
395398 UPDATE SET updated_at = NOW(), status = 'pending'
396- RETURNING *
399+ RETURNING ${ this . sql ( JOBS_COLUMNS ) }
400+ ` ;
401+ }
402+
403+ async insertRateLimitedHost ( args : {
404+ values : DbRateLimitedHostInsert ;
405+ } ) : Promise < DbRateLimitedHost > {
406+ const retryAfter = args . values . retry_after . toString ( ) ;
407+ const results = await this . sql < DbRateLimitedHost [ ] > `
408+ INSERT INTO rate_limited_hosts (hostname, created_at, retry_after)
409+ VALUES (${ args . values . hostname } , DEFAULT, NOW() + INTERVAL '${ this . sql ( retryAfter ) } seconds')
410+ ON CONFLICT ON CONSTRAINT rate_limited_hosts_hostname_unique DO
411+ UPDATE SET retry_after = EXCLUDED.retry_after
412+ RETURNING ${ this . sql ( RATE_LIMITED_HOSTS_COLUMNS ) }
413+ ` ;
414+ return results [ 0 ] ;
415+ }
416+
417+ async getRateLimitedHost ( args : { hostname : string } ) : Promise < DbRateLimitedHost | undefined > {
418+ const results = await this . sql < DbRateLimitedHost [ ] > `
419+ SELECT ${ this . sql ( RATE_LIMITED_HOSTS_COLUMNS ) }
420+ FROM rate_limited_hosts
421+ WHERE hostname = ${ args . hostname }
422+ ` ;
423+ if ( results . count > 0 ) {
424+ return results [ 0 ] ;
425+ }
426+ }
427+
428+ async deleteRateLimitedHost ( args : { hostname : string } ) : Promise < void > {
429+ await this . sql `
430+ DELETE FROM rate_limited_hosts WHERE hostname = ${ args . hostname }
397431 ` ;
398432 }
399433
0 commit comments