44namespace ScriptFUSION \Porter \Connector ;
55
66use Amp \Promise ;
7+ use ScriptFUSION \Async \Throttle \Throttle ;
78use ScriptFUSION \Porter \Cache \CacheUnavailableException ;
89use ScriptFUSION \Porter \Connector \Recoverable \RecoverableException ;
910use ScriptFUSION \Porter \Connector \Recoverable \RecoverableExceptionHandler ;
1011use ScriptFUSION \Porter \Connector \Recoverable \StatelessRecoverableExceptionHandler ;
12+ use function Amp \call ;
1113use function Amp \Promise \all ;
1214use function ScriptFUSION \Retry \retry ;
1315use function ScriptFUSION \Retry \retryAsync ;
@@ -40,6 +42,8 @@ final class ImportConnector implements ConnectorWrapper
4042
4143 private $ maxFetchAttempts ;
4244
45+ private $ throttle ;
46+
4347 /**
4448 * @param Connector|AsyncConnector $connector Wrapped connector.
4549 * @param RecoverableExceptionHandler $recoverableExceptionHandler User's recoverable exception handler.
@@ -50,7 +54,8 @@ public function __construct(
5054 $ connector ,
5155 RecoverableExceptionHandler $ recoverableExceptionHandler ,
5256 int $ maxFetchAttempts ,
53- bool $ mustCache
57+ bool $ mustCache ,
58+ ?Throttle $ throttle
5459 ) {
5560 if ($ mustCache && !$ connector instanceof CachingConnector) {
5661 throw CacheUnavailableException::createUnsupported ();
@@ -64,6 +69,7 @@ public function __construct(
6469 );
6570 $ this ->userExceptionHandler = $ recoverableExceptionHandler ;
6671 $ this ->maxFetchAttempts = $ maxFetchAttempts ;
72+ $ this ->throttle = $ throttle ;
6773 }
6874
6975 /**
@@ -96,7 +102,11 @@ public function fetchAsync(AsyncDataSource $source): Promise
96102 return retryAsync (
97103 $ this ->maxFetchAttempts ,
98104 function () use ($ source ): Promise {
99- return $ this ->connector ->fetchAsync ($ source );
105+ return call (function () use ($ source ): \Generator {
106+ yield $ this ->throttle ->await ($ response = $ this ->connector ->fetchAsync ($ source ));
107+
108+ return yield $ response ;
109+ });
100110 },
101111 $ this ->createExceptionHandler ()
102112 );
0 commit comments