@conduitplatform/grpc-sdk
Advanced tools
Comparing version 0.16.0-alpha.29 to 0.16.0-rc.0
import { RedisManager } from './RedisManager'; | ||
import { Lock } from 'redlock'; | ||
import { Indexable } from '../interfaces'; | ||
export declare enum KNOWN_LOCKS { | ||
STATE_MODIFICATION = "state_modification" | ||
} | ||
export declare class StateManager { | ||
private readonly redisClient; | ||
private readonly redLock; | ||
constructor(redisManager: RedisManager, name: string); | ||
acquireLock(resource: string, ttl?: number): Promise<Lock>; | ||
releaseLock(lock: Lock): Promise<void>; | ||
modifyState(modifier: (state: Indexable) => Promise<Indexable>): Promise<void>; | ||
setState(stateObj: any): Promise<"OK">; | ||
@@ -6,0 +15,0 @@ getState(): Promise<string | null>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StateManager = void 0; | ||
exports.StateManager = exports.KNOWN_LOCKS = void 0; | ||
const redlock_1 = __importDefault(require("redlock")); | ||
var KNOWN_LOCKS; | ||
(function (KNOWN_LOCKS) { | ||
KNOWN_LOCKS["STATE_MODIFICATION"] = "state_modification"; | ||
})(KNOWN_LOCKS = exports.KNOWN_LOCKS || (exports.KNOWN_LOCKS = {})); | ||
class StateManager { | ||
constructor(redisManager, name) { | ||
this.redisClient = redisManager.getClient({ keyPrefix: name + '_' }); | ||
this.redLock = new redlock_1.default([this.redisClient], { | ||
// The expected clock drift; for more details see: | ||
// http://redis.io/topics/distlock | ||
driftFactor: 0.01, | ||
// The max number of times Redlock will attempt to lock a resource | ||
// before error. | ||
retryCount: 10, | ||
// the time in ms between attempts | ||
retryDelay: 100, | ||
// the max time in ms randomly added to retries | ||
// to improve performance under high contention | ||
// see https://www.awsarchitectureblog.com/2015/03/backoff.html | ||
retryJitter: 200, | ||
// The minimum remaining time on a lock before an extension is automatically | ||
// attempted with the `using` API. | ||
automaticExtensionThreshold: 500, // time in ms | ||
}); | ||
} | ||
async acquireLock(resource, ttl = 5000) { | ||
return await this.redLock.acquire([resource], ttl); | ||
} | ||
async releaseLock(lock) { | ||
await lock.release(); | ||
} | ||
async modifyState(modifier) { | ||
var _a; | ||
const lock = await this.acquireLock(KNOWN_LOCKS.STATE_MODIFICATION); | ||
try { | ||
const retrievedState = (_a = (await this.getState())) !== null && _a !== void 0 ? _a : '{}'; | ||
const currentState = JSON.parse(retrievedState); | ||
const newState = await modifier(currentState); | ||
await this.setState(JSON.stringify(newState)); | ||
} | ||
finally { | ||
await this.releaseLock(lock); | ||
} | ||
} | ||
setState(stateObj) { | ||
@@ -9,0 +53,0 @@ return this.setKey('state', stateObj); |
{ | ||
"name": "@conduitplatform/grpc-sdk", | ||
"version": "0.16.0-alpha.29", | ||
"version": "0.16.0-rc.0", | ||
"main": "dist/index.js", | ||
@@ -25,3 +25,3 @@ "types": "dist/index.d.ts", | ||
"express": "^4.18.2", | ||
"fast-jwt": "^3.3.0", | ||
"fast-jwt": "^3.3.2", | ||
"fs-extra": "^11.1.1", | ||
@@ -32,4 +32,5 @@ "ioredis": "^5.3.2", | ||
"nice-grpc-client-middleware-retry": "^3", | ||
"prom-client": "^14.2.0", | ||
"prom-client": "^15.0.0", | ||
"protobufjs": "^7.2.4", | ||
"redlock": "^5.0.0-beta.2", | ||
"winston": "^3.8.2", | ||
@@ -36,0 +37,0 @@ "winston-loki": "^6.0.7" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1189537
18577
19
+ Addedredlock@^5.0.0-beta.2
+ Added@grpc/grpc-js@1.12.4(transitive)
+ Added@opentelemetry/api@1.9.0(transitive)
+ Addednode-abort-controller@3.1.1(transitive)
+ Addedprom-client@15.1.3(transitive)
+ Addedredlock@5.0.0-beta2(transitive)
- Removed@grpc/grpc-js@1.12.5(transitive)
- Removedprom-client@14.2.0(transitive)
Updatedfast-jwt@^3.3.2
Updatedprom-client@^15.0.0