@@ -3,14 +3,19 @@ import { assert, it, vi } from "@effect/vitest";
33import * as NodeServices from "@effect/platform-node/NodeServices" ;
44import * as ConfigProvider from "effect/ConfigProvider" ;
55import * as DateTime from "effect/DateTime" ;
6+ import * as Deferred from "effect/Deferred" ;
67import * as Effect from "effect/Effect" ;
8+ import * as Fiber from "effect/Fiber" ;
79import * as FileSystem from "effect/FileSystem" ;
810import * as Layer from "effect/Layer" ;
911import * as Option from "effect/Option" ;
12+ import * as Queue from "effect/Queue" ;
1013import * as Schema from "effect/Schema" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
1115import { HttpClient , HttpClientError , HttpClientRequest , HttpClientResponse } from "effect/http" ;
1216
1317import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi" ;
18+ import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit" ;
1419import * as ServerSettings from "../serverSettings.ts" ;
1520import * as GitVcsDriver from "../vcs/GitVcsDriver.ts" ;
1621import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts" ;
@@ -60,6 +65,8 @@ const repositoryJson = {
6065
6166function makeLayer ( input : {
6267 readonly response : ( request : HttpClientRequest . HttpClientRequest ) => Response ;
68+ /** Holds the answer until it resolves, in place of answering at once. */
69+ readonly answer ?: ( response : Response ) => Effect . Effect < Response > ;
6370 readonly requestFailure ?: (
6471 request : HttpClientRequest . HttpClientRequest ,
6572 ) => HttpClientError . HttpClientError ;
@@ -72,7 +79,9 @@ function makeLayer(input: {
7279 ? Effect . never
7380 : input . requestFailure
7481 ? Effect . fail ( input . requestFailure ( request ) )
75- : Effect . succeed ( HttpClientResponse . fromWeb ( request , input . response ( request ) ) ) ,
82+ : ( input . answer ?? Effect . succeed ) ( input . response ( request ) ) . pipe (
83+ Effect . map ( ( response ) => HttpClientResponse . fromWeb ( request , response ) ) ,
84+ ) ,
7685 ) ;
7786 const gitMock = {
7887 readConfigValue : vi . fn < GitVcsDriver . GitVcsDriver [ "Service" ] [ "readConfigValue" ] > ( ( ) =>
@@ -144,6 +153,7 @@ function makeLayer(input: {
144153
145154 const layer = BitbucketApi . layer . pipe (
146155 Layer . provide ( ServerSourceControlHost . layer ) ,
156+ Layer . provide ( SourceControlRateLimit . layer ) ,
147157 Layer . provide ( Layer . mock ( VcsProcess . VcsProcess ) ( { } ) ) ,
148158 Layer . provide (
149159 Layer . succeed (
@@ -736,3 +746,84 @@ it.effect("cuts a response short rather than reading an unbounded diff into memo
736746 ) ,
737747 ) ,
738748) ;
749+
750+ it . effect ( "pauses background reads after a 429 until Bitbucket's retry-after passes" , ( ) => {
751+ let answered = 0 ;
752+ const { execute, layer } = makeLayer ( {
753+ response : ( ) => {
754+ answered += 1 ;
755+ return answered === 1
756+ ? new Response ( "Rate limit exceeded" , { status : 429 , headers : { "retry-after" : "60" } } )
757+ : Response . json ( { values : [ ] } ) ;
758+ } ,
759+ } ) ;
760+
761+ return Effect . gen ( function * ( ) {
762+ const bitbucket = yield * BitbucketApi . BitbucketApi ;
763+ const lookup = bitbucket . listPullRequests ( {
764+ cwd : "/repo" ,
765+ headSelector : "feature/a" ,
766+ state : "all" ,
767+ } ) ;
768+
769+ yield * Effect . flip ( lookup ) ;
770+ const paused = yield * Effect . flip ( lookup ) ;
771+ // Keyed by the repository host, where PullRequestService records its own backoff.
772+ assert . deepInclude ( paused , {
773+ _tag : "SourceControlRateLimitPausedError" ,
774+ host : "bitbucket.org" ,
775+ retryAt : 60_000 ,
776+ } ) ;
777+ assert . strictEqual ( execute . mock . calls . length , 1 ) ;
778+
779+ // A user waiting on an answer is not held back by the background pause, and their success
780+ // does not end it early.
781+ yield * lookup . pipe ( Effect . provideService ( SourceControlRateLimit . Interactive , true ) ) ;
782+ yield * bitbucket . request ( {
783+ method : "POST" ,
784+ url : "/repositories/acme/web/pullrequests/1/approve" ,
785+ } ) ;
786+
787+ yield * TestClock . adjust ( "59 seconds" ) ;
788+ assert . strictEqual ( ( yield * Effect . flip ( lookup ) ) . _tag , "SourceControlRateLimitPausedError" ) ;
789+
790+ yield * TestClock . adjust ( "1 second" ) ;
791+ yield * lookup ;
792+ } ) . pipe ( Effect . provide ( layer ) ) ;
793+ } ) ;
794+
795+ it . effect ( "keeps a sweep over many branches to a few Bitbucket requests at a time" , ( ) =>
796+ Effect . gen ( function * ( ) {
797+ const started = yield * Queue . unbounded < void > ( ) ;
798+ const release = yield * Deferred . make < void > ( ) ;
799+ let inFlight = 0 ;
800+ let peak = 0 ;
801+ const { layer } = makeLayer ( {
802+ response : ( ) => Response . json ( { values : [ ] } ) ,
803+ answer : ( response ) =>
804+ Effect . gen ( function * ( ) {
805+ inFlight += 1 ;
806+ peak = Math . max ( peak , inFlight ) ;
807+ yield * Queue . offer ( started , undefined ) ;
808+ yield * Deferred . await ( release ) ;
809+ inFlight -= 1 ;
810+ return response ;
811+ } ) ,
812+ } ) ;
813+
814+ yield * Effect . gen ( function * ( ) {
815+ const bitbucket = yield * BitbucketApi . BitbucketApi ;
816+ const sweep = yield * Effect . forEach (
817+ Array . from ( { length : 12 } , ( _ , index ) => `feature/${ index } ` ) ,
818+ ( branch ) =>
819+ bitbucket . listPullRequests ( { cwd : "/repo" , headSelector : branch , state : "all" } ) ,
820+ { concurrency : "unbounded" , discard : true } ,
821+ ) . pipe ( Effect . forkChild ) ;
822+ for ( let index = 0 ; index < 4 ; index += 1 ) yield * Queue . take ( started ) ;
823+ yield * Deferred . succeed ( release , undefined ) ;
824+ yield * Fiber . join ( sweep ) ;
825+ } ) . pipe ( Effect . provide ( layer ) ) ;
826+
827+ assert . strictEqual ( peak , 4 ) ;
828+ } ) ,
829+ ) ;
0 commit comments