Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminate the program when the maximum ping difference is exceeded #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bin/smee.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ program
.usage('[options]')
.option('-u, --url <url>', 'URL of the webhook proxy service. Default: https://smee.io/new')
.option('-t, --target <target>', 'Full URL (including protocol and path) of the target service the events will forwarded to. Default: http://127.0.0.1:PORT/PATH')
.option('-h, --healthcheck <interval>', 'Perform health checks based on received pings at specified intervals (in seconds)')
.option('-m, --max-ping-difference <seconds>', 'The maximum difference between the last ping and the current time (in seconds) before the client is considered unhealthy. Default: 60', 60)
.option('-p, --port <n>', 'Local HTTP server port', process.env.PORT || 3000)
.option('-P, --path <path>', 'URL path to post proxied requests to`', '/')
.parse(process.argv)
Expand All @@ -21,13 +23,11 @@ const {
} = opts

async function setup () {
let source = opts.url
const source = opts.url || await Client.createChannel()
const healthcheck = Number.parseInt(opts.healthcheck, 10)
const maxPingDifference = Number.parseInt(opts.maxPingDifference, 10)

if (!source) {
source = await Client.createChannel()
}

const client = new Client({ source, target })
const client = new Client({ source, target, healthcheck, maxPingDifference })
client.start()
}

Expand Down
28 changes: 28 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@ type Severity = "info" | "error";
interface Options {
source: string;
target: string;
healthcheck: number;
maxPingDifference: number;
logger?: Pick<Console, Severity>;
fetch?: any;
}

class Client {
source: string;
target: string;
healthcheck: number;
maxPingDifference: number;
lastPing: number;
fetch: typeof global.fetch;
logger: Pick<Console, Severity>;
events!: EventSource;

constructor({
source,
target,
healthcheck,
maxPingDifference,
logger = console,
fetch = global.fetch,
}: Options) {
this.source = source;
this.target = target;
this.healthcheck = healthcheck;
this.maxPingDifference = maxPingDifference;
this.lastPing = Date.now();
this.logger = logger!;
this.fetch = fetch;

Expand Down Expand Up @@ -85,6 +95,11 @@ class Client {
this.logger.info("Connected", this.events.url);
}

onping() {
this.logger.info(`Received a ping on ${new Date().toISOString()}`);
this.lastPing = Date.now();
}

onerror(err: any) {
this.logger.error(err);
}
Expand All @@ -99,6 +114,19 @@ class Client {
events.addEventListener("open", this.onopen.bind(this));
events.addEventListener("error", this.onerror.bind(this));

if (this.healthcheck) {
events.addEventListener("ping", this.onping.bind(this));

setInterval(() => {
const difference = (Date.now() - this.lastPing) / 1000;

if (difference > this.maxPingDifference) {
this.logger.error(`Maximum ping difference exceeded. (Difference: ${difference.toFixed(4)}s, Maximum Allowed: ${this.maxPingDifference}s)`);
process.exit(1);
}
}, this.healthcheck * 1000);
}

this.logger.info(`Forwarding ${this.source} to ${this.target}`);
this.events = events;

Expand Down