nomagick commited on
Commit
b2ba2f9
·
unverified ·
1 Parent(s): f9223d7

fix: beware cf-browser-rendering capacity

Browse files
src/services/cf-browser-rendering.ts CHANGED
@@ -3,6 +3,8 @@ import { AsyncService } from 'civkit/async-service';
3
  import { SecretExposer } from '../shared/services/secrets';
4
  import { GlobalLogger } from './logger';
5
  import { CloudFlareHTTP } from '../shared/3rd-party/cloud-flare';
 
 
6
 
7
  @singleton()
8
  export class CFBrowserRendering extends AsyncService {
@@ -27,9 +29,22 @@ export class CFBrowserRendering extends AsyncService {
27
  }
28
 
29
  async fetchContent(url: string) {
30
- const r = await this.client.fetchBrowserRenderedHTML({ url });
31
-
32
- return r.parsed.result;
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
34
 
35
  }
 
3
  import { SecretExposer } from '../shared/services/secrets';
4
  import { GlobalLogger } from './logger';
5
  import { CloudFlareHTTP } from '../shared/3rd-party/cloud-flare';
6
+ import { HTTPServiceError } from 'civkit/http';
7
+ import { ServiceNodeResourceDrainError } from './errors';
8
 
9
  @singleton()
10
  export class CFBrowserRendering extends AsyncService {
 
29
  }
30
 
31
  async fetchContent(url: string) {
32
+ try {
33
+ const r = await this.client.fetchBrowserRenderedHTML({ url });
34
+
35
+ return r.parsed.result;
36
+ } catch (err) {
37
+ if (err instanceof HTTPServiceError) {
38
+ if (err.status === 429) {
39
+ // Rate limit exceeded, return empty result
40
+ this.logger.warn('Cloudflare browser rendering rate limit exceeded', { url });
41
+
42
+ throw new ServiceNodeResourceDrainError(`Cloudflare browser rendering (our account) is at capacity, please try again later or switch to another engine.`,);
43
+ }
44
+ }
45
+
46
+ throw err;
47
+ }
48
  }
49
 
50
  }