From d5f4ac690ff1c344599a576619ee7f00e1cc0ce1 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Mon, 24 Dec 2018 13:16:35 +0000 Subject: [PATCH] refactor: Only promisify read and write locks once Change-type: patch Signed-off-by: Cameron Diver --- src/config/configJson.ts | 10 +--------- src/device-state.coffee | 6 ++---- src/lib/update-lock.ts | 15 +++++++++++++-- src/logger.ts | 8 ++------ 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/config/configJson.ts b/src/config/configJson.ts index 31bec6ef..953a2a18 100644 --- a/src/config/configJson.ts +++ b/src/config/configJson.ts @@ -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 = () => diff --git a/src/device-state.coffee b/src/device-state.coffee index 9172530a..b0d9ed1c 100644 --- a/src/device-state.coffee +++ b/src/device-state.coffee @@ -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 diff --git a/src/lib/update-lock.ts b/src/lib/update-lock.ts index 4e794c0e..0386f58a 100644 --- a/src/lib/update-lock.ts +++ b/src/lib/update-lock.ts @@ -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 { 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()); + } } diff --git a/src/logger.ts b/src/logger.ts index c89ef35d..f3f195c9 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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(); }); }