Skip to content

Commit

Permalink
docs(core): added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MathurAditya724 committed Jun 29, 2024
1 parent 8591788 commit 2707cac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const SERVICE_UNAVAILABLE = 503;
const eventLoopUtilization: EventLoopUtilityFunction | undefined =
performance.eventLoopUtilization;

/**
* Setup the underPressure plugin.
*
* @param handler - Receives an array of middlewares and returns a server instance.
* @param config - Configuration options for the underPressure plugin.
*/
export function underPressure<
E extends { Variables: UnderPressureVariables },
P extends string = string,
Expand Down Expand Up @@ -199,6 +205,11 @@ export function underPressure<
}
}

/**
* Checks if the system is under pressure based on the configured thresholds.
*
* @returns {boolean} True if the system is under pressure, false otherwise.
*/
function isUnderPressure() {
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
return true;
Expand Down Expand Up @@ -226,6 +237,11 @@ export function underPressure<
return false;
}

/**
* Returns the current memory usage statistics.
*
* @returns {object} An object containing memory usage statistics.
*/
function memoryUsage() {
return {
eventLoopDelay,
Expand All @@ -235,6 +251,9 @@ export function underPressure<
};
}

/**
* Cleans up resources when the server is closed.
*/
function onClose() {
clearTimeout(timer);
clearTimeout(externalHealthCheckTimer);
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Context, Input } from "hono";

/**
* Variables added in the Hono context by using this plugin.
*/
export type UnderPressureVariables = {
memoryUsage: () => {
eventLoopDelay: number;
Expand All @@ -20,7 +23,7 @@ export type UnderPressureVariables = {
};

/**
* The configuration options for the rate limiter.
* The configuration options for the under pressure plugin.
*/
export interface ConfigType<
E extends { Variables: UnderPressureVariables },
Expand All @@ -43,6 +46,9 @@ export interface ConfigType<
) => Promise<void> | void;
}

/**
* Enum of pressure types.
*/
export enum PressureType {
EVENT_LOOP_DELAY = "eventLoopDelay",
HEAP_USED_BYTES = "heapUsedBytes",
Expand Down

0 comments on commit 2707cac

Please sign in to comment.