File tree Expand file tree Collapse file tree 4 files changed +51
-8
lines changed Expand file tree Collapse file tree 4 files changed +51
-8
lines changed Original file line number Diff line number Diff line change 24
24
" utils"
25
25
],
26
26
"dependencies" : {
27
- "@forivall/wyt" : " ^2.0.2" ,
28
27
"gatsby-source-filesystem" : " 2.11.0" ,
29
28
"google-oauth2-env-vars" : " ^1.3.0" ,
30
29
"googleapis" : " 67.1.1" ,
Original file line number Diff line number Diff line change @@ -4,9 +4,9 @@ const _chunk = require("lodash/chunk")
4
4
const _flatten = require ( "lodash/flatten" )
5
5
const GoogleOAuth2 = require ( "google-oauth2-env-vars" )
6
6
const yamljs = require ( "yamljs" )
7
- const wyt = require ( "@forivall/wyt" )
8
7
9
8
const { ENV_TOKEN_VAR } = require ( "./constants" )
9
+ const { wait} = require ( "./wait" )
10
10
11
11
const MIME_TYPE_DOCUMENT = "application/vnd.google-apps.document"
12
12
const MIME_TYPE_FOLDER = "application/vnd.google-apps.folder"
@@ -147,7 +147,7 @@ async function getGoogleDrive() {
147
147
*/
148
148
149
149
// 10 per 1.5 seconds.
150
- const rateLimit = wyt ( 10 , 1500 )
150
+ const rateLimit = wait ( 10 , 1500 )
151
151
const BATCH_SIZE = 100
152
152
/**
153
153
* @param {import('..').Options & FetchDocumentsOptions } options
Original file line number Diff line number Diff line change
1
+ function sleep ( ms ) {
2
+ return new Promise ( ( resolve ) => {
3
+ setTimeout ( ( ) => resolve ( ) , ms )
4
+ } )
5
+ }
6
+
7
+ function reload ( requestsPerInterval , interval , state ) {
8
+ const throughput = requestsPerInterval / interval
9
+ const now = Date . now ( )
10
+ const reloadTime = now - state . lastReload
11
+ const reloadedRequests = reloadTime * throughput
12
+ const newAvalableRequests = state . availableRequests + reloadedRequests
13
+
14
+ return {
15
+ ...state ,
16
+ lastReload : now ,
17
+ availableRequests : Math . min ( newAvalableRequests , requestsPerInterval ) ,
18
+ }
19
+ }
20
+
21
+ function wait ( requestsPerInterval , interval ) {
22
+ const timePerRequest = interval / requestsPerInterval
23
+
24
+ let state = {
25
+ lastReload : Date . now ( ) ,
26
+ availableRequests : requestsPerInterval ,
27
+ }
28
+
29
+ async function waitRequest ( requests = 1 ) {
30
+ if ( requests > requestsPerInterval ) {
31
+ throw new Error (
32
+ "Requests can not be greater than the number of requests per interval"
33
+ )
34
+ }
35
+
36
+ state = reload ( requestsPerInterval , interval , state )
37
+
38
+ const requestsToWait = Math . max ( 0 , requests - state . availableRequests )
39
+ const wait = Math . ceil ( requestsToWait * timePerRequest )
40
+
41
+ state . availableRequests -= requests
42
+ await sleep ( wait )
43
+ return wait
44
+ }
45
+
46
+ return waitRequest
47
+ }
48
+
49
+ exports . wait = wait
Original file line number Diff line number Diff line change 478
478
minimatch "^3.0.4"
479
479
strip-json-comments "^3.1.1"
480
480
481
- " @forivall/wyt@^2.0.2 " :
482
- version "2.0.2"
483
- resolved "https://registry.yarnpkg.com/@forivall/wyt/-/wyt-2.0.2.tgz#2516bfe7cecb21605447dfb37039ab3eea16dab3"
484
- integrity sha512-P5WLG9xdDf5cwzdZPLycrubCwStc1xlSnG1U4FVvCoK1owEClCJU9bK/BU2eD6VeUQy8naK00yt6YbVJqZHWPg==
485
-
486
481
" @iarna/cli@^1.2.0 " :
487
482
version "1.2.0"
488
483
resolved "https://registry.yarnpkg.com/@iarna/cli/-/cli-1.2.0.tgz#0f7af5e851afe895104583c4ca07377a8094d641"
You can’t perform that action at this time.
0 commit comments