@@ -7,6 +7,21 @@ import { ExecOptions } from '@actions/exec/lib/interfaces'
77import { promises as fs } from 'fs'
88import { assertManifests , Manifest , Manifests } from './Tar'
99import format from 'string-format'
10+ import PromisePool from 'native-promise-pool'
11+
12+ type ReturnPromiseFunc = < T > ( arg : any ) => Promise < T [ ] >
13+
14+ const createPromiseProducerFromArray = < T , T2 > ( array : T [ ] , callBackFunc : ( ( t : T ) => Promise < T2 > ) ) => {
15+ const currentIndex = 0 ;
16+
17+ return ( ) => callBackFunc ( array [ currentIndex ] )
18+ }
19+
20+ const generatePromise = function * < T1 , T2 > ( array : T1 [ ] , callbackFunc : ( ( t : T1 ) => Promise < T2 > ) ) {
21+ for ( let i = 0 ; i < array . length ; i ++ ) {
22+ yield callbackFunc ( array [ i ] )
23+ }
24+ }
1025
1126class LayerCache {
1227 // repotag: string
@@ -18,6 +33,7 @@ class LayerCache {
1833 enabledParallel = true
1934 // unpackedTarDir: string = ''
2035 // manifests: Manifests = []
36+ concurrency : number = 4
2137
2238 constructor ( ids : string [ ] ) {
2339 // this.repotag = repotag
@@ -108,7 +124,16 @@ class LayerCache {
108124 }
109125
110126 private async storeLayers ( ) : Promise < number [ ] > {
111- return await Promise . all ( ( await this . getLayerIds ( ) ) . map ( layerId => this . storeSingleLayerBy ( layerId ) ) )
127+ const pool = new PromisePool ( this . concurrency )
128+
129+ const result = Promise . all (
130+ ( await this . getLayerIds ( ) ) . map (
131+ layerId => {
132+ return pool . open ( ( ) => this . storeSingleLayerBy ( layerId ) )
133+ }
134+ )
135+ )
136+ return result
112137 }
113138
114139 static async dismissCacheAlreadyExistsError ( promise : Promise < number > ) : Promise < number > {
@@ -174,15 +199,34 @@ class LayerCache {
174199
175200 private async restoreLayers ( ) {
176201 const restoring = ( await this . getLayerIds ( ) ) . map ( layerId => this . restoreSingleLayerBy ( layerId ) ) ;
177- const restoredLayerKeysThatMayContainUndefined = await Promise . all ( restoring )
202+
203+ const promiseIter = generatePromise ( await this . getLayerIds ( ) , layerId => this . restoreSingleLayerBy ( layerId ) )
204+
205+ const pool = new PromisePool ( this . concurrency )
206+
207+ const restoredLayerKeysThatMayContainUndefined = await Promise . all (
208+ ( await this . getLayerIds ( ) ) . map (
209+ layerId => {
210+ return pool . open ( ( ) => this . restoreSingleLayerBy ( layerId ) )
211+ }
212+ )
213+ )
214+
178215 core . debug ( JSON . stringify ( { log : `restoreLayers` , restoredLayerKeysThatMayContainUndefined } ) )
179216 const FailedToRestore = ( restored : string | undefined ) => restored === undefined
180217 return restoredLayerKeysThatMayContainUndefined . filter ( FailedToRestore ) . length === 0
181218 }
182219
183- private async restoreSingleLayerBy ( id : string ) : Promise < string | undefined > {
220+ private async restoreSingleLayerBy ( id : string ) : Promise < string > {
184221 core . debug ( JSON . stringify ( { log : `restoreSingleLayerBy` , id } ) )
185- return await cache . restoreCache ( [ this . genSingleLayerStorePath ( id ) ] , this . genSingleLayerStoreKey ( id ) )
222+
223+ const result = await cache . restoreCache ( [ this . genSingleLayerStorePath ( id ) ] , this . genSingleLayerStoreKey ( id ) )
224+
225+ if ( result == null ) {
226+ throw new Error ( `Layer cache not found: ${ JSON . stringify ( { id } ) } ` )
227+ }
228+
229+ return result
186230 }
187231
188232 private async loadImageFromUnpacked ( ) {
0 commit comments