From 65cb03c22f63d7cf4bf0055f64c1199a361c3b35 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 19 Apr 2017 10:51:34 -0700 Subject: [PATCH] packager: GlobalTransformCache: throat() the file fetching function Reviewed By: cpojer Differential Revision: D4914187 fbshipit-source-id: ae3b8aa4ed4dbd1896ca35cf498b7c02fd109f58 --- .../metro-bundler/src/lib/GlobalTransformCache.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/metro-bundler/src/lib/GlobalTransformCache.js b/packages/metro-bundler/src/lib/GlobalTransformCache.js index 2304f0ab..0a64db55 100644 --- a/packages/metro-bundler/src/lib/GlobalTransformCache.js +++ b/packages/metro-bundler/src/lib/GlobalTransformCache.js @@ -18,6 +18,7 @@ const crypto = require('crypto'); const fetch = require('node-fetch'); const jsonStableStringify = require('json-stable-stringify'); const path = require('path'); +const throat = require('throat'); import type { Options as TransformWorkerOptions, @@ -260,7 +261,7 @@ class URIBasedGlobalTransformCache { * a second time if we expect them to be transient. We might even consider * waiting a little time before retring if experience shows it's useful. */ - static fetchResultFromURI(uri: string): Promise { + static _fetchResultFromURIWithRetry(uri: string): Promise { return URIBasedGlobalTransformCache._fetchResultFromURI(uri).catch(error => { if (!URIBasedGlobalTransformCache.shouldRetryAfterThatError(error)) { throw error; @@ -269,6 +270,12 @@ class URIBasedGlobalTransformCache { }); } + /** + * The exposed version uses throat() to limit concurrency, as making too many parallel requests + * is more likely to trigger server-side throttling and cause timeouts. + */ + static fetchResultFromURI: (uri: string) => Promise; + /** * We want to retry timeouts as they're likely temporary. We retry 503 * (Service Unavailable) and 502 (Bad Gateway) because they may be caused by a @@ -311,6 +318,9 @@ class URIBasedGlobalTransformCache { } +URIBasedGlobalTransformCache.fetchResultFromURI = + throat(500, URIBasedGlobalTransformCache._fetchResultFromURIWithRetry); + class OptionsHasher { _rootPath: string; _cache: WeakMap;