Skip to content

Commit

Permalink
Make timers obvious (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
doochik authored Nov 28, 2024
1 parent 3419696 commit 7f4650e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 72 deletions.
109 changes: 38 additions & 71 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
import { Cluster, Redis } from 'ioredis';

export interface Options {
// key TTL in seconds (default: 60 * 60 * 24)
/** key TTL in seconds (default: 60 * 60 * 24) */
defaultKeyTTL?: number;
// increment generation to invalidate all key across breaking changes releases (default: 1)
/** increment generation to invalidate all key across breaking changes releases (default: 1) */
generation?: number;
// read timeout in milliseconds (default: 100)
/** read timeout in milliseconds (default: 100) */
readTimeout?: number;
/** redis config */
redis: RedisOptions | { startupNodes: ClusterNode[], options?: ClusterOptions };
}

Expand All @@ -24,6 +25,11 @@ interface Logger {
log(event: LoggerEvent): void;
}

interface Timers {
start: number;
end: number;
}

export type LoggerEvent = (
{
type: EVENT.REDIS_CACHE_INITIALIZED;
Expand All @@ -42,50 +48,35 @@ export type LoggerEvent = (
type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_READ_TIMEOUT;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_READ_ERROR;
error: Error;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
data: unknown;
error: unknown;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_READ_DONE;
data: unknown;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_WRITE_START;
Expand All @@ -98,38 +89,27 @@ export type LoggerEvent = (
error: unknown;
key: string;
normalizedKey: string;
timers: {
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_WRITE_ERROR;
error: unknown;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_WRITE_FAILED;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
} |
{
type: EVENT.REDIS_CACHE_WRITE_DONE;
data: string;
key: string;
normalizedKey: string;
timers: {
network: [number, number];
total: [number, number];
}
timers: Timers
}
);

Expand Down Expand Up @@ -177,24 +157,19 @@ export class Cache<Result> implements CacheInterface<Result> {
normalizedKey,
});

const networkTimerStart = process.hrtime();
const totalTimerStart = process.hrtime();

const start = Date.now();
let isTimeout = false;

const timer = setTimeout(() => {
isTimeout = true;

const networkTimer = process.hrtime(networkTimerStart);
const totalTimer = process.hrtime(totalTimerStart);

this.#log({
type: EVENT.REDIS_CACHE_READ_TIMEOUT,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});

Expand All @@ -208,34 +183,31 @@ export class Cache<Result> implements CacheInterface<Result> {
return;
}

const networkTimer = process.hrtime(networkTimerStart);
clearTimeout(timer);

if (error) {
const totalTimer = process.hrtime(totalTimerStart);
this.#log({
type: EVENT.REDIS_CACHE_READ_ERROR,
error,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});

reject(deError({
id: EVENT.REDIS_CACHE_READ_ERROR,
}));
} else if (!data) {
const totalTimer = process.hrtime(totalTimerStart);
this.#log({
type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});

Expand All @@ -247,16 +219,15 @@ export class Cache<Result> implements CacheInterface<Result> {
try {
parsedValue = JSON.parse(data);
} catch (error) {
const totalTimer = process.hrtime(totalTimerStart);
this.#log({
type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
data,
error,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});

Expand All @@ -266,15 +237,14 @@ export class Cache<Result> implements CacheInterface<Result> {
return;
}

const totalTimer = process.hrtime(totalTimerStart);
this.#log({
type: EVENT.REDIS_CACHE_READ_DONE,
data,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});

Expand All @@ -289,7 +259,7 @@ export class Cache<Result> implements CacheInterface<Result> {
return Promise.resolve();
}

const totalTimerStart = process.hrtime();
const start = Date.now();
const normalizedKey = this.#normalizeKey(key);

return new Promise<void>((resolve, reject) => {
Expand All @@ -303,15 +273,15 @@ export class Cache<Result> implements CacheInterface<Result> {
try {
json = JSON.stringify(value);
} catch (error) {
const totalTimer = process.hrtime(totalTimerStart);
this.#log({
type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
data: value,
error,
key,
normalizedKey,
timers: {
total: totalTimer,
start,
end: Date.now(),
},
});
reject(deError({
Expand All @@ -320,20 +290,17 @@ export class Cache<Result> implements CacheInterface<Result> {
return;
}

const networkTimerStart = process.hrtime();
// maxage - seconds
this.#client.set(normalizedKey, json, 'EX', maxage, (error, done) => {
const networkTimer = process.hrtime(networkTimerStart);
const totalTimer = process.hrtime(totalTimerStart);
if (error) {
this.#log({
type: EVENT.REDIS_CACHE_WRITE_ERROR,
error,
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});
reject(deError({
Expand All @@ -345,8 +312,8 @@ export class Cache<Result> implements CacheInterface<Result> {
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});
reject(deError({
Expand All @@ -359,8 +326,8 @@ export class Cache<Result> implements CacheInterface<Result> {
key,
normalizedKey,
timers: {
network: networkTimer,
total: totalTimer,
start,
end: Date.now(),
},
});
resolve();
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
// "noEmit": true, /* Disable emitting files from a compilation. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./build", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
"removeComments": false, /* Disable emitting comments. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
Expand Down

0 comments on commit 7f4650e

Please sign in to comment.