@@ -24,6 +24,7 @@ import { StorageBackendsManager } from "./StorageBackendsManager";
24
24
import { StorageBlocksCache } from "./StorageBlocksCache" ;
25
25
import { JobOrchestrationService } from "./JobOrchestrationService" ;
26
26
import { BlobsController } from "../data-access/BlobsController" ;
27
+ import { NumberDictionary } from "acts-util-core" ;
27
28
28
29
@Injectable
29
30
export class StorageBlocksManager
@@ -33,6 +34,7 @@ export class StorageBlocksManager
33
34
private jobOrchestrationService : JobOrchestrationService , private blobsController : BlobsController
34
35
)
35
36
{
37
+ this . inFlightRequests = { } ;
36
38
this . storageBlockAcquirementLock = new Lock ;
37
39
}
38
40
@@ -60,12 +62,17 @@ export class StorageBlocksManager
60
62
if ( cached !== undefined )
61
63
return cached ;
62
64
63
- const read = await this . DownloadEncryptedStorageBlock ( storageBlockId ) ;
64
- const encryptionInfo = await this . storageBlocksController . QueryEncryptionInfo ( storageBlockId ) ;
65
- const decrypted = await this . storageEncryptionManager . Decrypt ( this . GetPartitionNumber ( storageBlockId ) , read , encryptionInfo ! . iv , encryptionInfo ! . authTag ) ;
65
+ const inFlightRequest = this . inFlightRequests [ storageBlockId ] ;
66
+ if ( inFlightRequest !== undefined )
67
+ return inFlightRequest ;
66
68
67
- this . storageBlocksCache . AddToCache ( storageBlockId , decrypted ) ;
68
- return decrypted ;
69
+ const promise = this . DownloadAndDecryptStorageBlock ( storageBlockId ) ;
70
+ this . inFlightRequests [ storageBlockId ] = promise ;
71
+
72
+ const result = await promise ;
73
+ this . inFlightRequests [ storageBlockId ] = undefined ;
74
+
75
+ return result ;
69
76
}
70
77
71
78
public async FreeUnreferencedStorageBlock ( storageBlockId : number )
@@ -169,6 +176,16 @@ export class StorageBlocksManager
169
176
} ) ;
170
177
}
171
178
179
+ private async DownloadAndDecryptStorageBlock ( storageBlockId : number )
180
+ {
181
+ const read = await this . DownloadEncryptedStorageBlock ( storageBlockId ) ;
182
+ const encryptionInfo = await this . storageBlocksController . QueryEncryptionInfo ( storageBlockId ) ;
183
+ const decrypted = await this . storageEncryptionManager . Decrypt ( this . GetPartitionNumber ( storageBlockId ) , read , encryptionInfo ! . iv , encryptionInfo ! . authTag ) ;
184
+
185
+ this . storageBlocksCache . AddToCache ( storageBlockId , decrypted ) ;
186
+ return decrypted ;
187
+ }
188
+
172
189
private async DownloadEncryptedStorageBlock ( storageBlockId : number )
173
190
{
174
191
const backend = await this . storageBackendsManager . FindFastestBackendForReading ( storageBlockId ) ;
@@ -264,5 +281,6 @@ export class StorageBlocksManager
264
281
}
265
282
266
283
//State
284
+ private inFlightRequests : NumberDictionary < Promise < Buffer > > ;
267
285
private storageBlockAcquirementLock : Lock ;
268
286
}
0 commit comments