@@ -33,7 +33,7 @@ import { utils as nodesUtils } from '../nodes';
33
33
import { utils as keysUtils } from '../keys' ;
34
34
import * as validationUtils from '../validation/utils' ;
35
35
import config from '../config' ;
36
- import { mkdirExists } from '../utils' ;
36
+ import { mkdirExists , ResourceAcquire , RWLock } from '../utils' ;
37
37
import * as nodesPB from '../proto/js/polykey/v1/nodes/nodes_pb' ;
38
38
39
39
/**
@@ -43,7 +43,7 @@ type VaultMap = Map<
43
43
VaultId ,
44
44
{
45
45
vault ?: VaultInternal ;
46
- lock : MutexInterface ;
46
+ lock : RWLock ;
47
47
}
48
48
> ;
49
49
@@ -286,42 +286,23 @@ class VaultManager {
286
286
// replace this transact with our new withF and withG mechanisms
287
287
// all we need to do is create `ResourceAcquire` types in this domain
288
288
289
- /**
290
- * By default will not lock anything
291
- */
292
- public async transact < T > ( f : ( ) => Promise < T > , vaults : Array < VaultId > = [ ] ) {
293
- // Will lock nothing by default
294
- return await this . withLocks ( f , vaults . map ( this . getLock . bind ( this ) ) ) ;
295
- }
296
-
297
- protected async withLocks < T > (
298
- f : ( ) => Promise < T > ,
299
- locks : Array < MutexInterface > = [ ] ,
300
- ) : Promise < T > {
301
- const releases : Array < MutexInterface . Releaser > = [ ] ;
302
- for ( const lock of locks ) {
303
- // Take the lock for each vault in memory and acquire it
304
- releases . push ( await lock . acquire ( ) ) ;
305
- }
306
- try {
307
- return await f ( ) ;
308
- } finally {
309
- // Release the vault locks in the opposite order
310
- releases . reverse ( ) ;
311
- for ( const r of releases ) {
312
- r ( ) ;
313
- }
314
- }
315
- }
316
289
317
- protected getLock ( vaultId : VaultId ) : MutexInterface {
290
+ protected getLock ( vaultId : VaultId ) : RWLock {
318
291
const vaultAndLock = this . vaultMap . get ( vaultId ) ;
319
292
if ( vaultAndLock != null ) return vaultAndLock . lock ;
320
- const lock = new Mutex ( ) ;
293
+ const lock = new RWLock ( ) ;
321
294
this . vaultMap . set ( vaultId , { lock } ) ;
322
295
return lock ;
323
296
}
324
297
298
+ protected getReadLock ( vaultId : VaultId ) : ResourceAcquire < RWLock > {
299
+ const lock = this . getLock ( vaultId ) ;
300
+ const release = lock . acquireRead ( ) ;
301
+ return [ async ( ) => release ( ) , ] ;
302
+ }
303
+
304
+
305
+
325
306
/**
326
307
* Constructs a new vault instance with a given name and
327
308
* stores it in memory
@@ -336,7 +317,7 @@ class VaultManager {
336
317
throw new vaultsErrors . ErrorVaultsVaultDefined ( ) ;
337
318
}
338
319
const vaultId = await this . generateVaultId ( ) ;
339
- const lock = new Mutex ( ) ;
320
+ const lock = new RWLock ( ) ;
340
321
this . vaultMap . set ( vaultId , { lock } ) ;
341
322
return await this . transact ( async ( ) => {
342
323
// Adding vault to name map
@@ -607,6 +588,7 @@ class VaultManager {
607
588
efs : this . efs ,
608
589
logger : this . logger . getChild ( VaultInternal . name ) ,
609
590
} ) ;
591
+ // TODO: We need to add the cloned vaultName to the name->id mapping
610
592
this . vaultMap . set ( vaultId , { lock, vault } ) ;
611
593
this . logger . info (
612
594
`Cloned Vault ${ vaultsUtils . encodeVaultId ( vaultId ) } on Node ${ nodeId } ` ,
0 commit comments