Skip to content

Commit 04b39ff

Browse files
committed
WIP - #305 - EOD
1 parent fe12502 commit 04b39ff

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

src/vaults/VaultInternal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
2626
import * as vaultsUtils from './utils';
2727
import * as vaultsErrors from './errors';
28-
import { withF, withG } from '../utils';
28+
import { RWLock, withF, withG } from '../utils';
2929
import { utils as nodesUtils } from '../nodes';
3030

3131
// TODO: Update creation of metadata.

src/vaults/VaultManager.ts

+14-32
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { utils as nodesUtils } from '../nodes';
3333
import { utils as keysUtils } from '../keys';
3434
import * as validationUtils from '../validation/utils';
3535
import config from '../config';
36-
import { mkdirExists } from '../utils';
36+
import { mkdirExists, ResourceAcquire, RWLock } from '../utils';
3737
import * as nodesPB from '../proto/js/polykey/v1/nodes/nodes_pb';
3838

3939
/**
@@ -43,7 +43,7 @@ type VaultMap = Map<
4343
VaultId,
4444
{
4545
vault?: VaultInternal;
46-
lock: MutexInterface;
46+
lock: RWLock;
4747
}
4848
>;
4949

@@ -286,42 +286,23 @@ class VaultManager {
286286
// replace this transact with our new withF and withG mechanisms
287287
// all we need to do is create `ResourceAcquire` types in this domain
288288

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-
}
316289

317-
protected getLock(vaultId: VaultId): MutexInterface {
290+
protected getLock(vaultId: VaultId): RWLock {
318291
const vaultAndLock = this.vaultMap.get(vaultId);
319292
if (vaultAndLock != null) return vaultAndLock.lock;
320-
const lock = new Mutex();
293+
const lock = new RWLock();
321294
this.vaultMap.set(vaultId, { lock });
322295
return lock;
323296
}
324297

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+
325306
/**
326307
* Constructs a new vault instance with a given name and
327308
* stores it in memory
@@ -336,7 +317,7 @@ class VaultManager {
336317
throw new vaultsErrors.ErrorVaultsVaultDefined();
337318
}
338319
const vaultId = await this.generateVaultId();
339-
const lock = new Mutex();
320+
const lock = new RWLock();
340321
this.vaultMap.set(vaultId, { lock });
341322
return await this.transact(async () => {
342323
// Adding vault to name map
@@ -607,6 +588,7 @@ class VaultManager {
607588
efs: this.efs,
608589
logger: this.logger.getChild(VaultInternal.name),
609590
});
591+
// TODO: We need to add the cloned vaultName to the name->id mapping
610592
this.vaultMap.set(vaultId, { lock, vault });
611593
this.logger.info(
612594
`Cloned Vault ${vaultsUtils.encodeVaultId(vaultId)} on Node ${nodeId}`,

0 commit comments

Comments
 (0)