refactor: Only promisify read and write locks once

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2018-12-24 13:16:35 +00:00
parent 9decea1d3b
commit d5f4ac690f
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
4 changed files with 18 additions and 21 deletions

View File

@ -2,18 +2,15 @@ import * as Promise from 'bluebird';
import * as _ from 'lodash';
import { fs } from 'mz';
import * as path from 'path';
import * as Lock from 'rwlock';
import { ConfigSchema, ConfigValue } from '../lib/types';
import { readLock, writeLock } from '../lib/update-lock';
import * as constants from '../lib/constants';
import { writeAndSyncFile, writeFileAtomic } from '../lib/fs-utils';
import * as osRelease from '../lib/os-release';
type LockCallback = (file: string) => Promise<() => void>;
export default class ConfigJsonConfigBackend {
private lock: Lock;
private readLockConfigJson: () => Promise.Disposer<() => void>;
private writeLockConfigJson: () => Promise.Disposer<() => void>;
@ -25,12 +22,7 @@ export default class ConfigJsonConfigBackend {
public constructor(schema: ConfigSchema, configPath?: string) {
this.configPath = configPath;
this.schema = schema;
this.lock = new Lock();
const writeLock: LockCallback = Promise.promisify(
this.lock.async.writeLock,
);
const readLock: LockCallback = Promise.promisify(this.lock.async.readLock);
this.writeLockConfigJson = () =>
writeLock('config.json').disposer(release => release());
this.readLockConfigJson = () =>

View File

@ -1,6 +1,5 @@
Promise = require 'bluebird'
_ = require 'lodash'
Lock = require 'rwlock'
EventEmitter = require 'events'
fs = Promise.promisifyAll(require('fs'))
express = require 'express'
@ -122,9 +121,8 @@ module.exports = class DeviceState extends EventEmitter
@on 'error', (err) ->
console.error('Error in deviceState: ', err, err.stack)
@_currentVolatile = {}
_lock = new Lock()
@_writeLock = Promise.promisify(_lock.async.writeLock)
@_readLock = Promise.promisify(_lock.async.readLock)
@_writeLock = updateLock.writeLock
@_readLock = updateLock.readLock
@lastSuccessfulUpdate = null
@failedUpdates = 0
@applyInProgress = false

View File

@ -46,8 +46,14 @@ process.on('exit', () => {
}
});
type LockFn = (key: string | number) => Bluebird<() => void>;
const locker = new Lock();
const writeLock = Bluebird.promisify(locker.async.writeLock).bind(locker);
export const writeLock: LockFn = Bluebird.promisify(locker.async.writeLock, {
context: locker,
});
export const readLock: LockFn = Bluebird.promisify(locker.async.readLock, {
context: locker,
});
function dispose(release: () => void): Bluebird<void> {
return Bluebird.map(_.keys(locksTaken), lockName => {
@ -101,5 +107,10 @@ export function lock(
.disposer(dispose);
};
return Bluebird.using(takeTheLock(), fn);
const disposer = takeTheLock();
if (disposer) {
return Bluebird.using(disposer, fn);
} else {
return Bluebird.resolve(fn());
}
}

View File

@ -1,11 +1,11 @@
import * as Bluebird from 'bluebird';
import * as es from 'event-stream';
import * as _ from 'lodash';
import * as Lock from 'rwlock';
import { EventTracker } from './event-tracker';
import Docker = require('./lib/docker-utils');
import { LogType } from './lib/log-types';
import { writeLock } from './lib/update-lock';
import {
LocalLogBackend,
LogBackend,
@ -34,10 +34,6 @@ interface LoggerConstructOptions {
}
export class Logger {
private writeLock: (key: string) => Bluebird<() => void> = Bluebird.promisify(
new Lock().async.writeLock,
);
private backend: LogBackend | null = null;
private balenaBackend: BalenaLogBackend | null = null;
private localBackend: LocalLogBackend | null = null;
@ -130,7 +126,7 @@ export class Logger {
}
public lock(containerId: string): Bluebird.Disposer<() => void> {
return this.writeLock(containerId).disposer(release => {
return writeLock(containerId).disposer(release => {
release();
});
}