-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Worker and Worker Pool (unstable)
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |