nomagick commited on
Commit
7c57123
·
unverified ·
1 Parent(s): 8eee951

feat: allow custom rate limit per uid

Browse files
backend/functions/src/cloud-functions/crawler.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
  } from 'civkit';
7
  import { singleton } from 'tsyringe';
8
  import { AsyncContext, CloudHTTPv2, Ctx, FirebaseStorageBucketControl, InsufficientBalanceError, Logger, OutputServerEventStream, RPCReflect } from '../shared';
9
- import { RateLimitControl } from '../shared/services/rate-limit';
10
  import _ from 'lodash';
11
  import { PageSnapshot, PuppeteerControl, ScrappingOptions } from '../services/puppeteer';
12
  import { Request, Response } from 'express';
@@ -535,23 +535,26 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
535
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
536
  }
537
 
538
- const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['CRAWL'],
539
- [
540
- // 200 requests per minute
541
- new Date(Date.now() - 60 * 1000), 200
542
- ]
 
 
 
543
  );
544
 
545
  rpcReflect.finally(() => {
546
  if (chargeAmount) {
547
- auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
548
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
549
  });
550
  apiRoll.chargeAmount = chargeAmount;
551
  }
552
  });
553
  } else if (ctx.req.ip) {
554
- const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['CRAWL'],
555
  [
556
  // 20 requests per minute
557
  new Date(Date.now() - 60 * 1000), 20
 
6
  } from 'civkit';
7
  import { singleton } from 'tsyringe';
8
  import { AsyncContext, CloudHTTPv2, Ctx, FirebaseStorageBucketControl, InsufficientBalanceError, Logger, OutputServerEventStream, RPCReflect } from '../shared';
9
+ import { RateLimitControl, RateLimitDesc } from '../shared/services/rate-limit';
10
  import _ from 'lodash';
11
  import { PageSnapshot, PuppeteerControl, ScrappingOptions } from '../services/puppeteer';
12
  import { Request, Response } from 'express';
 
535
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
536
  }
537
 
538
+ const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [RateLimitDesc.from({
539
+ occurrence: 200,
540
+ periodSeconds: 60
541
+ })];
542
+
543
+ const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
544
+ rpcReflect, uid, [rpcReflect.name.toUpperCase()],
545
+ ...rateLimitPolicy
546
  );
547
 
548
  rpcReflect.finally(() => {
549
  if (chargeAmount) {
550
+ auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
551
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
552
  });
553
  apiRoll.chargeAmount = chargeAmount;
554
  }
555
  });
556
  } else if (ctx.req.ip) {
557
+ const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, [rpcReflect.name.toUpperCase()],
558
  [
559
  // 20 requests per minute
560
  new Date(Date.now() - 60 * 1000), 20
backend/functions/src/cloud-functions/searcher.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
  } from 'civkit';
7
  import { singleton } from 'tsyringe';
8
  import { AsyncContext, CloudHTTPv2, Ctx, InsufficientBalanceError, Logger, OutputServerEventStream, RPCReflect } from '../shared';
9
- import { RateLimitControl } from '../shared/services/rate-limit';
10
  import _ from 'lodash';
11
  import { ScrappingOptions } from '../services/puppeteer';
12
  import { Request, Response } from 'express';
@@ -168,16 +168,19 @@ export class SearcherHost extends RPCHost {
168
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
169
  }
170
 
171
- const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['SEARCH'],
172
- [
173
- // 40 requests per minute
174
- new Date(Date.now() - 60 * 1000), 40
175
- ]
 
 
 
176
  );
177
 
178
  rpcReflect.finally(() => {
179
  if (chargeAmount) {
180
- auth.reportUsage(chargeAmount, 'reader-search').catch((err) => {
181
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
182
  });
183
  apiRoll.chargeAmount = chargeAmount;
@@ -185,7 +188,7 @@ export class SearcherHost extends RPCHost {
185
  });
186
  } else if (ctx.req.ip) {
187
  this.threadLocal.set('ip', ctx.req.ip);
188
- const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['SEARCH'],
189
  [
190
  // 5 requests per minute
191
  new Date(Date.now() - 60 * 1000), 5
 
6
  } from 'civkit';
7
  import { singleton } from 'tsyringe';
8
  import { AsyncContext, CloudHTTPv2, Ctx, InsufficientBalanceError, Logger, OutputServerEventStream, RPCReflect } from '../shared';
9
+ import { RateLimitControl, RateLimitDesc } from '../shared/services/rate-limit';
10
  import _ from 'lodash';
11
  import { ScrappingOptions } from '../services/puppeteer';
12
  import { Request, Response } from 'express';
 
168
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
169
  }
170
 
171
+ const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [RateLimitDesc.from({
172
+ occurrence: 40,
173
+ periodSeconds: 60
174
+ })];
175
+
176
+ const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
177
+ rpcReflect, uid, [rpcReflect.name.toUpperCase()],
178
+ ...rateLimitPolicy
179
  );
180
 
181
  rpcReflect.finally(() => {
182
  if (chargeAmount) {
183
+ auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
184
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
185
  });
186
  apiRoll.chargeAmount = chargeAmount;
 
188
  });
189
  } else if (ctx.req.ip) {
190
  this.threadLocal.set('ip', ctx.req.ip);
191
+ const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, [rpcReflect.name.toUpperCase()],
192
  [
193
  // 5 requests per minute
194
  new Date(Date.now() - 60 * 1000), 5
thinapps-shared CHANGED
@@ -1 +1 @@
1
- Subproject commit 1b28100c71b3c7e37669fa98756affbac3095ced
 
1
+ Subproject commit 17ee85dd08becd8a86acf993ae7952d8f911b05e