Skip to content

Commit

Permalink
feat: add Worker and Worker Pool (unstable)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathe42 committed Apr 21, 2022
1 parent caf0cc1 commit 009d37f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ const client = new SmtpClient({
});
```

## Pool, Worker
> This is unstable API may change! This requires deno to run in unstable mode.
Adds 2 new classes `SMTPWorker` and `SMTPWorkerPool` (for constructor options see code for now). This creates a SMTP client (or multiple) that get automaticly killed if the connection is not used for around 60s.

## TLS issues
When getting TLS errors make shure:
1. you use the correct port (mostly 25, 587, 465)
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export type {
} from "./config.ts";
export { SmtpClient } from "./smtp.ts";
export { quotedPrintableEncode } from "./encoding.ts";
export { SMTPWorker, SMTPWorkerPool } from './pool.ts'
107 changes: 107 additions & 0 deletions pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { ConnectConfigWithAuthentication, SendConfig } from "./config.ts";

export class SMTPWorker {
#timeout: number;

constructor(
config: ConnectConfigWithAuthentication,
{ timeout = 60000, autoconnect = true } = {},
) {
this.#config = config;
this.#timeout = timeout;
if(autoconnect) {
this.#startup();
}
}
#w!: Worker;
#idleTO: number | null = null;
#idleMode2 = false;
#noCon = true;
#config: ConnectConfigWithAuthentication;

#startup() {
this.#w = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
deno: {
permissions: {
net: "inherit",
// ts files
read: true,
},
namespace: true,
},
});

this.#w.addEventListener("message", (ev: MessageEvent<boolean>) => {
if (ev.data) {
this.#stopIdle();
} else {
if (this.#idleMode2) {
this.#cleanup();
} else {
this.#startIdle();
}
}
});

this.#w.postMessage({
__setup: this.#config,
});

this.#noCon = false;
}

#startIdle() {
console.log("started idle");
if (this.#idleTO) return;

this.#idleTO = setTimeout(() => {
console.log("idle mod 2");
this.#idleMode2 = true;
this.#w.postMessage({ __check_idle: true });
}, this.#timeout);
}

#stopIdle() {
if (this.#idleTO) clearTimeout(this.#idleTO);

this.#idleMode2 = false;
this.#idleTO = null;
}

#cleanup() {
console.log("killed");
this.#w.terminate();
this.#stopIdle();
}

public send(mail: SendConfig) {
this.#stopIdle();
if (this.#noCon) {
this.#startup();
}
this.#w.postMessage(mail);
}
}

export class SMTPWorkerPool {
pool: SMTPWorker[] = []

constructor(
config: ConnectConfigWithAuthentication,
{ timeout = 60000, size = 2 } = {}
) {
for (let i = 0; i < size; i++) {
this.pool.push(new SMTPWorker(config, {timeout, autoconnect: i === 0}))
}
}

#lastUsed = -1

send(mail: SendConfig) {
this.#lastUsed = (this.#lastUsed + 1) % this.pool.length

this.pool[this.#lastUsed].send(mail)
}
}

40 changes: 40 additions & 0 deletions worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />
/// <reference lib="deno.unstable" />

import { SmtpClient } from "./smtp.ts";
import { SendConfig } from "./config.ts";

const client = new SmtpClient({ console_debug: true });

let cb: () => void;
const readyPromise = new Promise<void>((res) => {
cb = res;
});

let hasIdlePromise = false;

async function send(config: SendConfig) {
client.send(config);

if (!hasIdlePromise) {
hasIdlePromise = true;
await client.idle;
postMessage(false);
hasIdlePromise = false;
}
}

addEventListener("message", async (ev: MessageEvent) => {
if (ev.data.__setup) {
await client.connectTLS(ev.data.__setup);
cb();
return;
}
if (ev.data.__check_idle) {
postMessage(client.isSending);
return;
}
await readyPromise;
send(ev.data);
});

0 comments on commit 009d37f

Please sign in to comment.