Skip to content

Commit

Permalink
include worker id in all error messages from worker code
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly committed Dec 7, 2023
1 parent 709b013 commit fa68901
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const debug = createDebug(
}`,
)

const errorPrefix = `ClusterMemoryStoreWorker:${
cluster.worker?.id ?? 'not-a-worker'
}:`

/**
* A `Store` for the `express-rate-limit` package that communicates with the primary process to store and retrieve hits
*/
Expand Down Expand Up @@ -63,9 +67,7 @@ export class ClusterMemoryStoreWorker implements Store {
this.prefix = options?.prefix ?? 'default'
if (!cluster.isWorker) {
console.warn(
new Error(
'ClusterMemoryStoreWorker instance created in non-worker process',
),
new Error(errorPrefix + ' instance created in non-worker process'),
)
}
}
Expand All @@ -78,18 +80,18 @@ export class ClusterMemoryStoreWorker implements Store {
* @impl
*/
async init(options: RateLimitConfiguration) {
debug('Initializing with options %o', options)
debug('Initializing with parent options %o', options)
this.windowMs = options.windowMs
if (!cluster.worker) {
throw new Error(
'ClusterMemoryStoreWorker: cluster.worker is undefined, unable to initialize',
`${errorPrefix} cluster.worker is undefined, unable to initialize`,
)
}

cluster.worker.on('message', this.onMessage.bind(this))
return this.send('init', [{ windowMs: this.windowMs }]).catch(
(error: any) => {
console.error('ClusterMemoryStoreWorker: failed to initialize', error)
console.error(`${errorPrefix} failed to initialize`, error)
},
)
}
Expand Down Expand Up @@ -137,7 +139,7 @@ export class ClusterMemoryStoreWorker implements Store {
const timeoutId = setTimeout(() => {
reject(
new Error(
`ClusterMemoryStoreWorker: no response recieved to ${command} command after ${timelimit}ms.`,
`${errorPrefix} no response recieved to ${command} command after ${timelimit}ms.`,
),
)
this.openRequests.delete(requestId)
Expand All @@ -146,7 +148,7 @@ export class ClusterMemoryStoreWorker implements Store {
if (!process.send) {
reject(
new Error(
'ClusterMemoryStoreWorker: process.send is undefined, indicating that this is probably not a node:cluster worker.',
`${errorPrefix} process.send is undefined, indicating that this is probably not a node:cluster worker.`,
),
)
return
Expand Down Expand Up @@ -175,7 +177,8 @@ export class ClusterMemoryStoreWorker implements Store {
if (!shouldSendMore) {
console.warn(
new Error(
'CluserMemoryStoreWorker: process.send() returned false indicating that the channel is closed and/or there is a large backlog of messages waiting to be sent.',
errorPrefix +
' process.send() returned false indicating that the channel is closed and/or there is a large backlog of messages waiting to be sent.',
),
)
}
Expand All @@ -196,7 +199,7 @@ export class ClusterMemoryStoreWorker implements Store {
} else {
console.warn(
new Error(
'ClusterMemoryStoreWorker: response recieved without matching open request: ' +
`${errorPrefix} response recieved without matching open request: ` +
JSON.stringify(message_),
),
)
Expand Down

0 comments on commit fa68901

Please sign in to comment.