Skip to content

Commit

Permalink
WIP - #305 - EOD
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Feb 18, 2022
1 parent fe12502 commit 04b39ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import * as vaultsUtils from './utils';
import * as vaultsErrors from './errors';
import { withF, withG } from '../utils';
import { RWLock, withF, withG } from '../utils';
import { utils as nodesUtils } from '../nodes';

// TODO: Update creation of metadata.
Expand Down
46 changes: 14 additions & 32 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { utils as nodesUtils } from '../nodes';
import { utils as keysUtils } from '../keys';
import * as validationUtils from '../validation/utils';
import config from '../config';
import { mkdirExists } from '../utils';
import { mkdirExists, ResourceAcquire, RWLock } from '../utils';
import * as nodesPB from '../proto/js/polykey/v1/nodes/nodes_pb';

/**
Expand All @@ -43,7 +43,7 @@ type VaultMap = Map<
VaultId,
{
vault?: VaultInternal;
lock: MutexInterface;
lock: RWLock;
}
>;

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

/**
* By default will not lock anything
*/
public async transact<T>(f: () => Promise<T>, vaults: Array<VaultId> = []) {
// Will lock nothing by default
return await this.withLocks(f, vaults.map(this.getLock.bind(this)));
}

protected async withLocks<T>(
f: () => Promise<T>,
locks: Array<MutexInterface> = [],
): Promise<T> {
const releases: Array<MutexInterface.Releaser> = [];
for (const lock of locks) {
// Take the lock for each vault in memory and acquire it
releases.push(await lock.acquire());
}
try {
return await f();
} finally {
// Release the vault locks in the opposite order
releases.reverse();
for (const r of releases) {
r();
}
}
}

protected getLock(vaultId: VaultId): MutexInterface {
protected getLock(vaultId: VaultId): RWLock {
const vaultAndLock = this.vaultMap.get(vaultId);
if (vaultAndLock != null) return vaultAndLock.lock;
const lock = new Mutex();
const lock = new RWLock();
this.vaultMap.set(vaultId, { lock });
return lock;
}

protected getReadLock(vaultId: VaultId): ResourceAcquire<RWLock> {
const lock = this.getLock(vaultId);
const release = lock.acquireRead();
return [async () => release(),];
}



/**
* Constructs a new vault instance with a given name and
* stores it in memory
Expand All @@ -336,7 +317,7 @@ class VaultManager {
throw new vaultsErrors.ErrorVaultsVaultDefined();
}
const vaultId = await this.generateVaultId();
const lock = new Mutex();
const lock = new RWLock();
this.vaultMap.set(vaultId, { lock });
return await this.transact(async () => {
// Adding vault to name map
Expand Down Expand Up @@ -607,6 +588,7 @@ class VaultManager {
efs: this.efs,
logger: this.logger.getChild(VaultInternal.name),
});
// TODO: We need to add the cloned vaultName to the name->id mapping
this.vaultMap.set(vaultId, { lock, vault });
this.logger.info(
`Cloned Vault ${vaultsUtils.encodeVaultId(vaultId)} on Node ${nodeId}`,
Expand Down

0 comments on commit 04b39ff

Please sign in to comment.